How to request the character "%" using the LIKE operator in Cassandra 3.7?

I am using Cassandra 3.7 and a text column with a SASI index. Suppose I want to find the values ​​of columns that contain the symbol "%" somewhere in the middle. The problem is that “%” is the char command for LIKE clauses. How to avoid "%" char in type request LIKE '%%%'?

Here is the test script:

DROP keyspace if exists kmv;
CREATE keyspace if not exists kmv WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor':'1'} ;
USE kmv;
CREATE TABLE if not exists kmv (id int, c1 text, c2 text, PRIMARY KEY(id, c1));
CREATE CUSTOM INDEX ON kmv.kmv  ( c2 ) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {
'analyzed' : 'true',
'analyzer_class' : 'org.apache.cassandra.index.sasi.analyzer.NonTokenizingAnalyzer',
'case_sensitive' : 'false', 
'mode' : 'CONTAINS'
};  

INSERT into kmv (id, c1, c2) values (1, 'f22', 'qwe%asd');  
SELECT c2 from kmv.kmv where c2 like '%$$%$$%';

The select query returns nothing.

+4
source share
2 answers

I think you can use the $$ syntax to achieve this. The where clause should be:

LIKE '% $$% $$%'

: https://docs.datastax.com/en/cql/3.3/cql/cql_reference/escape_char_r.html

+1

. LIKE '%%%' .

cqlsh:kmv> SELECT c2 from kmv.kmv where c2 like '%%%';

 c2
----------
 qwe%asd

, , , , , , , %.

0

Source: https://habr.com/ru/post/1653818/


All Articles