# ElasticSearch escape special character

**URL:** <https://discuss.elastic.co/t/elasticsearch-escape-special-character/275765>\
**Category:** Elasticsearch\
**Created:** [June 13, 2021, 3:03pm UTC](https://discuss.elastic.co/t/elasticsearch-escape-special-character/275765 "2021-06-13T15:03:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![surajdalvi](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@surajdalvi](https://discuss.elastic.co/u/surajdalvi)\
**Post date:** [June 13, 2021, 3:03pm UTC](https://discuss.elastic.co/t/elasticsearch-escape-special-character/275765/1 "2021-06-13T15:03:44Z")

</div>

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

```auto
{
    "title":"Sachin$tendular"
     } 

```

I am using the query\_string query in the following way:

```auto
{
  "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?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 13, 2021, 5:41pm UTC](https://discuss.elastic.co/t/elasticsearch-escape-special-character/275765/2 "2021-06-13T17:41:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![surajdalvi](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@surajdalvi](https://discuss.elastic.co/u/surajdalvi)\
**Post date:** [June 14, 2021, 8:44am UTC](https://discuss.elastic.co/t/elasticsearch-escape-special-character/275765/3 "2021-06-14T08:44:49Z")

</div>

How we can use contains query in elastic search?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 14, 2021, 9:34am UTC](https://discuss.elastic.co/t/elasticsearch-escape-special-character/275765/4 "2021-06-14T09:34:24Z")

</div>

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:

```auto
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:

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

```

This shows us what was put in the index:

```auto
{
  "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:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 12, 2021, 9:34am UTC](https://discuss.elastic.co/t/elasticsearch-escape-special-character/275765/5 "2021-07-12T09:34:52Z")

</div>

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