Apache Lucene open-source search software
LUCENE-8270: Remove MatchesIterator.term()
Alan Woodwarde167e9124757
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 301b360d0c7..a440f9567a4 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -137,9 +137,9 @@ New Featuressoft deletes if the reader is opened form a directory. (Simon Willnauer,Mike McCandless, Uwe Schindler, Adrien Grand)-* LUCENE-8229: Add a method Weight.matches(LeafReaderContext, doc) that returns- an iterator over matching positions for a given query and document. This- allows exact hit extraction and will enable implementation of accurate+* LUCENE-8229, LUCENE-8270: Add a method Weight.matches(LeafReaderContext, doc)+ that returns an iterator over matching positions for a given query and document.+ This allows exact hit extraction and will enable implementation of accuratehighlighters. (Alan Woodward, Adrien Grand, David Smiley)* LUCENE-8246: Allow to customize the number of deletes a merge claims. Thisdiff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.javaindex a18b280e378..975199bd141 100644--- a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.java+++ b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.java@@ -84,8 +84,7 @@ final class DisjunctionMatchesIterator implements MatchesIterator {if (te.seekExact(term)) {PostingsEnum pe = te.postings(reuse, PostingsEnum.OFFSETS);if (pe.advance(doc) == doc) {- // TODO do we want to use the copied term here, or instead create a label that associates all of the TMIs with a single term?- mis.add(new TermMatchesIterator(BytesRef.deepCopyOf(term), pe));+ mis.add(new TermMatchesIterator(pe));reuse = null;}else {@@ -114,7 +113,7 @@ final class DisjunctionMatchesIterator implements MatchesIterator {protected boolean lessThan(MatchesIterator a, MatchesIterator b) {return a.startPosition() < b.startPosition() ||(a.startPosition() == b.startPosition() && a.endPosition() < b.endPosition()) ||- (a.startPosition() == b.startPosition() && a.endPosition() == b.endPosition() && a.term().compareTo(b.term()) < 0);+ (a.startPosition() == b.startPosition() && a.endPosition() == b.endPosition());}};for (MatchesIterator mi : matches) {@@ -159,9 +158,4 @@ final class DisjunctionMatchesIterator implements MatchesIterator {return queue.top().endOffset();}- @Override- public BytesRef term() {- return queue.top().term();- }-}diff --git a/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.java b/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.javaindex d695ea59f63..450a352720e 100644--- a/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.java+++ b/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.java@@ -20,7 +20,6 @@ package org.apache.lucene.search;import java.io.IOException;import org.apache.lucene.index.LeafReaderContext;-import org.apache.lucene.util.BytesRef;/*** An iterator over match positions (and optionally offsets) for a single document and field@@ -71,11 +70,4 @@ public interface MatchesIterator {*/int endOffset() throws IOException;- /**- * The underlying term of the current match- *- * Should only be called after {@link #next()} has returned {@code true}- */- BytesRef term();-}diff --git a/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.java b/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.javaindex 051699651db..defc3af5751 100644--- a/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.java+++ b/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.java@@ -20,7 +20,6 @@ package org.apache.lucene.search;import java.io.IOException;import org.apache.lucene.index.PostingsEnum;-import org.apache.lucene.util.BytesRef;/*** A {@link MatchesIterator} over a single term's postings list@@ -30,15 +29,13 @@ class TermMatchesIterator implements MatchesIterator {private int upto;private int pos;private final PostingsEnum pe;- private final BytesRef term;/*** Create a new {@link TermMatchesIterator} for the given term and postings list*/- TermMatchesIterator(BytesRef term, PostingsEnum pe) throws IOException {+ TermMatchesIterator(PostingsEnum pe) throws IOException {this.pe = pe;this.upto = pe.freq();- this.term = term;}@Override@@ -70,8 +67,4 @@ class TermMatchesIterator implements MatchesIterator {return pe.endOffset();}- @Override- public BytesRef term() {- return term;- }}diff --git a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermQuery.javaindex b86f340f334..27b42ada480 100644--- a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/TermQuery.java@@ -95,7 +95,7 @@ public class TermQuery extends Query {if (pe.advance(doc) != doc) {return null;}- return new TermMatchesIterator(term.bytes(), pe);+ return new TermMatchesIterator(pe);});}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java b/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.javaindex 3b3dd322516..185aad9be93 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java@@ -147,30 +147,6 @@ public class TestMatchesIterator extends LuceneTestCase {}}- void checkTerms(Query q, String field, String[][] expected) throws IOException {- Weight w = searcher.createWeight(searcher.rewrite(q), ScoreMode.COMPLETE_NO_SCORES, 1);- for (int i = 0; i < expected.length; i++) {- LeafReaderContext ctx = searcher.leafContexts.get(ReaderUtil.subIndex(i, searcher.leafContexts));- int doc = i - ctx.docBase;- Matches matches = w.matches(ctx, doc);- if (matches == null) {- assertEquals(expected[i].length, 0);- continue;- }- MatchesIterator it = matches.getMatches(field);- if (it == null) {- assertEquals(expected[i].length, 0);- continue;- }- int pos = 0;- while (it.next()) {- assertEquals(expected[i][pos], it.term().utf8ToString());- pos += 1;- }- assertEquals(expected[i].length, pos);- }- }-public void testTermQuery() throws IOException {Query q = new TermQuery(new Term(FIELD_WITH_OFFSETS, "w1"));checkMatches(q, FIELD_WITH_OFFSETS, new int[][]{@@ -191,13 +167,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, -1, -1, 2, 2, -1, -1 },{ 4 }});- checkTerms(q, FIELD_NO_OFFSETS, new String[][]{- { "w1" },- { "w1" },- { "w1" },- { "w1", "w1" },- {}- });}public void testTermQueryNoPositions() throws IOException {@@ -208,9 +177,11 @@ public class TestMatchesIterator extends LuceneTestCase {}public void testDisjunction() throws IOException {+ Query w1 = new TermQuery(new Term(FIELD_WITH_OFFSETS, "w1"));+ Query w3 = new TermQuery(new Term(FIELD_WITH_OFFSETS, "w3"));Query q = new BooleanQuery.Builder()- .add(new TermQuery(new Term(FIELD_WITH_OFFSETS, "w1")), BooleanClause.Occur.SHOULD)- .add(new TermQuery(new Term(FIELD_WITH_OFFSETS, "w3")), BooleanClause.Occur.SHOULD)+ .add(w1, BooleanClause.Occur.SHOULD)+ .add(w3, BooleanClause.Occur.SHOULD).build();checkMatches(q, FIELD_WITH_OFFSETS, new int[][]{{ 0, 0, 0, 0, 2, 2, 2, 6, 8 },@@ -219,13 +190,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, 0, 2, 2, 2, 6, 8, 5, 5, 15, 17 },{ 4 }});- checkTerms(q, FIELD_WITH_OFFSETS, new String[][]{- { "w1", "w3" },- { "w1", "w3", "w3" },- { "w1" },- { "w1", "w1", "w3" },- {}- });}public void testDisjunctionNoPositions() throws IOException {@@ -263,12 +227,16 @@ public class TestMatchesIterator extends LuceneTestCase {}public void testMinShouldMatch() throws IOException {+ Query w1 = new TermQuery(new Term(FIELD_WITH_OFFSETS, "w1"));+ Query w3 = new TermQuery(new Term(FIELD_WITH_OFFSETS, "w3"));+ Query w4 = new TermQuery(new Term(FIELD_WITH_OFFSETS, "w4"));+ Query xx = new TermQuery(new Term(FIELD_WITH_OFFSETS, "xx"));Query q = new BooleanQuery.Builder()- .add(new TermQuery(new Term(FIELD_WITH_OFFSETS, "w3")), BooleanClause.Occur.SHOULD)+ .add(w3, BooleanClause.Occur.SHOULD).add(new BooleanQuery.Builder()- .add(new TermQuery(new Term(FIELD_WITH_OFFSETS, "w1")), BooleanClause.Occur.SHOULD)- .add(new TermQuery(new Term(FIELD_WITH_OFFSETS, "w4")), BooleanClause.Occur.SHOULD)- .add(new TermQuery(new Term(FIELD_WITH_OFFSETS, "xx")), BooleanClause.Occur.SHOULD)+ .add(w1, BooleanClause.Occur.SHOULD)+ .add(w4, BooleanClause.Occur.SHOULD)+ .add(xx, BooleanClause.Occur.SHOULD).setMinimumNumberShouldMatch(2).build(), BooleanClause.Occur.SHOULD).build();@@ -279,13 +247,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, 0, 2, 2, 2, 6, 8, 3, 3, 9, 11, 5, 5, 15, 17 },{ 4 }});- checkTerms(q, FIELD_WITH_OFFSETS, new String[][]{- { "w1", "w3", "w4" },- { "w3", "w3" },- { "w1", "xx", "w4" },- { "w1", "w1", "w4", "w3" },- {}- });}public void testMinShouldMatchNoPositions() throws IOException {@@ -360,9 +321,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3 },{ 4 }});- checkTerms(q, FIELD_WITH_OFFSETS, new String[][]{- {}, {}, { "xx" }, {}- });Query rq = new RegexpQuery(new Term(FIELD_WITH_OFFSETS, "w[1-2]"));checkMatches(rq, FIELD_WITH_OFFSETS, new int[][]{@@ -430,11 +388,4 @@ public class TestMatchesIterator extends LuceneTestCase {assertTrue(fields.contains("id"));}- protected String[] doc1Fields = {- "w1 w2 w3 w4 w5",- "w1 w3 w2 w3 zz",- "w1 xx w2 yy w4",- "w1 w2 w1 w4 w2 w3"- };-}diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingMatchesIterator.java b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingMatchesIterator.javaindex 52fb184250c..4f065129ec3 100644--- a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingMatchesIterator.java+++ b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingMatchesIterator.java@@ -19,8 +19,6 @@ package org.apache.lucene.search;import java.io.IOException;-import org.apache.lucene.util.BytesRef;-class AssertingMatchesIterator implements MatchesIterator {private final MatchesIterator in;@@ -69,9 +67,4 @@ class AssertingMatchesIterator implements MatchesIterator {return in.endOffset();}- @Override- public BytesRef term() {- assert state == State.ITERATING : state;- return in.term();- }}