Last Updated: May 18, 2018
·
5.371K
· craigmarvelley

ElasticSearch field queries containing slashes

If you're indexing some unanalyzed content that contains backslashes, you might find that the results you're expecting aren't returned when making queries. For instance, assume you're indexing this string

MyIndex\MyType\Classname

(which happens to be a PHP class name), into a field on your index called className. You've indexed it unanalyzed, so ElasticSearch indexes the string without any modification. Now you want to match that value in a query. If you were to query over HTTP, your URL would look something like this:

http://localhost:9200/my_index/my_type/_search?pretty=true&q=className:MyIndex\MyType\Classname

But you wouldn't get any results; ElasticSearch requires backslashes to be escaped in order to match. So a working query would look like this:

http://localhost:9200/my_index/my_type/_search?pretty=true&q=className:MyIndex\\MyType\\Classname

If you're querying through PHP, as I am, the addslashes() function is your friend, or even a simple str_replace(). There may well be a nicer way to get around this, but it worked for me!

1 Response
Add your response

That's a nice tip. But first you have to integrate Elasticsearch in PHP. You can do that using Elasticsearch PHP API.

over 1 year ago ·