Search code examples
elasticsearch

elasticseach escape special characters


I am running the following query

GET myindex/my_type/_search
{
  "query": {
    "query_string": {
      "default_field": "campaign_new",
      "query": "(firtPart - Second Third)"
    }
  }
}

and I am getting

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

But I have values for this campaign if I do a match_all search, but it is not fetched by this query. I have doubt that this might be due to the escaping issue. But when I tried firtPart \- Second\ Third, it is showing an error in Sense.


Solution

  • You need to escape the hypen twice. I assume you are trying to do an exact match, so you only need to wrap the query in double quotes, which also needs to be escaped (quotes only need to be escaped once).

    {
        "query": {
            "query_string": {
                "default_field": "campaign_new",
                "query": "\"(firtPart - Second Third)\""
            }
        }
    }