# Elastic Function\_Score not applying properly

**URL:** <https://discuss.elastic.co/t/elastic-function-score-not-applying-properly/170660>\
**Category:** Elasticsearch\
**Created:** [March 3, 2019, 10:01pm UTC](https://discuss.elastic.co/t/elastic-function-score-not-applying-properly/170660 "2019-03-03T22:01:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anveshdd](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@anveshdd](https://discuss.elastic.co/u/anveshdd)\
**Post date:** [March 3, 2019, 10:01pm UTC](https://discuss.elastic.co/t/elastic-function-score-not-applying-properly/170660/1 "2019-03-03T22:01:53Z")

</div>

Working on elastic search 6.5.3. I am using `function_score` to change the weights of the particular results. I am expecting the output as the results with the highest weight comes first and then remaining follows. Below is my Query

```
GET www-test-index/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "operator": "and",
          "query": "foo",
          "fields": [
            "title^10",
            "content^5"
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "match": {
              "url": "https://www-somesite/sports/first"
            }
          },
           
          "weight": 50
        },
        {
          "filter": {
            "match": {
              "url": "https://www-somesite/sports/second"
            }
          },
        
          "weight": 49
        }
      ],
      "max_boost": 42,
      "score_mode": "sum",
      "boost_mode": "multiply",
      "min_score": 42
    }
  }
}

```

When I run the above query the result with the `https://www-somesite/sports/second` is appearing as first place followed by `https://www-somesite/sports/first`. Little confusion how I can get `https://www-somesite/sports/first` in the first place.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [March 5, 2019, 10:15am UTC](https://discuss.elastic.co/t/elastic-function-score-not-applying-properly/170660/2 "2019-03-05T10:15:11Z")

</div>

There's a few things that could be going on:

- The `max_boost` of 42 is lower than the actual weights of the functions. As a result, both URLs will get the same boost.
- Is `url` a `text` field or a `keyword` field? If it is a `text` field, both URLs will actually match any of your function queries. If so, you may want to change `url` into `url.keyword` in your function queries.
- How many shards does your index have? With more than one shard, while testing on small datasets, you may get unexpected scoring results. Try creating the `www-test-index` index with one shard.

---

<div class="post-metadata">

**Author:** ![anveshdd](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@anveshdd](https://discuss.elastic.co/u/anveshdd)\
**Post date:** [March 5, 2019, 2:33pm UTC](https://discuss.elastic.co/t/elastic-function-score-not-applying-properly/170660/3 "2019-03-05T14:33:36Z")

</div>

- Can I increase the Boost value to max number?

- Url is text field in my case

- Well I am using 4 shards and my index managing 10M docs.

Here is my config.

```auto
{
   "settings":{
      "index":{
         "number_of_shards":4,
         "number_of_replicas":0,
         "refresh_interval":"500ms",
         "analysis":{
            "analyzer":{
               "my_analyzer":{
                  "tokenizer":"whitespace",
                  "filter":[
                     "lowercase",
                     "my_snow",
                     "my_stemmer",
                     "my_synonym",
                     "my_stop"
                  ]
               }
            },
            "filter":{
               "my_stop":{
                  "type":"stop",
                  "stopwords_path":"stopwords/stopwords_en.txt"
               },
               "my_snow":{
                  "type":"snowball",
                  "language":"English"
               },
               "my_synonym":{
                  "type":"synonym_graph",
                  "expand":false,
                  "synonyms_path":"analysis/synonym.txt"
               },
               "my_stemmer":{
                  "type":"stemmer",
                  "name":"english"
               }
            }
         }
      }
   },
   "mappings":{
      "doc":{
         "_source":{
            "enabled":true
         },
         "properties":{
            "title":{
               "type":"text",
               "index":"true",
               "store":true,
               "analyzer":"my_analyzer"
            },
            "content":{
               "type":"text",
               "index":"true",
               "store":true,
               "analyzer":"my_analyzer"
            },
            "url":{
               "type":"text",
               "index":"true",
               "store":true
            },
            "host":{
               "type":"keyword",
               "index":"true",
               "store":true
            }            
         }
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [March 5, 2019, 3:21pm UTC](https://discuss.elastic.co/t/elastic-function-score-not-applying-properly/170660/4 "2019-03-05T15:21:31Z")

</div>

- Yeah, in your case, if you want to keep the `max_boost`, you'd need to set it to at least the highest weight (50).
- Does it make sense, for the Url to be a `text` field? Typically, fields like Url are only used for exact searches, not for full-text queries. Often, it makes most sense to map a field like Url as type `keyword` instead.  
As a work-around you could change the `match` queries in your functions into `match_phrase` queries.
- If you're working with millions of documents then you can forget what I said about shards. I wanted to make sure you were not testing against 2 documents in an index with the default of 5 shards.

---

<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:** [April 2, 2019, 3:23pm UTC](https://discuss.elastic.co/t/elastic-function-score-not-applying-properly/170660/5 "2019-04-02T15:23:01Z")

</div>

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