# Favour prefixes at query time for an analyzed index?

**URL:** <https://discuss.elastic.co/t/favour-prefixes-at-query-time-for-an-analyzed-index/306517>\
**Category:** Elasticsearch\
**Created:** [June 7, 2022, 2:43am UTC](https://discuss.elastic.co/t/favour-prefixes-at-query-time-for-an-analyzed-index/306517 "2022-06-07T02:43:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![corndog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/corndog/32/96803_2.png) [@corndog](https://discuss.elastic.co/u/corndog)\
**Post date:** [June 7, 2022, 2:43am UTC](https://discuss.elastic.co/t/favour-prefixes-at-query-time-for-an-analyzed-index/306517/1 "2022-06-07T02:43:54Z")

</div>

How can I get the desired output at query time for my edge-ngram autocomplete, given my mappings?

**Desired Output**

```auto
"Fish Lake, New Zealand"
"Fife Rock, New Zealand"
"Fish Rock, New Zealand"
>"Finger Rocks, New Zealand"<
>"Lake Fiddle, New Zealand"<

```

Actual Output

```auto
"Fish Lake, New Zealand"
"Fife Rock, New Zealand"
"Fish Rock, New Zealand"
>"Lake Fiddle, New Zealand"<
>"Finger Rocks, New Zealand"<

```

Search Query

```auto
POST /300_localities_nz/_search?pretty
{
  "_source": ["search_name"], 
  "size": 5, 
  "query": {
    "match": {
      "search_name.autocomplete": {
        "query": "Fi",
        "operator": "and",
        "fuzziness": "Auto"
      }
    }
  }
}

```

Settings

```auto
{
  "300_localities_nz" : {
    "settings" : {
      "index" : {
        "analysis" : {
          "filter" : {
            "english_stopwords" : {
              "type" : "stop",
              "stopwords" : "_english_"
            }
          },
          "analyzer" : {
            "autocomplete_index" : {
              "filter" : [
                "lowercase",
                "english_stopwords"
              ],
              "type" : "custom",
              "tokenizer" : "autocomplete_index"
            },
            "autocomplete_search" : {
              "tokenizer" : "lowercase"
            }
          },
          "tokenizer" : {
            "autocomplete_index" : {
              "token_chars" : [
                "digit",
                "letter"
              ],
              "min_gram" : "2",
              "type" : "edge_ngram",
              "max_gram" : "10"
            }
          }
        }
      }
    }
  }
}

```

Mappings

```auto
{
  "300_localities_nz" : {
    "mappings" : {
      "properties" : {
        "search_name" : {
          "type" : "text",
          "fields" : {
            "autocomplete" : {
              "type" : "text",
              "analyzer" : "autocomplete_index",
              "search_analyzer" : "autocomplete_search"
            },
            "exact" : {
              "type" : "keyword"
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 7, 2022, 5:00am UTC](https://discuss.elastic.co/t/favour-prefixes-at-query-time-for-an-analyzed-index/306517/2 "2022-06-07T05:00:43Z")

</div>

Hi!!

Maybe [Prefix Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html#query-dsl-prefix-query) resolve it but for solutions autocomplete [Completion suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html#completion-suggester) and the [`search_as_you_type`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html) are best solutions.

---

<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 5, 2022, 5:01am UTC](https://discuss.elastic.co/t/favour-prefixes-at-query-time-for-an-analyzed-index/306517/3 "2022-07-05T05:01:43Z")

</div>

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