# Unexpected hits when using n-gram

**URL:** <https://discuss.elastic.co/t/unexpected-hits-when-using-n-gram/286760>\
**Category:** Elasticsearch\
**Created:** [October 14, 2021, 4:45pm UTC](https://discuss.elastic.co/t/unexpected-hits-when-using-n-gram/286760 "2021-10-14T16:45:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsharma](https://avatars.discourse-cdn.com/v4/letter/b/4bbf92/32.png) [@bsharma](https://discuss.elastic.co/u/bsharma)\
**Post date:** [October 14, 2021, 4:45pm UTC](https://discuss.elastic.co/t/unexpected-hits-when-using-n-gram/286760/1 "2021-10-14T16:45:40Z")

</div>

Hi, I am using n-gram filter with my index.

Index configuration:

```auto
PUT dev-dk-ngram-test
{
  "settings": {
    "index": {
      "max_ngram_diff": 2
    },
    "analysis": {
      "analyzer": {
        "default": {
          "tokenizer": "whitespace",
          "filter": ["lowercase", "custom_grams"]
        }, 
        "default_search": {
          "tokenizer": "whitespace",
          "filter": ["lowercase", "custom_grams"]
        }
      },
      "filter": {
        "custom_grams": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 5
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "dk-ngram-test": {
        "type": "nested", 
        "include_in_parent": true, 
        "properties": {
          "name": { 
            "type": "text"
          },
          "desc": { 
            "type": "text"
          },
          "age": { "type": "integer" }
        }
      }
    }
  }
}

```

Them I am adding few records:

```auto
PUT dev-dk-ngram-test/_doc/8?refresh
{
  "dk-ngram-test": {
    "name": "Atlas",
    "desc": "Domain",
    "age": 1
  }
}

PUT dev-dk-ngram-test/_doc/9?refresh
{
  "dk-ngram-test": {
    "name": "Functional Classification",
    "desc": "Functional Classification",
    "age": 1
  }
}

```

Now I am searching "Atlas".

```auto
GET dev-dk-ngram-test/_search
{
  "query": {
    "query_string": {
      "query": "atlas",
      "escape": true
    }
  }
}

```

I expect the Document with ID 8 to be of higher sore, but instead the document with ID 9 has higher score.

To give more background, we started with default (standard) settings, but it didn't give us results which had a minor spelling mistakes. So we switched to n-gram. It works well for our most search use cases, but searches like above are causing issue. The term which match exactly are getting lower score at times. Could you please help.

---

<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:** [November 11, 2021, 4:46pm UTC](https://discuss.elastic.co/t/unexpected-hits-when-using-n-gram/286760/2 "2021-11-11T16:46:32Z")

</div>

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