LUCENE-9073: IntervalQuery expose field on toString and explain

This commit is contained in:
Mikhail Khludnev 2019-12-02 19:07:55 +03:00
parent 71f1bc33b3
commit eb3a4757ff
4 changed files with 29 additions and 4 deletions

View File

@ -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

View File

@ -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");

View File

@ -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

View File

@ -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 });