Skip to content

Commit

Permalink
fix explain bug in multifunctionScorequery (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
waziqi89 authored Jun 2, 2023
1 parent 3af4248 commit 2d2ebbb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sourceCompatibility = 1.14
targetCompatibility = 1.14

allprojects {
version = '0.24.0'
version = '0.24.1'
group = 'com.yelp.nrtsearch'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class MultiFunctionScoreQuery extends Query {
private final float minScore;
private final boolean minExcluded;

private static boolean isFilteredByMinScore(
private static boolean hasPassedMinScore(
float currentScore, float minimalScore, boolean minimalExcluded) {
if (currentScore > minimalScore) {
return true;
Expand Down Expand Up @@ -248,7 +248,7 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio
expl = explainBoost(expl, factorExplanation);
}
float curScore = expl.getValue().floatValue();
if (isFilteredByMinScore(curScore, minScore, minExcluded)) {
if (!hasPassedMinScore(curScore, minScore, minExcluded)) {
expl =
Explanation.noMatch(
"Score value is too low, expected at least "
Expand Down Expand Up @@ -372,7 +372,7 @@ public boolean matches() throws IOException {
// we need to check the two-phase iterator first
// otherwise calling score() is illegal
curScore = in.score();
return isFilteredByMinScore(curScore, minScore, minExcluded);
return hasPassedMinScore(curScore, minScore, minExcluded);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,69 @@ public void testSingle_ScriptWith_0_Score_excluded() {
verifyResponseHits(response, List.of(), List.of());
}

@Test
public void test_noFunctions() {
SearchResponse response =
getGrpcServer()
.getBlockingStub()
.search(
SearchRequest.newBuilder()
.setIndexName(DEFAULT_TEST_INDEX)
.setStartHit(0)
.setTopHits(10)
.addRetrieveFields("doc_id")
.addRetrieveFields("text_field")
.setExplain(true)
.setQuery(
Query.newBuilder()
.setMultiFunctionScoreQuery(
MultiFunctionScoreQuery.newBuilder()
.setQuery(
Query.newBuilder()
.setMatchQuery(
MatchQuery.newBuilder()
.setField("text_field")
.setQuery("term1")
.build())
.build())
.build())
.build())
.build());
verifyResponseHits(response, List.of(2, 4), List.of(0.33812057971954346, 0.27725890278816223));
}

@Test
public void test_noFunctions_withMinScore() {
SearchResponse response =
getGrpcServer()
.getBlockingStub()
.search(
SearchRequest.newBuilder()
.setIndexName(DEFAULT_TEST_INDEX)
.setStartHit(0)
.setTopHits(10)
.addRetrieveFields("doc_id")
.addRetrieveFields("text_field")
.setExplain(true)
.setQuery(
Query.newBuilder()
.setMultiFunctionScoreQuery(
MultiFunctionScoreQuery.newBuilder()
.setQuery(
Query.newBuilder()
.setMatchQuery(
MatchQuery.newBuilder()
.setField("text_field")
.setQuery("term1")
.build())
.build())
.setMinScore(0.3f)
.build())
.build())
.build());
verifyResponseHits(response, List.of(2), List.of(0.33812057971954346));
}

private void multiFunctionAndVerify(
Query innerQuery,
FunctionScoreMode scoreMode,
Expand Down

0 comments on commit 2d2ebbb

Please sign in to comment.