[Solved] Is there any query for Cassandra as same as SQL:LIKE Condition?

The LIKE condition allows us to use wildcards in the where clause of an SQL statement. This allows us to perform pattern matching. The LIKE condition can be used in any valid SQL statement – select, insert, update, or delete. Like this

WHERE user_name like 'babu%';

like the same above operation any query is available for Cassandra in CLI.

Enquirer: BobDroid


Solution #1:

Simple answer: there is no equivalent of LIKE


Here is the command reference for v0.8:


If you maintain another set of rows that hold references to a username:

row: username:bab -> col:babu1, col:babar
row: username:babu -> col:babur

Effectively you are cheating by pre-populating all of the results that you would normally search with in the RDBMS world. Storage is cheap in comparison to what it was years ago … which is why this is now an accepted approach. It’s less intensive on the CPU and Memory to retrieve a pre-populated list of information.

Respondent: sdolgy

Solution #2:

Since Cassandra 3.4 (3.5 recommended), LIKE queries can be achieved using a SSTable Attached Secondary Index (SASI).

For example:

CREATE TABLE cycling.cyclist_name ( 
  lastname text, 
  firstname text

Creating the SASI as follows:

CREATE CUSTOM INDEX  fn_prefix ON cyclist_name (firstname)
USING 'org.apache.cassandra.index.sasi.SASIIndex';

Then a prefix LIKE query is working:

SELECT * FROM cyclist_name WHERE firstname LIKE 'M%';
SELECT * FROM cyclist_name WHERE firstname LIKE 'Mic%';

These examples and more configuration options, like suffix queries, can be found in the documentation

A more in depth explanation about how SASI works can be found here.

Respondent: nstrelow

Solution #3:

I know: It’s a old question but there is a solution for this topic:

You can’t use like operator in cassandra but you can use range operators and with the range operator you can solve this “like ‘whatever%'”

An example:
I have more than one product. Each product has his own partition key (first part of the primary key):

CREATE TABLE user(productId int, username text, PRIMARY KEY(productId, username));

Now i have some users:

INSERT INTO user(productId, username) VALUES (1, 'anna');
INSERT INTO user(productId, username) VALUES (1, 'alpha');
INSERT INTO user(productId, username) VALUES (1, 'andreas');
INSERT INTO user(productId, username) VALUES (1, 'alex');
INSERT INTO user(productId, username) VALUES (1, 'bernd');
INSERT INTO user(productId, username) VALUES (1, 'bob');

Now, i want to find all users which have an a at the beginning. In a SQL world i use LIKE ‘a%’ in Cassandra i use this:

SELECT * FROM user WHERE productId = 1 AND username >= 'a' AND username < 'b';

The result:

productid | username
     1 |     alex
     1 |    alpha
     1 |  andreas
     1 |     anna
Respondent: Citrullin

Solution #4:

I came across this post while I was searching for a solution to execute WHERE column_name LIKE '%keyword%' in Cassandra. The answers were promising but not quite addressing my issue.

CREATE CUSTOM INDEX idx_name ON keyspace.columnfamily (column_name) 
USING 'org.apache.cassandra.index.sasi.SASIIndex' 
'mode': 'CONTAINS', 
'analyzer_class': 'org.apache.cassandra.index.sasi.analyzer.NonTokenizingAnalyzer', 
'case_sensitive': 'false'};

In order to make %keyword% (two %s) works, the index must have options with mode: CONTAINS along with that analyzer_class to make case_sensitive effective.

Respondent: Kamyar

Solution #5:

CQL LIKE statements are in Scylla Open Source 3.2 RC1, the release candidate for Scylla, a CQL-compatible database. We’d love feedback before release. Here’s the details:

  • CQL: LIKE Operation #4477

The new CQL LIKE keyword allows matching any column to a search pattern, using % as a wildcard. Note that LIKE only works with ALLOW FILTERING.

LIKE Syntax support:

‘_’ matches any single character

‘%’ matches any substring (including an empty string)

” escapes the next pattern character, so it matches verbatim

any other pattern character matches itself

an empty pattern matches empty text fields

For example:

INSERT INTO t (id, name) VALUES (17, ‘Mircevski’)

SELECT * FROM t where name LIKE ‘Mirc%’ allow filtering

Source: [RELEASE] Scylla 3.2 RC1 2

Respondent: Peter Corless

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.