Last Updated: May 18, 2018
· 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


(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:


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:


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 ·