From 2d2ebbb3eee0c7066af0fdaf1eed5ad62722dc0c Mon Sep 17 00:00:00 2001 From: waziqi89 <89210409+waziqi89@users.noreply.github.com> Date: Fri, 2 Jun 2023 16:52:15 -0400 Subject: [PATCH] fix explain bug in multifunctionScorequery (#579) --- build.gradle | 2 +- .../MultiFunctionScoreQuery.java | 6 +- .../MultiFunctionScoreQueryTest.java | 63 +++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 903ce600f..e5784c0f0 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ sourceCompatibility = 1.14 targetCompatibility = 1.14 allprojects { - version = '0.24.0' + version = '0.24.1' group = 'com.yelp.nrtsearch' } diff --git a/src/main/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQuery.java b/src/main/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQuery.java index dfd2d36b2..c8d76e88d 100644 --- a/src/main/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQuery.java +++ b/src/main/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQuery.java @@ -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; @@ -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 " @@ -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 diff --git a/src/test/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQueryTest.java b/src/test/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQueryTest.java index 2e178af6a..6190d793f 100644 --- a/src/test/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQueryTest.java +++ b/src/test/java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/MultiFunctionScoreQueryTest.java @@ -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,