# Wildcard searches

**URL:** <https://discuss.elastic.co/t/wildcard-searches/137594>\
**Category:** Elasticsearch\
**Created:** [June 27, 2018, 10:08am UTC](https://discuss.elastic.co/t/wildcard-searches/137594 "2018-06-27T10:08:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Shiriyara](https://avatars.discourse-cdn.com/v4/letter/r/c89c15/32.png) [@Rakesh\_Shiriyara](https://discuss.elastic.co/u/Rakesh_Shiriyara)\
**Post date:** [June 27, 2018, 10:08am UTC](https://discuss.elastic.co/t/wildcard-searches/137594/1 "2018-06-27T10:08:17Z")

</div>

Currently i use below wildcard search for my service,

```
{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "PRODUCT_DESCRIPTION": "\*collaboration\*services\*shiriyara\*"
          }
        }
      ]
    }
  }
}

```

This returns me expected result. But i am looking for alternative ways to achieve this without using wildcard query, as wildcard takes more time.

I tried "query\_string" on a "standard" analyzed field. But this returns result if whole word matches.

```
  "query_string": {
    "default_field": "PRODUCT_DESCRIPTION",
    "default_operator": "AND",
    "query": "collaboration services shiriyara"
  }

```

If the string is "collab services shiriyara", it won't give any result, whereas wildcard gives.

Let me know, if anybody has thoughts. Index time changes also fine with me.

---

<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:** [June 27, 2018, 11:13am UTC](https://discuss.elastic.co/t/wildcard-searches/137594/2 "2018-06-27T11:13:08Z")

</div>

It'd be good to Ngram based analyzer. See [https://www.elastic.co/guide/en/elasticsearch/reference/6.3/analysis-ngram-tokenfilter.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/analysis-ngram-tokenfilter.html)

---

<div class="post-metadata">

**Author:** ![Rakesh\_Shiriyara](https://avatars.discourse-cdn.com/v4/letter/r/c89c15/32.png) [@Rakesh\_Shiriyara](https://discuss.elastic.co/u/Rakesh_Shiriyara)\
**Post date:** [June 29, 2018, 7:36am UTC](https://discuss.elastic.co/t/wildcard-searches/137594/3 "2018-06-29T07:36:59Z")

</div>

I tried ngram with this metadata.....  
"ngram\_filter": {  
"type": "ngram",  
"min\_gram": "3",  
"max\_gram": "10"  
}........  
"ngramAnalyzerKey": {  
"filter": [  
"lowercase"  
,  
"ngram\_filter"  
],  
"type": "custom",  
"tokenizer": "keyword"  
}.....

This matches if any of the word in query string matches even though there is a AND in query. Can you help ?

---

<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:** [June 29, 2018, 8:08am UTC](https://discuss.elastic.co/t/wildcard-searches/137594/4 "2018-06-29T08:08:24Z")

</div>

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:** ![Rakesh\_Shiriyara](https://avatars.discourse-cdn.com/v4/letter/r/c89c15/32.png) [@Rakesh\_Shiriyara](https://discuss.elastic.co/u/Rakesh_Shiriyara)\
**Post date:** [June 29, 2018, 10:15am UTC](https://discuss.elastic.co/t/wildcard-searches/137594/5 "2018-06-29T10:15:40Z")

</div>

I found the solution..i was using **ngram** as filter instead of **tokeniser**. Below code snippets worked.

Created a index with below metadata,

> ```
> curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
> {
> "settings": {
> "analysis": {
> "analyzer": {
> "autocomplete": {
> "tokenizer": "autocomplete",
> "filter": [
> "lowercase"
> ]
> },
> "autocomplete_search": {
> "tokenizer": "lowercase"
> }
> },
> "tokenizer": {
> "autocomplete": {
> "type": "ngram",
> "min_gram": 1,
> "max_gram": 10,
> "token_chars": [
> "letter",
> "digit",
> "punctuation",
> "symbol"
> ]
> }
> }
> }
> },
> "mappings": {
> "_doc": {
> "properties": {
> "title": {
> "type": "text",
> "analyzer": "autocomplete",
> "search_analyzer": "autocomplete_search"
> }
> }
> }
> }
> } 
> 
> ```

Inserted 2 records...

> ```
> curl -X PUT "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
> {
> "title": "Quick Foxes" 
> }
> 'curl -X PUT "localhost:9200/my_index/_doc/2" -H 'Content-Type: application/json' -d'
> {
> "title": "Quick Tiger" 
> }
> '
> 
> ```

Now if i search for "Quick Fo" with below query, it gives me only \_doc 1, as it is matching both "Quick" and "Fo".

> ```
> curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
> {
> "query": {
> "match": {
> "title": {
> "query": "Quick Fo", 
> "operator": "and"
> }
> }
> }
> }
> '
> 
> ```

This is what i wanted. But with ngram as filter it was giving result even if just "Quick" matches.

---

<div class="post-metadata">

**Author:** ![Rakesh\_Shiriyara](https://avatars.discourse-cdn.com/v4/letter/r/c89c15/32.png) [@Rakesh\_Shiriyara](https://discuss.elastic.co/u/Rakesh_Shiriyara)\
**Post date:** [July 3, 2018, 10:37am UTC](https://discuss.elastic.co/t/wildcard-searches/137594/6 "2018-07-03T10:37:44Z")

</div>

Looks like its not working in all cases.

For eg:- I inserted 2 new records ("SP SSPT", "PSS SSPT2"). When i search for "SP SSPT" according to expectation, only SP SSPT should come as result. But i get both SP SSPT and PSS SSPT2.

This is because ngram tokens for "PSS SSPT2" contain both SP & SSPT.

Can you let me know how to handle these scanarios?

---

<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 31, 2018, 10:38am UTC](https://discuss.elastic.co/t/wildcard-searches/137594/7 "2018-07-31T10:38:01Z")

</div>

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