You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ji...@apache.org on 2018/02/21 20:08:59 UTC

lucene-solr:branch_7x: LUCENE-8182: Fixed BoostingQuery to apply the context boost instead of the parent query boost

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x c189c094f -> 6d712c5e4


LUCENE-8182: Fixed BoostingQuery to apply the context boost instead of the parent query boost


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/6d712c5e
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/6d712c5e
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/6d712c5e

Branch: refs/heads/branch_7x
Commit: 6d712c5e4b2fc8f4ac6dfec2eac4a17386c978f0
Parents: c189c09
Author: Jim Ferenczi <ji...@apache.org>
Authored: Wed Feb 21 21:08:46 2018 +0100
Committer: Jim Ferenczi <ji...@apache.org>
Committed: Wed Feb 21 21:08:46 2018 +0100

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  3 ++
 .../apache/lucene/queries/BoostingQuery.java    | 18 +++----
 .../lucene/queries/BoostingQueryTest.java       | 51 +++++++++++++++++++-
 3 files changed, 61 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6d712c5e/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 06fe19b..cb1087a 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -94,6 +94,9 @@ Bug Fixes
 * LUCENE-8174: Fixed {Float,Double,Int,Long}Range.toString(). (Oliver Kaleske
   via Adrien Grand)
 
+* LUCENE-8182: Fixed BoostingQuery to apply the context boost instead of the parent query
+  boost (Jim Ferenczi)
+
 Other
 
 * LUCENE-8111: IndexOrDocValuesQuery Javadoc references outdated method name.

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6d712c5e/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java
----------------------------------------------------------------------
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java
index 4f0cb09..53201bd 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java
@@ -52,14 +52,14 @@ import org.apache.lucene.search.Weight;
  */
 @Deprecated
 public class BoostingQuery extends Query {
-    private final float boost;                            // the amount to boost by
+    private final float contextBoost;                     // the amount to boost by
     private final Query match;                            // query to match
     private final Query context;                          // boost when matches too
 
     public BoostingQuery(Query match, Query context, float boost) {
       this.match = match;
       this.context = context; // ignore context-only matches
-      this.boost = boost;
+      this.contextBoost = boost;
     }
 
     @Override
@@ -67,7 +67,7 @@ public class BoostingQuery extends Query {
       Query matchRewritten = match.rewrite(reader);
       Query contextRewritten = context.rewrite(reader);
       if (match != matchRewritten || context != contextRewritten) {
-        return new BoostingQuery(matchRewritten, contextRewritten, boost);
+        return new BoostingQuery(matchRewritten, contextRewritten, contextBoost);
       }
       return super.rewrite(reader);
     }
@@ -96,9 +96,9 @@ public class BoostingQuery extends Query {
           if (matchExplanation.isMatch() == false || contextExplanation.isMatch() == false) {
             return matchExplanation;
           }
-          return Explanation.match(matchExplanation.getValue() * boost, "product of:",
+          return Explanation.match(matchExplanation.getValue() * contextBoost, "product of:",
               matchExplanation,
-              Explanation.match(boost, "boost"));
+              Explanation.match(contextBoost, "boost"));
         }
 
         @Override
@@ -125,7 +125,7 @@ public class BoostingQuery extends Query {
               float score = super.score();
               if (contextApproximation.docID() == docID()
                   && (contextTwoPhase == null || contextTwoPhase.matches())) {
-                score *= boost;
+                score *= contextBoost;
               }
               return score;
             }
@@ -149,12 +149,12 @@ public class BoostingQuery extends Query {
     }
 
     public float getBoost() {
-      return boost;
+      return contextBoost;
     }
 
     @Override
     public int hashCode() {
-      return 31 * classHash() + Objects.hash(match, context, boost);
+      return 31 * classHash() + Objects.hash(match, context, contextBoost);
     }
 
     @Override
@@ -166,7 +166,7 @@ public class BoostingQuery extends Query {
     private boolean equalsTo(BoostingQuery other) {
       return match.equals(other.match)
           && context.equals(other.context)
-          && Float.floatToIntBits(boost) == Float.floatToIntBits(other.boost);
+          && Float.floatToIntBits(contextBoost) == Float.floatToIntBits(other.contextBoost);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6d712c5e/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java
----------------------------------------------------------------------
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java b/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java
index a224bda..95faf7b 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java
@@ -18,8 +18,14 @@ package org.apache.lucene.queries;
 
 import java.io.IOException;
 
+import org.apache.lucene.analysis.MockAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.FieldType;
+import org.apache.lucene.index.IndexOptions;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.MultiReader;
+import org.apache.lucene.index.RandomIndexWriter;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.IndexSearcher;
@@ -28,12 +34,15 @@ import org.apache.lucene.search.MatchNoDocsQuery;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.QueryUtils;
 import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.search.similarities.BooleanSimilarity;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.IOUtils;
 import org.apache.lucene.util.LuceneTestCase;
 
 public class BoostingQueryTest extends LuceneTestCase {
   // TODO: this suite desperately needs more tests!
-  // ... like ones that actually run the query
-  
+
   public void testBoostingQueryEquals() {
     TermQuery q1 = new TermQuery(new Term("subject:", "java"));
     TermQuery q2 = new TermQuery(new Term("subject:", "java"));
@@ -53,4 +62,42 @@ public class BoostingQueryTest extends LuceneTestCase {
     assertSame(rewritten, rewritten.rewrite(reader));
   }
 
+  public void testQueryScore() throws IOException {
+    Directory dir = newDirectory();
+    MockAnalyzer analyzer = new MockAnalyzer(random());
+    RandomIndexWriter w = new RandomIndexWriter(random(), dir, analyzer);
+    String[] docs = new String[] {
+        "foo bar",
+        "foo",
+        "foobar",
+        "foo baz"
+    };
+    FieldType ft = new FieldType();
+    ft.setStored(false);
+    ft.setIndexOptions(IndexOptions.DOCS);
+    ft.setOmitNorms(true);
+    for (int i = 0; i < docs.length; i++) {
+      Document doc = new Document();
+      doc.add(newStringField("id", "" + i, Field.Store.YES));
+      doc.add(new Field("field", docs[i], ft));
+      w.addDocument(doc);
+    }
+    IndexReader r = w.getReader();
+    IndexSearcher s = newSearcher(r);
+    s.setSimilarity(new BooleanSimilarity());
+    BoostingQuery query = new BoostingQuery(
+        new TermQuery(new Term("field", "foo")),
+        new TermQuery(new Term("field", "bar")),
+        0.1f
+    );
+    TopDocs search = s.search(query, 10);
+    assertEquals(3L, search.totalHits);
+    assertEquals(3, search.scoreDocs.length);
+    assertEquals(1.0f, search.scoreDocs[0].score, 0.0);
+    assertEquals(1.0f, search.scoreDocs[1].score, 0.0);
+    assertEquals(0.1f, search.scoreDocs[2].score, 0.0);
+    QueryUtils.check(random(), query, s);
+    IOUtils.close(r, w, dir, analyzer);
+  }
+
 }