diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 08f9f7024e6..a41ec633834 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -295,6 +295,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 --------------------- @@ -390,7 +393,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 ======================= diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalBuilder.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalBuilder.java index 1e4075fd134..1552ec08682 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalBuilder.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/IntervalBuilder.java @@ -41,8 +41,6 @@ package org.apache.lucene.queries.intervals; 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; @@ -50,9 +48,7 @@ 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.IndexSearcher; -import org.apache.lucene.search.QueryVisitor; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings; @@ -66,6 +62,10 @@ import org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings; * "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; @@ -235,94 +235,4 @@ final class IntervalBuilder { } 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 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"; - } - }; } diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java index aa546a469c2..a16e721b5c8 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java @@ -500,6 +500,15 @@ public final class Intervals { 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 diff --git a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalBuilder.java b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalBuilder.java index b74670a51bc..e849475f4eb 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalBuilder.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalBuilder.java @@ -46,7 +46,6 @@ import org.apache.lucene.document.Field; 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; @@ -115,7 +114,7 @@ public class TestIntervalBuilder extends LuceneTestCase { 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 { @@ -235,18 +234,8 @@ public class TestIntervalBuilder extends LuceneTestCase { 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();