Maximum score of a field in script score - ElasticSearch

I'm using ElasticSearch 2.4.0 . Using the script_score, I'm calculating custom score like

'{"script":"return ( ((doc.field_a.value)* 0.5) + (_score * 0.5) );"}'

I want something like (some normalisation effect on the field_a value):

'max_score = doc.field_a.values.max(); return ( ((doc.field_a.value)* 0.5)/max_score + (_score * 0.5) );'

Any help appreciated.

Hey,

I could not extract from this post what your actual problem is. Can you extend on that? Is it the score calculation or the results? Can you also take your time and create a reproducible example?

--Alex

Thank you for your reply :slight_smile:

I want to mix up the default '_score' with a field value in the doc(by some weights). I want to normalise the field value effect by finding maximum of field value in the filtered result.

curl localhost:9200/index_name/type/_search -d'{"_source":false,"query":{"function_score":{"query":{"filtered":{"filter":{"bool":{"must":[{"bool":{"should":[{"terms":{"ids":["6","15","5"]}}]}}},{"range":{"count_on_hand":{"gte":1}}}],"must_not":[],"should":[]}}}},"script_score":{"script":"max_score=doc.field_a.values.max();return (((doc.field_a.value) 0.5)/max_score + (_score * 0.5));"},"boost_mode":"replace"}},"from":0,"size":12}'*

For eg: The field_a contains value from 1 to 100000. After the filters, if the max_score of the field is 5000, then
by the above formula, the document contains the max_score & high relevancy will be on the top. and followed by the rest of the documents.

1 Like

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