You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mk...@apache.org on 2019/12/02 16:13:44 UTC

[lucene-solr] branch master updated: LUCENE-9073: IntervalQuery expose field on toString and explain

This is an automated email from the ASF dual-hosted git repository.

mkhl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/master by this push:
     new eb3a475  LUCENE-9073: IntervalQuery expose field on toString and explain
eb3a475 is described below

commit eb3a4757ffc2cffaee062187622c2ce5a394e996
Author: Mikhail Khludnev <mk...@apache.org>
AuthorDate: Mon Dec 2 19:07:55 2019 +0300

    LUCENE-9073: IntervalQuery expose field on toString and explain
---
 lucene/CHANGES.txt                                   |  2 ++
 .../lucene/queries/intervals/IntervalQuery.java      |  7 +++++--
 .../queries/intervals/IntervalScoreFunction.java     |  4 ++--
 .../lucene/queries/intervals/TestIntervalQuery.java  | 20 ++++++++++++++++++++
 4 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index cf1f2ff..65f4098 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -93,6 +93,8 @@ Improvements
   that match a class of terms to pass a ByteRunAutomaton matching those that class
   back to the visitor. (Alan Woodward, David Smiley)
 
+* LUCENE-9073: IntervalQuery to respond field on toString() and explain() (Mikhail Khludnev)
+
 Optimizations
 
 * LUCENE-8928: When building a kd-tree for dimensions n > 2, compute exact bounds for an inner node every N splits
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalQuery.java
index 4bbfd68..10bff72 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalQuery.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalQuery.java
@@ -97,6 +97,9 @@ public final class IntervalQuery extends Query {
   }
 
   private IntervalQuery(String field, IntervalsSource intervalsSource, IntervalScoreFunction scoreFunction) {
+    Objects.requireNonNull(field, "null field aren't accepted");
+    Objects.requireNonNull(intervalsSource, "null intervalsSource aren't accepted");
+    Objects.requireNonNull(scoreFunction, "null scoreFunction aren't accepted");
     this.field = field;
     this.intervalsSource = intervalsSource;
     this.scoreFunction = scoreFunction;
@@ -111,7 +114,7 @@ public final class IntervalQuery extends Query {
 
   @Override
   public String toString(String field) {
-    return intervalsSource.toString();
+    return (!getField().equals(field) ? getField() + ":" : "") + intervalsSource.toString();
   }
 
   @Override
@@ -158,7 +161,7 @@ public final class IntervalQuery extends Query {
         int newDoc = scorer.iterator().advance(doc);
         if (newDoc == doc) {
           float freq = scorer.freq();
-          return scoreFunction.explain(intervalsSource.toString(), boost, freq);
+          return scoreFunction.explain(IntervalQuery.this.toString(), boost, freq);
         }
       }
       return Explanation.noMatch("no matching intervals");
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalScoreFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalScoreFunction.java
index 9adc442..4dc0c74 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalScoreFunction.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalScoreFunction.java
@@ -82,7 +82,7 @@ abstract class IntervalScoreFunction {
           "Saturation function on interval frequency, computed as w * S / (S + k) from:",
           Explanation.match(weight, "w, weight of this function"),
           Explanation.match(pivot, "k, pivot feature value that would give a score contribution equal to w/2"),
-          Explanation.match(sloppyFreq, "S, the sloppy frequency of the interval " + interval));
+          Explanation.match(sloppyFreq, "S, the sloppy frequency of the interval query " + interval));
     }
 
     @Override
@@ -136,7 +136,7 @@ abstract class IntervalScoreFunction {
           Explanation.match(weight, "w, weight of this function"),
           Explanation.match(pivot, "k, pivot feature value that would give a score contribution equal to w/2"),
           Explanation.match(a, "a, exponent, higher values make the function grow slower before k and faster after k"),
-          Explanation.match(sloppyFreq, "S, the sloppy frequency of the interval " + interval));
+          Explanation.match(sloppyFreq, "S, the sloppy frequency of the interval query " + interval));
     }
 
     @Override
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java
index 0f18446..097b4ab 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java
@@ -26,6 +26,7 @@ import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.RandomIndexWriter;
 import org.apache.lucene.search.BoostQuery;
 import org.apache.lucene.search.CheckHits;
+import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TopDocs;
@@ -126,6 +127,25 @@ public class TestIntervalQuery extends LuceneTestCase {
     checkHits(q, new int[]{1, 3, 5});
   }
 
+  public void testFieldInToString() throws IOException {
+    final IntervalQuery fieldW1 = new IntervalQuery(field, Intervals.term("w1"));
+    assertTrue(fieldW1.toString().contains(field));
+    final String field2 = field+"2";
+    final IntervalQuery f2w1 = new IntervalQuery(field2, Intervals.term("w1"));
+    assertTrue(f2w1.toString().contains(field2+":"));
+    assertFalse("suppress default field",f2w1.toString(field2).contains(field2));
+    
+    final Explanation explain = searcher.explain(new IntervalQuery(field, 
+        Intervals.ordered(Intervals.term("w1"), Intervals.term("w2"))), searcher.search(fieldW1, 1).scoreDocs[0].doc);
+    assertTrue(explain.toString().contains(field));
+  }
+  
+  public void testNullConstructorArgs() throws IOException {
+    expectThrows(NullPointerException.class, ()-> new IntervalQuery(null, Intervals.term("z")));
+    expectThrows(NullPointerException.class, ()-> new IntervalQuery("field", null));
+  }
+  
+  
   public void testNotWithinQuery() throws IOException {
     Query q = new IntervalQuery(field, Intervals.notWithin(Intervals.term("w1"), 1, Intervals.term("w2")));
     checkHits(q, new int[]{ 1, 2, 3 });