# How to apply Function Score in elastic search and remove old score

**URL:** https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429
**Category:** Elasticsearch
**Created:** [December 8, 2016, 11:48am UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429 "2016-12-08T11:48:09Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Magapu\_Tatarao](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Magapu\_Tatarao](https://discuss.elastic.co/u/Magapu_Tatarao)
#### Post date: [December 8, 2016, 11:48am UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/1 "2016-12-08T11:48:09Z")

</div>

{  
"size": 40,  
"query": {  
"function\_score": {

```
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "in_search": 1
          }
        },
        {
          "match": {
            "prod_id_active": 1
          }
        },
        {
          "match": {
            "search_title": "apple iphone 6s"
            
          }
        }
      ]
    }
  },
  "functions": [
    {
      "filter": {
        "match": {
          "search_title": "apple iphone 6s"
        }
      },
      "script_score": {
        "params": {
          "possibility_rank": 7,
          "max_cat_rank": 5000,
          "max_prod_rank": 10000,
          "_score" : 0
        },
        "script": "_score + possibility_rank * (max_cat_rank + max_prod_rank)-(doc['category_rank'].value + doc['product_rank'].value)"
      }
    },
    {
      "filter": {
        "match": {
          "search_title": "apple iphone"
        }
      },
      "script_score": {
        "params": {
          "possibility_rank": 6,
          "max_cat_rank": 5000,
          "max_prod_rank": 10000,
           "_score" : 0
        },
        "script": "_score + possibility_rank * (max_cat_rank + max_prod_rank)-(doc['category_rank'].value + doc['product_rank'].value)"
      }
    },
    {
      "filter": {
        "match": {
          "search_title": "apple 6s"
        }
      },
      "script_score": {
        "params": {
          "possibility_rank": 5,
          "max_cat_rank": 5000,
          "max_prod_rank": 10000,
           "_score" : 0
        },
        "script": "_score + possibility_rank * (max_cat_rank + max_prod_rank)-(doc['category_rank'].value + doc['product_rank'].value)"
      }
    },
    {
      "filter": {
        "match": {
          "search_title": "iphone 6s"
        }
      },
      "script_score": {
        "params": {
          "possibility_rank": 4,
          "max_cat_rank": 5000,
          "max_prod_rank": 10000,
           "_score" : 0
        },
        "script": " _score + possibility_rank * (max_cat_rank + max_prod_rank)-(doc['category_rank'].value + doc['product_rank'].value)"
      }
    },
    {
      "filter": {
        "match": {
          "search_title": "apple"
        }
      },
      "script_score": {
        "params": {
          "possibility_rank": 3,
          "max_cat_rank": 5000,
          "max_prod_rank": 10000,
           "_score" : 0
        },
        "script": "_score + possibility_rank * (max_cat_rank + max_prod_rank)-(doc['category_rank'].value + doc['product_rank'].value)"
      }
    },
    {
      "filter": {
        "match": {
          "search_title": "iphone"
        }
      },
      "script_score": {
        "params": {
          "possibility_rank": 2,
          "max_cat_rank": 5000,
          "max_prod_rank": 10000,
           "_score" : 0
        },
        "script": " _score + possibility_rank * (max_cat_rank + max_prod_rank)-(doc['category_rank'].value + doc['product_rank'].value)"
      }
    },
    {
      "filter": {
        "match": {
          "search_title": "6s"
        }
      },
      "script_score": {
        "params": {
          "possibility_rank": 1,
          "max_cat_rank": 5000,
          "max_prod_rank": 10000,
           "_score" : 0
        },
        "script": " _score + possibility_rank * (max_cat_rank + max_prod_rank)-(doc['category_rank'].value + doc['product_rank'].value)"
      }
    }
  ],
  "score_mode": "sum"
}

```

}  
}

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [December 8, 2016, 11:58am UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/2 "2016-12-08T11:58:22Z")

</div>

It seems like you are looking for the `"boost_mode": "replace"` option which will replace the score calculated from Lucene with the score you calculate in your function score. see [https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-function-score-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-function-score-query.html) for more details

---

<div class="post-metadata">

### Author: ![Magapu\_Tatarao](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Magapu\_Tatarao](https://discuss.elastic.co/u/Magapu_Tatarao)
#### Post date: [December 8, 2016, 12:05pm UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/3 "2016-12-08T12:05:22Z")

</div>

@colings86

thanks for your reply

I am using "function\_score" here with my custom formula

it is working fine but , the score is applying with the all scores of that particular document

so before that i want to make my doc score to 0 and apply the score calculated by my function

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [December 8, 2016, 12:11pm UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/4 "2016-12-08T12:11:22Z")

</div>

As I said, it sounds like you just need to add `"boost_mode": "replace"` to your function\_score which will ignore the score that is calculated outside of the function score. If you are wanting only one of the functions in you function score to calculate the score then you will need to change the `score_mode` from `sum` to some other value e.g. `first`

---

<div class="post-metadata">

### Author: ![Magapu\_Tatarao](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Magapu\_Tatarao](https://discuss.elastic.co/u/Magapu_Tatarao)
#### Post date: [December 8, 2016, 12:18pm UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/5 "2016-12-08T12:18:20Z")

</div>

check with both of your suggestions

and checked with ? explain tag in kibana

the actual score generated by my function for document is 104878

but it is showing by adding all other scores of a document

it is showing  
"\_score" : 554572.06  
for "score\_mode": "first"  
and  
"\_score": 2216353,  
for "score\_mode": "sum"

please help where i am missing it

---

<div class="post-metadata">

### Author: ![Magapu\_Tatarao](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Magapu\_Tatarao](https://discuss.elastic.co/u/Magapu_Tatarao)
#### Post date: [December 8, 2016, 12:25pm UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/6 "2016-12-08T12:25:34Z")

</div>

@colings86

Wow Thanks for your help ,

I have applied both statements given by u collectively

and achieved what i need

now the scoring function getting the result  
\_score = 104972

and score also getting same \_score = 104972

Thanks!

---

<div class="post-metadata">

### Author: ![Magapu\_Tatarao](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Magapu\_Tatarao](https://discuss.elastic.co/u/Magapu_Tatarao)
#### Post date: [December 8, 2016, 12:42pm UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/7 "2016-12-08T12:42:27Z")

</div>

@colings86

small isssue is there,

it changed the order of results

---

<div class="post-metadata">

### Author: ![Magapu\_Tatarao](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Magapu\_Tatarao](https://discuss.elastic.co/u/Magapu_Tatarao)
#### Post date: [December 8, 2016, 12:50pm UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/8 "2016-12-08T12:50:49Z")

</div>

all are taking the first function for score allocation all are taking values from first function all are taking possibility rank "possibility\_rank": 7 for calculating score

"functions": [  
{  
"filter": {  
"match": {  
"search\_title": "apple iphone 6s"  
}  
},  
"script\_score": {  
"params": {  
"possibility\_rank": 7,  
"max\_cat\_rank": 5000,  
"max\_prod\_rank": 10000,  
"\_score" : 0  
},  
"script": "\_score + possibility\_rank \* (max\_cat\_rank + max\_prod\_rank)-(doc['category\_rank'].value + doc['product\_rank'].value)"  
}  
},  
{  
"filter": {  
"match": {  
"search\_title": "apple iphone"  
}  
},  
"script\_score": {  
"params": {  
"possibility\_rank": 6,  
"max\_cat\_rank": 5000,  
"max\_prod\_rank": 10000,  
"\_score" : 0  
},  
"script": "\_score + possibility\_rank \* (max\_cat\_rank + max\_prod\_rank)-(doc['category\_rank'].value + doc['product\_rank'].value)"  
}  
},  
{  
"filter": {  
"match": {  
"search\_title": "apple 6s"  
}  
},  
"script\_score": {  
"params": {  
"possibility\_rank": 5,  
"max\_cat\_rank": 5000,  
"max\_prod\_rank": 10000,  
"\_score" : 0  
},  
"script": "\_score + possibility\_rank \* (max\_cat\_rank + max\_prod\_rank)-(doc['category\_rank'].value + doc['product\_rank'].value)"  
}  
}

---

<div class="post-metadata">

### Author: ![Magapu\_Tatarao](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Magapu\_Tatarao](https://discuss.elastic.co/u/Magapu_Tatarao)
#### Post date: [December 15, 2016, 5:53am UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/9 "2016-12-15T05:53:03Z")

</div>

Hi i was stuck out there !!. replacing the document relevant score with the score calculated by function,  
can any one have suggestions on this please let me know.

Thanks in advance

---

<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: [January 12, 2017, 5:53am UTC](https://discuss.elastic.co/t/how-to-apply-function-score-in-elastic-search-and-remove-old-score/68429/10 "2017-01-12T05:53:08Z")

</div>

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