elasticsearch - Why script in custom_filters_score behaves as boost? -
{ "query": { "custom_filters_score": { "query": { "term": { "name": "user1234" } }, "filters": [ { "filter": { "term": { "subject": "math" } }, "script": "_score + doc['subject_score'].value" } ] } } }
if script having above gives error: unresolvable property or identifier: _score
if script "script": "doc['subject_score'].value"
multiplies _score in similar way boost does. want replace elasticsearch _score custom score.
if understood correctly use elasticsearch scoring if subject not math , use custom scoring subject math. if using elasticsearch v0.90.4 or higher, can achieved using new function_score
query:
{ "query": { "function_score": { "query": { "term": { "name": "user1234" } }, "functions": [{ "filter": { "term": { "subject": "math" } }, "script_score": { "script": "doc[\"subject_score\"].value" } }, { "boost_factor": 0 }], "score_mode": "first", "boost_mode": "sum" } } }
prior v0.90.4 have resort using combination of custom_score
, custom_filters_score
:
{ "query": { "custom_score": { "query": { "custom_filters_score": { "query": { "term": { "name": "user1234" } }, "filters": [{ "filter": { "term": { "subject": "math" } }, "script": "-1.0" }] } }, "script": "_score < 0.0 ? _score * -1.0 + doc[\"subject_score\"].value : _score" } } }
or @javanna suggested, use multiple custom_score queries combined bool query:
{ "query": { "bool": { "disable_coord": true, "should": [{ "filtered": { "query": { "term": { "name": "user1234" } }, "filter": { "bool": { "must_not": [{ "term": { "subject": "math" } }] } } } }, { "filtered": { "query": { "custom_score": { "query": { "term": { "name": "user1234" } }, "script": "doc['subject_score'].value" } }, "filter": { "term": { "subject": "math" } } } }] } } }
Comments
Post a Comment