Skip to content

Commit

Permalink
Add Intervals.noIntervals() method (#13389)
Browse files Browse the repository at this point in the history
Parsers may sometimes want to create an IntervalsSource that returns no
intervals.  This adds a new factory method to `Intervals` that will create one,
and changes `IntervalBuilder` to use it in place of its custom empty intervals
source.
  • Loading branch information
romseygeek committed May 21, 2024
1 parent 26fca9e commit f1398ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 109 deletions.
4 changes: 3 additions & 1 deletion lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ Improvements

* GITHUB#13362: Add sub query explanations to DisjunctionMaxQuery, if the overall query didn't match. (Tim Grein)

* GITHUB#13385: Add Intervals.noIntervals() method to produce an empty IntervalsSource.
(Aniketh Jain, Uwe Schindler, Alan Woodward))

Optimizations
---------------------

Expand Down Expand Up @@ -162,7 +165,6 @@ Other

* GITHUB#13077: Add public getter for SynonymQuery#field (Andrey Bozhko)

* GITHUB#13385: Make NO_INTERVALS source as public to be used by Lucene clients instead of creating clones themselves (Aniketh Jain)

======================== Lucene 9.10.0 =======================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.lucene.analysis.CachingTokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute;
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings;

Expand All @@ -66,6 +62,10 @@
* "https://github.com/elastic/elasticsearch/blob/7.10/server/src/main/java/org/elasticsearch/index/query/IntervalBuilder.java"
*/
final class IntervalBuilder {

private static final IntervalsSource NO_INTERVALS =
Intervals.noIntervals("No terms in analyzed text");

static IntervalsSource analyzeText(CachingTokenFilter stream, int maxGaps, boolean ordered)
throws IOException {
assert stream != null;
Expand Down Expand Up @@ -236,94 +236,4 @@ private static List<IntervalsSource> analyzeGraph(TokenStream source) throws IOE
}
return clauses;
}

public static final IntervalsSource NO_INTERVALS =
new IntervalsSource() {
@Override
public IntervalIterator intervals(String field, LeafReaderContext ctx) {
return new IntervalIterator() {
boolean exhausted = false;

@Override
public int start() {
return NO_MORE_INTERVALS;
}

@Override
public int end() {
return NO_MORE_INTERVALS;
}

@Override
public int gaps() {
throw new UnsupportedOperationException();
}

@Override
public int nextInterval() {
return NO_MORE_INTERVALS;
}

@Override
public float matchCost() {
return 0;
}

@Override
public int docID() {
return exhausted ? NO_MORE_DOCS : -1;
}

@Override
public int nextDoc() {
exhausted = true;
return NO_MORE_DOCS;
}

@Override
public int advance(int target) {
exhausted = true;
return NO_MORE_DOCS;
}

@Override
public long cost() {
return 0;
}
};
}

@Override
public IntervalMatchesIterator matches(String field, LeafReaderContext ctx, int doc) {
return null;
}

@Override
public void visit(String field, QueryVisitor visitor) {}

@Override
public int minExtent() {
return 0;
}

@Override
public Collection<IntervalsSource> pullUpDisjunctions() {
return Collections.emptyList();
}

@Override
public int hashCode() {
return 0;
}

@Override
public boolean equals(Object other) {
return other == this;
}

@Override
public String toString() {
return "no_match";
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,15 @@ public static IntervalsSource after(IntervalsSource source, IntervalsSource refe
Intervals.extend(new OffsetIntervalsSource(reference, false), 0, Integer.MAX_VALUE));
}

/**
* Returns a source that produces no intervals
*
* @param reason A reason string that will appear in the toString output of this source
*/
public static IntervalsSource noIntervals(String reason) {
return new NoMatchIntervalsSource(reason);
}

/**
* Returns intervals that correspond to tokens from a {@link TokenStream} returned for {@code
* text} by applying the provided {@link Analyzer} as if {@code text} was the content of the given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.CannedTokenStream;
import org.apache.lucene.tests.analysis.Token;
Expand Down Expand Up @@ -115,7 +114,7 @@ public void testPhraseWithStopword() throws IOException {
public void testEmptyTokenStream() throws IOException {
CannedTokenStream ts = new CannedTokenStream();
IntervalsSource source = IntervalBuilder.analyzeText(new CachingTokenFilter(ts), 0, true);
assertSame(IntervalBuilder.NO_INTERVALS, source);
assertEquals(Intervals.noIntervals("No terms in analyzed text"), source);
}

public void testSimpleSynonyms() throws IOException {
Expand Down Expand Up @@ -235,18 +234,8 @@ public void testEmptyIntervals() throws IOException {
IndexReader reader = DirectoryReader.open(directory);
LeafReaderContext ctx = reader.leaves().get(0);

{
IntervalIterator it = source.intervals("field", ctx);
assertEquals(-1, it.docID());
it.nextDoc();
assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.docID());
}
{
IntervalIterator it = source.intervals("field", ctx);
assertEquals(-1, it.docID());
it.advance(1);
assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.docID());
}
IntervalIterator it = source.intervals("field", ctx);
assertNull(it);

reader.close();
directory.close();
Expand Down

0 comments on commit f1398ce

Please sign in to comment.