# Not getting correct results when using special characters in query string

**URL:** <https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372>\
**Category:** Elasticsearch\
**Created:** [February 21, 2019, 10:09am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372 "2019-02-21T10:09:28Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 21, 2019, 10:09am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/1 "2019-02-21T10:09:28Z")

</div>

Hi am new to elastic search and trying to use query string as below but not getting the excat matches if use special characters

```auto
GET_search
{
    "query": { 
         "bool": { 
               "must": { 
                     "query_string": { 
                             "query":"*lr-*", 
                                      "fields": ["title","description"]
                        }
                   }
              }
     }
}

```

```auto
 
 
GET_search
{
     "query": { 
          "bool": { 
                 "must": { 
                       "query_string": { 
                               "query":"*lr/*", 
                                        "fields": ["title","description"]
                          }
                  }
            }
      }
}

```

Can anyone please help me out

Thanks in advance  
Shyam

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 10:27am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/2 "2019-02-21T10:27:32Z")

</div>

It depends on documents, mapping...

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 21, 2019, 11:21am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/3 "2019-02-21T11:21:00Z")

</div>

Hi

I cannot recreate the index as i only acessing it please dind the mappings what i got

```auto
{
  "test-index": {
    "mappings": {
      "names": {
        "properties": {
          "Feild1": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "Feild2": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

```

Regards  
Shyam

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 11:37am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/4 "2019-02-21T11:37:25Z")

</div>

You can always reproduce an example in a blank new test cluster.

Anyway, your problem is because of the way elasticsearch is analyzing and indexing the text.  
Note that your example does not contain a `title` field or a `description`.

If you need more help, please provide a working example as I suggested.

---

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 21, 2019, 1:19pm UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/5 "2019-02-21T13:19:24Z")

</div>

Hi

I created a test\_index and sharing the details

```
PUT test_index/1/101
{
  "title": "LR-123QZE",
  "description": "LR-123QZE description"
}
PUT test_index/1/102
{
  "title": "Lr-124QZE",
  "description": "Lr-124QZE description"
}
PUT test_index/1/103
{
  "title": "lr-125QZE",
  "description": "lr-125QZE description"
}
PUT test_index/1/104
{
  "title": "LR-123QZE",
  "description": "LR 126QZE description"
}
PUT test_index/1/105
{
  "title": "LR-123QZE",
  "description": "lr 123QZE description"
}
PUT test_index/1/106
{
  "title": "ID/123WDER",
  "description": "ID/123WDER description"
}

GET test_index/_search
{
  "query": {  
		"bool": {  
			"must": {  
				"query_string": {  
					"query":"*lr-*", ///comment: "query":"*LR-*" or "query":"*lr\\-*", or "query":"*LR\\-*", 
					"fields": ["title","description"]
				}
			}
		}
	}
}

```

result:

```auto
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
      }

```

expected result:

```auto
    "hits": [
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "101",
            "_score": 1,
            "_source": {
              "title": "LR-123QZE",
              "description": "LR-123QZE description"
            }
          },
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "103",
            "_score": 1,
            "_source": {
              "title": "lr-125QZE",
              "description": "lr-125QZE description"
            }
          },
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "104",
            "_score": 1,
            "_source": {
              "title": "LR-123QZE",
              "description": "LR 126QZE description"
            }
          },
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "102",
            "_score": 1,
            "_source": {
              "title": "Lr-124QZE",
              "description": "Lr-124QZE description"
            }
          }
        ]
      }

```

Query:

```auto
      GET test_index/_search
    {
      "query": {  
    		"bool": {  
    			"must": {  
    				"query_string": {  
    					"query":"*ID/*", 
    					"fields": ["title","description"]
    				}
    			}
    		}
    	}
    }

```

result: error

```auto
    {
      "error": {
        "root_cause": [
          {
            "type": "query_shard_exception",
            "reason": "Failed to parse query [*ID/*]",
            "index_uuid": "aXYtffzVTFeQsxtxEDyGgg",
            "index": "test_index"
          }
        ]

```

