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

{
"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"
}

}
}

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 for more details

@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

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

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

@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!

@colings86

small isssue is there,

it changed the order of results

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)"
}
}

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

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