# Using Exact Prefix/MatchPhrase Prefix Queries with Ngram Filter

**URL:** <https://discuss.elastic.co/t/using-exact-prefix-matchphrase-prefix-queries-with-ngram-filter/244207>\
**Category:** Elasticsearch\
**Created:** [August 7, 2020, 5:00pm UTC](https://discuss.elastic.co/t/using-exact-prefix-matchphrase-prefix-queries-with-ngram-filter/244207 "2020-08-07T17:00:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![k3np4ch1](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@k3np4ch1](https://discuss.elastic.co/u/k3np4ch1)\
**Post date:** [August 7, 2020, 5:00pm UTC](https://discuss.elastic.co/t/using-exact-prefix-matchphrase-prefix-queries-with-ngram-filter/244207/1 "2020-08-07T17:00:49Z")

</div>

Hi,  
My goal is to search query text having length one or two character long.  
This is my setting for the index.

```auto
"settings" : {
      "index" : {
        "number_of_shards" : "5",
        "provided_name" : "my_user",
        "analysis" : {
          "filter" : {
            "ngrammed" : {
              "type" : "ngram",
              "min_gram" : "3",
              "max_gram" : "50"
            }
          },
          "analyzer" : {
            "ngrammed_ci" : {
              "filter" : [
                "lowercase",
                "ngrammed"
              ],
              "type" : "custom",
              "tokenizer" : "standard"
            },
            "keyword_ci" : {
              "filter" : [
                "lowercase"
              ],
              "type" : "custom",
              "tokenizer" : "keyword"
            }
          }
        }
      }
    }

```

I have a set of users with a display name field with the following analyzers. Say if I have a couple of users with names like  
`Allen`, `Alec`, `Kimball`, `Polly`  
The problem I am facing is that when I search with a 2 character length query string like `al` along with `Allen` & `Alec`, it matches with `Kimball` as well since the ngram filter tokenizes `Kimball` as `all` in the inverted index. I am trying to avoid this scenario. Also wanted to know if there is anyway to implement this functionality without changing anythin on the Index side of things and make the changes only for query side.

```auto
"user_display_name" : {
  "type" : "text",
  "fields" : {
    "ci" : {
    "type" : "text",
    "analyzer" : "keyword_ci"
    }
  "cs" : {
    "type" : "keyword"
    }
  },
  "analyzer" : "ngrammed_ci",
  "search_analyzer" : "standard"
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 12, 2020, 9:55am UTC](https://discuss.elastic.co/t/using-exact-prefix-matchphrase-prefix-queries-with-ngram-filter/244207/2 "2020-08-12T09:55:55Z")

</div>

sounds like you are more interested in so called [edge\_ngrams](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenfilter.html) than ngrams.

That said, maybe you can also make use of the [search as you type datatype](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-as-you-type.html), which can probably save you some setup woes here and works out of the box.

---

<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:** [September 9, 2020, 9:56am UTC](https://discuss.elastic.co/t/using-exact-prefix-matchphrase-prefix-queries-with-ngram-filter/244207/3 "2020-09-09T09:56:14Z")

</div>

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