expected result:error

Query

```auto
      GET test_index/_search
    {
      "query": {  
    		"bool": {  
    			"must": {  
    				"query_string": {  
    					"query":"*ID\\/*", 
    					"fields": ["title","description"]
    				}
    			}
    		}
    	}
    }

```

result:

```````auto
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": [\*]
      }
     ``````
     expected result:

```````

```
 "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
     {
        "_index": "test_index",
        "_type": "1",
        "_id": "106",
        "_score": 1,
        "_source": {
          "title": "ID/123WDER",
          "description": "ID/123WDER description"
        }
      }
    ]
  }

```

```auto
mapping details

```

```
  {
  "test_index": {
    "mappings": {
      "1": {
        "properties": {
          "description": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

```

```auto

Regards,
Shyam
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 1:31pm UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/6 "2019-02-21T13:31:16Z")

</div>

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.  
Please update your post.

Here search in `title.keyword` instead.

---

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 21, 2019, 1:58pm UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/7 "2019-02-21T13:58:53Z")

</div>

Hi

Sorry for the formatting issue. I have corrected it now.

I tried with `title.keyword` but now it returning only case sensitive results  
I need all data irrespective of the case

```auto
GET test_index/_search
{
  "query": {  
		"bool": {  
			"must": {  
				"query_string": {  
					"query":"*lr-*", 
					"fields": ["title.keyword","description.keyword"]
				}
			}
		}
	}
}

```

result:

```auto
"hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "1",
        "_id": "103",
        "_score": 1,
        "_source": {
          "title": "lr-125QZE",
          "description": "lr-125QZE description"
        }
      }
    ]
  }

```

Regards,  
Shyam

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 2:22pm UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/8 "2019-02-21T14:22:57Z")

</div>

That's expected. You can define a lowercase normalizer to the keyword field or change the analyzer of the title field to `simple`.

---

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 21, 2019, 3:32pm UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/9 "2019-02-21T15:32:53Z")

</div>

Hi,

For this I need to re-create my index? and if yes in all PUT calls I need to add the normalizer or analyzer. and can you please provide some sample examples for the above both ways.

Regards,  
Shyam

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 21, 2019, 5:00pm UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/10 "2019-02-21T17:00:49Z")

</div>

> For this I need to re-create my index?

Yes.

> can you please provide some sample examples for the above both ways.

The documentation is normally pretty much explicit:

- [normalizer | Elasticsearch Guide [6.6] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/normalizer.html)
- [analyzer | Elasticsearch Guide [6.6] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/analyzer.html)

---

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 22, 2019, 7:38am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/11 "2019-02-22T07:38:13Z")

</div>

Hi,

Thanks for support. Will try to use normalizer or analyzer .

Regards,  
Shyam

---

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 22, 2019, 10:32am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/12 "2019-02-22T10:32:11Z")

</div>

Hi ,

Can we achieve 'keyword'case sensitivity without analyzer or normalizer without changing the index?

Thanks ,  
Shyam

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 22, 2019, 10:52am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/13 "2019-02-22T10:52:57Z")

</div>

Not really in a fast way at least.

---

<div class="post-metadata">

**Author:** ![\_Shyam](https://avatars.discourse-cdn.com/v4/letter/_/b5e925/32.png) [@\_Shyam](https://discuss.elastic.co/u/_Shyam)\
**Post date:** [February 25, 2019, 8:21am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/14 "2019-02-25T08:21:38Z")

</div>

Hi

I tried the changes it’s working but not for all special characters i need to append \ for each time. Is there any way to replace all special characters by handling in query itself

Regards  
Shyam

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 25, 2019, 9:38am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/15 "2019-02-25T09:38:33Z")

</div>

I can't really know with a fully working example. Read [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

---

<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:** [March 25, 2019, 9:38am UTC](https://discuss.elastic.co/t/not-getting-correct-results-when-using-special-characters-in-query-string/169372/16 "2019-03-25T09:38:38Z")

</div>

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