# Match on Ngram field where the words are in order without scripting

**URL:** <https://discuss.elastic.co/t/match-on-ngram-field-where-the-words-are-in-order-without-scripting/281518>\
**Category:** Elasticsearch\
**Created:** [August 16, 2021, 8:37am UTC](https://discuss.elastic.co/t/match-on-ngram-field-where-the-words-are-in-order-without-scripting/281518 "2021-08-16T08:37:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron\_lusha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ron_lusha/32/87691_2.png) [@Ron\_lusha](https://discuss.elastic.co/u/Ron_lusha)\
**Post date:** [August 16, 2021, 8:37am UTC](https://discuss.elastic.co/t/match-on-ngram-field-where-the-words-are-in-order-without-scripting/281518/1 "2021-08-16T08:37:37Z")

</div>

Hello, I have a question regarding auto-complete using Ngram search, Im using elasticsearch 7.3, so the new "wildcard" field type is not relevant for me

This is my index:

```auto
PUT ngram_test
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "edgengram": {
            "type": "text",
            "analyzer": "edge_ngram_analyzer",
            "search_analyzer": "main_analyzer"
          },
          "keyword": {
            "type": "keyword",
            "normalizer": "lower_keyword"
          }
        },
        "analyzer": "main_analyzer"
      }
    }
  },
  "settings": {
    "index": {
      "number_of_shards": "1",
      "analysis": {
        "normalizer": {
          "lower_keyword": {
            "filter": "lowercase",
            "type": "custom"
          }
        },
        "analyzer": {
          "edge_ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "edge_ngram_tokenizer"
          },
          "main_analyzer": {
            "filter": "lowercase",
            "tokenizer": "standard"
          }
        },
        "tokenizer": {
          "edge_ngram_tokenizer": {
            "token_chars": [
              "letter",
              "digit"
            ],
            "min_gram": "1",
            "type": "edge_ngram",
            "max_gram": "15"
          }
        }
      },
      "number_of_replicas": "0"
    }
  }
}

```

And docs:

```auto
POST ngram_test/_doc/1
{
  "name": "Burger King"
}

POST ngram_test/_doc/2
{
  "name": "Burger King Japan"
}

POST ngram_test/_doc/3
{
  "name": "Great Burger of the Kings"
}

POST ngram_test/_doc/4
{
  "name": "King of Burger"
}

POST ngram_test/_doc/5
{
  "name": "Kind Burgers"
}

```

My target is for the user to input "Burger Ki" and only get the first three docs  
So far I'm doing it this way and it works, but I was wondering if there is a way to do it without using scripting as it can be slow if I'm not mistaken

```auto
GET ngram_test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name.edgengram": {
              "query": "Burger Ki",
              "operator": "and"
            }
          }
        }
      ],
      "filter": {
        "script": {
          "script": {
            "params": {
              "input": ["burger", "ki"]
            },
            "source": """
            int pos = -1;
            String name = doc['name.keyword'].value;
            for (word in params.input) {
              int current_pos = name.indexOf(word);
              if (current_pos > pos) {
                pos = current_pos;
              }
              else {
                return false;
              }
            }
            return true;
            """
          }
        }
      }
    }
  }
}

```

^^ the script also doesn't take into account cases where the same ngram will appear twice in two different indexes, so that's something I'll need to solve too

Thanks!

---

<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 13, 2021, 8:38am UTC](https://discuss.elastic.co/t/match-on-ngram-field-where-the-words-are-in-order-without-scripting/281518/2 "2021-09-13T08:38:36Z")

</div>

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