# Search-as-you-type Optimization: How to boost the First N-Gram for More Accurate Results

**URL:** <https://discuss.elastic.co/t/search-as-you-type-optimization-how-to-boost-the-first-n-gram-for-more-accurate-results/375264>\
**Category:** Elasticsearch\
**Created:** [March 1, 2025, 11:39am UTC](https://discuss.elastic.co/t/search-as-you-type-optimization-how-to-boost-the-first-n-gram-for-more-accurate-results/375264 "2025-03-01T11:39:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crout](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crout/32/141734_2.png) [@Crout](https://discuss.elastic.co/u/Crout)\
**Post date:** [March 1, 2025, 11:39am UTC](https://discuss.elastic.co/t/search-as-you-type-optimization-how-to-boost-the-first-n-gram-for-more-accurate-results/375264/1 "2025-03-01T11:39:27Z")

</div>

Hello everyone, I have been trying to improve my search for a while now  
I am searching for names with the help of search\_as\_you\_type and a mixture of match\_phrase and the math\_phrase\_prefix. the search itself works wonderfully. However, I still have a problem:

If the search is not yet long, and I search e.g. with `Da`, the results are ranked as follows:

> Dave Peter  
> Peter Dave

But of course I would like the weighting of the first ngram to be much higher. I have tried to give the first ngram a higher boost with a span\_first, unfortunately without success. Does anyone have any tips on the best way to do this? Thanks for your help!

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "span_first": {
            "match": {
              "span_term": {
                "name.name": "searchTerm"
              }
            },
            "end": 1,
            "boost": 5
          }
        },
        {
          "match_phrase": {
            "name.name": {
              "query": "searchTerm",
              "boost": 10
            }
          }
        },
        {
          "match_phrase_prefix": {
            "name.name": {
              "query": "searchTerm",
              "boost": 7
            }
          }
        },
        {
          "multi_match": {
            "query": "searchTerm",
            "type": "bool_prefix",
            "fields": [
              "name.name",
              "name.name._2gram",
              "name.name._3gram"
            ],
            "operator": "and",
            "minimum_should_match": "100%"
          }
        },
        {
          "multi_match": {
            "query": "searchTerm",
            "fields": [
              "name.name",
              "name.name._2gram",
              "name.name._3gram"
            ],
            "fuzziness": 2,
            "operator": "and",
            "minimum_should_match": "100%",
            "boost": 0.5
          }
        }
      ]
    }
  }
}

```
