Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

How to escape query containing special characters? #119

Closed
masumsoft opened this issue Apr 20, 2016 · 1 comment
Closed

How to escape query containing special characters? #119

masumsoft opened this issue Apr 20, 2016 · 1 comment
Labels

Comments

@masumsoft
Copy link

masumsoft commented Apr 20, 2016

After digging into the lucene docs, I found following are the characters I should escape if present in my query string:

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ /

But if the query contains a single quote which is not present in the above list of characters also throws error. So my question is how do I escape my query in this plugin and what are the characters that needs to be escaped? Also I'm assuming the escape character is \ (backslash) and not " (quote) like in cassandra.

@adelapena
Copy link
Contributor

Hi @masumsoft,

The aforementioned special characters are part of the Lucene query syntax and should be escaped with double backslash (\). This escaping is only used in queries depending on Lucene syntax. Also, single quotation mark (') is a special character for CQL, and it should be escaped with another single quotation mark in all CQL occurrences.

Here you have a small example showing both types of escaping:

CREATE KEYSPACE test WITH replication = {'class':'SimpleStrategy', 'replication_factor': 1};
USE test;

CREATE TABLE test (
    id int PRIMARY KEY, 
    text text);

INSERT INTO test(id, text) VALUES (2,  'a''b'); -- Ecaped quotation mark
INSERT INTO test(id, text) VALUES (3,  'a[b]');

-- Note the stored single quotation
SELECT * FROM test;

-- Create index
CREATE CUSTOM INDEX test_idx ON test() using 'com.stratio.cassandra.lucene.Index' 
WITH OPTIONS = {
    'refresh_seconds' : '1',
    'schema' : '{ fields : { text : {type : "string"} } }'
};

SELECT * FROM test WHERE expr(test_idx, '{filter : {type: "match", field : "text", value : "a''b"}}'); -- Escaped quotation mark
SELECT * FROM test WHERE expr(test_idx, '{filter : {type: "match", field : "text", value : "a[b]"}}'); -- Escaping is not required
SELECT * FROM test WHERE expr(test_idx, '{filter : {type: "wildcard", field : "text", value : "a\\[b\\]"}}'); -- Escaped brackets
SELECT * FROM test WHERE expr(test_idx, '{filter : {type: "lucene", query : "text:a\\[b\\]"}}'); -- Escaped brackets

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants