I know booleanQuery will add score of should clauses and must clauses. I used to do log(scoreOfShould) + log(scoreOfMust) + log(_score) for sorting as scoreOfShould * scoreOfMust * _score.
In my case, scoreOfShould is querying nested doc, and the final score sometimes less than 1 which leads log(scoreOfShould) to be negative.
My query object looks like this. The outer object has titile, description, outerRatio and logOuterRatio. The nested object has keyword, innerRatio and logInnerRatio. What I want is sorting by outerRatio * innerRatio * _score. So I get _score from the multiMatch, then use script_score to calulate logOutRatio + log(_score), then add the max log(innerRatio) of hit child docs. Sometimes, log(innerRatio) is negative to weaken the must result.
But negative score is not supported in 7.x, any one can help on this?
{
"from": 0,
"size": 20,
"query": {
"bool": {
"should": [
{
"nested": {
"path": "innerObject",
"score_mode": "max",
"query": {
"function_score": {
"functions": [
{
"field_value_factor": {
"field": "innerObject.logInnerRatio"
}
}
],
"query": {
"bool": {
"filter": {
"terms": {
"innerObject.keyword": [
"led",
"logo"
]
}
}
}
},
"boost_mode": "replace"
}
}
}
}
],
"must": [
{
"function_score": {
"functions": [
{
"script_score": {
"script": {
"source": "Math.log(_score) + doc['logOuterRatio'].value"
}
}
}
],
"query": {
"bool": {
"must": {
"multi_match": {
"query": "led logo",
"type": "cross_fields",
"fields": [
"title^2.0",
"description^0.5"
]
}
}
}
},
"boost_mode": "replace"
}
}
]
}
},
"_source": [
"id",
"uid",
"title"
],
"sort": [
{
"_score": {
"order": "desc"
}
}
]
}