Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape reserved characters #86

Open
intranets-talk opened this issue Apr 3, 2017 · 2 comments
Open

Escape reserved characters #86

intranets-talk opened this issue Apr 3, 2017 · 2 comments

Comments

@intranets-talk
Copy link

intranets-talk commented Apr 3, 2017

Hi,
I've managed to set up Plastic as per documentation info - and works fine, thank you.

I'm having a issue where using ES "reserved characters" in queries throws a BadRequest400Exception - query_shard_exception: Failed to parse query. Example: searching for test: throws out the mentioned exception.

I am aware that this is more of a ES related issue, not Plastic's but I was wondering how others managed to get around this. Is query sanitation required, or do I need to use some regex? I know I can catch the exception, but I rather not.

Example of how I am building the query:

Attachment::search()->querystring('*' . $query . '*' , ['fields' => ['name^4', 'file_slug', 'path', 'extension', 'size']])->size(15)->get()->hits();     

The ES reserved characters are listed here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters

2017-04-03_10-51

@sleimanx2
Copy link
Owner

Yes as per the documentation not escaping the reserved characters could lead to a syntax error. I'm still not sure if escaping those characters automatically is a good idea. Do you have any insights?

@intranets-talk
Copy link
Author

intranets-talk commented Apr 5, 2017

Agree, escaping the reserved characters it's probably not the way to go, and playing with this can render inaccurate search results.

What I am doing right now is allowing some of the characters (which I believe are "most used"), like + : - &, and escape the rest by using preg_replace . Those only throw the exception if a wildcard is used in query (within the controller, like in my example above). So I have removed the wildcards from the query.

This takes away some of the ES functionality, but I have nothing better so far. At least this way, I get accurate results.

Also, in this scenario I am still getting an exception error if no term is used after the search operator (for example: test + will throw the error and test +foo will parse fine). I am also catching this error.

Since ES plugs in quite a few languages, I guess it is up to the programmer to deal with search operators in queries.

I believe that this can be closed, is it not as issue with Plastic, but a request for info. Thanks again.

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

No branches or pull requests

2 participants