# Elastic Search Query to fetch data control or special characters

**URL:** <https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839>\
**Category:** Elasticsearch\
**Created:** [March 17, 2020, 3:55am UTC](https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839 "2020-03-17T03:55:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MangeshTech](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@MangeshTech](https://discuss.elastic.co/u/MangeshTech)\
**Post date:** [March 17, 2020, 3:55am UTC](https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839/1 "2020-03-17T03:55:02Z")

</div>

Trying to search for severity values with special characters in it e.g. "1 - Critical". tried with backslashes to see if I can get the exact results.  
If I change the query below with "\*2 \\- Cri\*", it also returns documents with values '1 - Critical' and '2 - Major'. I tried by putting .keyword but no results. Pls. assist.

POST /\_search  
{

"from" : 0, "size" : 1000,  
"query":  
{  
"bool": {  
"must": [  
{"query\_string": {"fields": ["severity"],  
"query": "\*1 \\- Cr\*"}  
}  
]  
}  
},  
"\_source" : ["ticketNo","severity"]  
}

---

<div class="post-metadata">

**Author:** ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)\
**Post date:** [March 17, 2020, 4:08am UTC](https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839/2 "2020-03-17T04:08:15Z")

</div>

Please add sample doc and mapping.

---

<div class="post-metadata">

**Author:** ![MangeshTech](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@MangeshTech](https://discuss.elastic.co/u/MangeshTech)\
**Post date:** [March 18, 2020, 3:00am UTC](https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839/3 "2020-03-18T03:00:34Z")

</div>

Mapping :

{  
"test" : {  
"aliases" : { },  
"mappings" : {  
"properties" : {  
"severity" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
},  
"ticketNo" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
}  
}  
}

Sample docs :  
POST test/\_doc/1  
{  
"severity" : "1 - Critical",  
"ticketNo" : "1"  
}

POST test/\_doc/2  
{  
"severity" : "2 - Major",  
"ticketNo" : "2"  
}

---

<div class="post-metadata">

**Author:** ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)\
**Post date:** [March 18, 2020, 3:29am UTC](https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839/4 "2020-03-18T03:29:54Z")

</div>

When you want to use wildcard in query string then use the `keyword` type of field. Here, `severity.keyword`. Also you need to escape only those special character which are part of lucene query syntax. The query would be:

```
POST /_search
{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "fields": [
              "severity"
            ],
            "query": "*1 - Cri*"
          }
        }
      ]
    }
  },
  "_source": [
    "ticketNo",
    "severity"
  ]
}

```

**NOTE:**

1. Wildcard queries are heavy. Use `*` with care.
2. Querying on `keyword` type of field will be case-sensitive.

---

<div class="post-metadata">

**Author:** ![MangeshTech](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@MangeshTech](https://discuss.elastic.co/u/MangeshTech)\
**Post date:** [April 3, 2020, 10:38pm UTC](https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839/5 "2020-04-03T22:38:58Z")

</div>

Thanks Nishant, what is the option if I want to enable case-insensitive search with wild card

---

<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:** [May 1, 2020, 10:39pm UTC](https://discuss.elastic.co/t/elastic-search-query-to-fetch-data-control-or-special-characters/223839/6 "2020-05-01T22:39:09Z")

</div>

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