ElasticSearch escape special character

How I escape Unicode characters using a query_string query? For example, my documents consist of the following data:

{
    "title":"Sachin$tendular"
     } 

I am using the query_string query in the following way:

{
  "query": {
    "query_string": {
      "fields": ["title"],
      "query": "*Sachin$*"
    }
  }
}

but it is not giving me any result and if I removed $ from the query it works

SO how we can handle $ here?

The backslash character is normally used just before reserved characters to escape them.

How we can use contains query in elastic search?

Your problem relates to how the content is stored in the index.
Normally text strings like this sentence are chopped into individual words and put in the index.
Your issue is likely caused by the fact that by default the $ character is thrown away and used as a word separator (like white space does).
Example:

Create a new index with your example doc:

DELETE test

POST test/_doc/1
{
    "title":"Sachin$tendular"
}

This automatically created a default schema (mapping) which we can now use to show how it chops up strings:

POST test/_analyze
{
  "field": "title",
  "text": ["Sachin$tendular"]
}

This shows us what was put in the index:

{
  "tokens" : [
    {
      "token" : "sachin",
      "start_offset" : 0,
      "end_offset" : 6,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "tendular",
      "start_offset" : 7,
      "end_offset" : 15,
      "type" : "<ALPHANUM>",
      "position" : 1
    }
  ]
}

Note there are 2 words - sachin and tendular.
People often want to search on exact values rather than individual words ("tokens"). For this reason we also by default index short strings into an un-tokenized field called xxx.keyword where xxx is the name of the field in the original JSON.
Long story short - you can query what the full value contains using:

POST test/_search
{
  "query": {
    "query_string": {
      "fields": ["title.keyword"],
      "query": "*Sachin$*"
    }
  }
}
2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.