# Wildcard search with \_ (underscore) is giving no result

**URL:** <https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010>\
**Category:** Elasticsearch\
**Created:** [January 4, 2018, 5:28am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010 "2018-01-04T05:28:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RohanKumbhar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohankumbhar/32/23649_2.png) [@RohanKumbhar](https://discuss.elastic.co/u/RohanKumbhar)\
**Post date:** [January 4, 2018, 5:28am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/1 "2018-01-04T05:28:54Z")

</div>

Hi Team,

I facing the issue while using wild card search with query containing understore (\_) in it with elasticsearch version 6.0.1  
And i'm using word\_delimiter\_filter for the field my\_field as mentioned in below query

I have document with my\_field = document\_02.txt and i want to search for this document using \_02\* as mentioned in below query but this will give me zero result

```
GET my_index/_search
{
 
 "query": {
   "bool": { 
     "must": [
       
       {
         "query_string": {
           
           "query":"my_field:_02*"
         }
       }
     ]
   }
 }
}

```

Below query gives me the valid search result if wildcard is not used in the query

```
  GET my_index/_search
    {
     
     "query": {
       "bool": { 
         "must": [
           
           {
             "query_string": {
               
               "query":"my_field:_02"
             }
           }
         ]
       }
     }
    }

```

kindly help!

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 4, 2018, 5:40am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/2 "2018-01-04T05:40:00Z")

</div>

Can you show the mapping of your field and the definition of the analyzer you're using for that field?

---

<div class="post-metadata">

**Author:** ![RohanKumbhar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohankumbhar/32/23649_2.png) [@RohanKumbhar](https://discuss.elastic.co/u/RohanKumbhar)\
**Post date:** [January 4, 2018, 5:55am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/3 "2018-01-04T05:55:24Z")

</div>

Here are the steps to replicate the issue

```
PUT my_index
{
    "settings": {
      "index": {
        "analysis": {
          "analyzer": {
            "custom_analyzer": {
              "filter": [
                "word_delimiter",
                "lowercase"
              ],
              "type": "custom",
              "tokenizer": "standard"
            }
          }
        }
      }
    },
    "mappings": {
      "doc": {
        "properties": {
          "my_field": {
            "type": "text",
            "analyzer": "custom_analyzer",
            "fields": {
              "keyword": {
                "ignore_above": 256,
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
  
  

  
PUT my_index/doc/1 
{
  "my_field":"document_02.txt"
}

GET my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "my_field",
            "query": "_02*"
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 4, 2018, 6:01am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/4 "2018-01-04T06:01:40Z")

</div>

I'm not sure why you're using a `word_delimiter` token filter. When analyzing `document_02.txt`, it's going to produce the following tokens (obtained from the `_analyze` endpoint):

```
{
  "tokens": [
    {
      "token": "document",
      "start_offset": 0,
      "end_offset": 8,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "02",
      "start_offset": 9,
      "end_offset": 11,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "txt",
      "start_offset": 12,
      "end_offset": 15,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}

```

If you want to search inside words, what you need to to leverage the [`ngram` token filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenfilter.html)

---

<div class="post-metadata">

**Author:** ![RohanKumbhar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohankumbhar/32/23649_2.png) [@RohanKumbhar](https://discuss.elastic.co/u/RohanKumbhar)\
**Post date:** [January 4, 2018, 6:35am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/5 "2018-01-04T06:35:25Z")

</div>

> [@val](#):
>
> I'm not sure why you're using a word\_delimiter token filter. When analyzing document\_02.txt, it's going to produce the following tokens (obtained from the \_analyze endpoint):

I'm using word\_delimiter filter as i want to create token based on only words and numbers in it and not on any special character.

Just wanted to know any fix for this issue without changing the filter to ngram  
or any workaround to fix the same?

Kindly help

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 4, 2018, 6:37am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/6 "2018-01-04T06:37:58Z")

</div>

Ok I understand. Since `document_02.txt` gets tokenized and indexed as the three tokens `document`, `02` and `txt`, you can search for `02*` instead of `_02*` since the underscore is discarded during the analysis process.

---

<div class="post-metadata">

**Author:** ![RohanKumbhar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohankumbhar/32/23649_2.png) [@RohanKumbhar](https://discuss.elastic.co/u/RohanKumbhar)\
**Post date:** [January 4, 2018, 6:41am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/7 "2018-01-04T06:41:43Z")

</div>

Sure , thanks @val !!

---

<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:** [February 1, 2018, 6:42am UTC](https://discuss.elastic.co/t/wildcard-search-with-underscore-is-giving-no-result/114010/8 "2018-02-01T06:42:10Z")

</div>

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