-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct boost in script_score query and error on negative scores #52478
Correct boost in script_score query and error on negative scores #52478
Conversation
Before boost in script_score query was wrongly applied only to the subquery. This commit makes sure that the boost is applied to the whole score that comes out of script. Closes elastic#48465
Pinging @elastic/es-search (:Search/Search) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I'm completely inexperienced in the area but I left some comments, especially the one regarding explanation.
@@ -48,9 +48,12 @@ scores be positive or `0`. | |||
-- | |||
|
|||
`min_score`:: | |||
(Optional, float) Documents with a <<relevance-scores,relevance score>> lower | |||
(Optional, float) Documents with a score lower |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you remove the reference to the score docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we were referencing relevance scores, while I think the goal of script_score query is to calculate custom scores through some scripts, not a traditional textual relevance score.
@@ -143,7 +148,12 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio | |||
explanation = Explanation.match(score, desc); | |||
} | |||
} | |||
|
|||
if (boost != 1.0f) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be executed also when explanation == null
? Or I'm missing something?
Maybe it worths checking for a test that when the boost is != 1
and explanation is false there is no explanation returned regarding the boost?
Also, why not return an explanation if the boost is 1
, since it's asked by the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way we handle explanation
object is different from other queries, which may be confusing. In script_score
query a user can provide his/her custom explanation.
This seems to be executed also when explanation == null?
Not exactly. We start with explanation
that is user provided explanation for the script, and if it null, it is substituted by our standard explanation (line 143), so on in line 151 explanation is never null.
Maybe it worths checking for a test that when the boost is != 1 and explanation is false there is no explanation returned regarding the boost?
When is explanation is not asked (explain = false
in a search request), we will not even go to this method. This method is executed only when a user requests an explanation.
Also, why not return an explanation if the boost is 1, since it's asked by the user?
I was thinking since boost
is an optional parameter, when it is not provided by a user, there is no need to provide an explanation about it.
query: {match_all: {boost: 10}} | ||
script: | ||
source: "doc['i'].value * _score" | ||
boost: 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use a different score here, e.g.: 5
to make it more clear.
@matriv Thanks for the review. I have tried to address your comments, please continue to review when you have time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks a lot for responding to my questions and providing some more context!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a minor suggestion, otherwise LGTM.
this.explanation = explanation; | ||
} | ||
|
||
@Override | ||
public float score() throws IOException { | ||
int docId = docID(); | ||
scoreScript.setDocument(docId); | ||
float score = (float) scoreScript.execute(explanation); | ||
float score = (float) scoreScript.execute(explanation) * boost; | ||
if (score == Float.NEGATIVE_INFINITY || Float.isNaN(score)) { | ||
throw new ElasticsearchException( | ||
"script_score query returned an invalid score [" + score + "] for doc [" + docId + "]."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better if this error message returned the value produced by the script without the boost.
subs.addAll(Arrays.asList(explanation.getDetails())); | ||
subs.add(Explanation.match(boost, "boost")); | ||
explanation = Explanation.match(explanation.getValue(), explanation.getDescription(), subs); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest wrapping the explanation instead of modifying it in-place: create the scorer with boost=1f a couple lines above, and then here:
if (boost != 1f) {
explanation = Explanation.match(boost * explanation.getValue().floatValue(), "Boosted score, product of:",
Explanation.match(boost, "boost"),
explanation);
}
@jpountz Thanks for the feedback, I have addressed it. |
Before boost in script_score query was wrongly applied only to the subquery. This commit makes sure that the boost is applied to the whole score that comes out of script. Closes elastic#48465
if (score == Float.NEGATIVE_INFINITY || Float.isNaN(score)) { | ||
throw new ElasticsearchException( | ||
"script_score query returned an invalid score [" + score + "] for doc [" + docId + "]."); | ||
if (score < 0f || Float.isNaN(score)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mayya-sharipova it looks like this PR also fixed a bug in script_score
queries where we allowed negative scores. I think we should add a note to the breaking changes docs and also update the PR description to make it clear we included this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jtibshirani Thanks. Even before without this change a user would get an error if their script_score
query produced a negative score. They would just get it from a different place, one of them from the Lucene here
So the only thing changed from a user perspective is an error message and error status code (before was 500, not 400x). Do you think it warrants a breaking change notice?
+1 for include this in the PR description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this out using an Elasticsearch 7.6 build, and didn't receive an error:
PUT my_index/_doc/1?refresh
{
"field": "value"
}
GET my_index/_search
{
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "-1000"
}
}
}
}
The line you linked to is an assert, so perhaps these Lucene checks didn't always catch the issue in non-test environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jtibshirani Thanks for uncovering this. I understood what happened:
- Before 7.5,
script_score
query was using ScriptScoreFunction that was returning 400 error with a negative score. - From 7.5, we have changed it to not use
ScriptScoreFunction
but forgot to add a condition for a negative score. But TopScoreDocCollector assertion is tripped, causing fatal error in the dev mode. But I guess we silence these assertions in a production mode as we don't see any visible errors or error log messages.
So, negative scores were wrongly allowed only in 7.5-7.6 versions, so to me it doesn't look like a really breaking change. But I think it is still worth to add a note with explanation in release notes. I will do that. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding an explanation to the release notes makes sense to me. I agree it shouldn't be presented as a typical 'breaking change', it is more like a regression that we fixed. Perhaps we could add a unit test along with the release notes update, to prevent a future regression?
7.5 and 7.6 had a regression that allowed for script_score queries to have negative scores. We have corrected this regression in elastic#52478. This is an addition to elastic#52478 that adds a test and release notes.
Before boost in script_score query was wrongly applied only to the subquery.
This commit makes sure that the boost is applied to the whole score
that comes out of script.
Also provide error 400x error message on negative scores in script_score.
Closes #48465