Apache Lucene open-source search software
Revert "LUCENE-8306: Allow iteration over submatches"
Alan Woodward6e3f61f6f9b4
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 47769cd8145..8257dbc801f 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -198,9 +198,6 @@ Improvements* LUCENE-8345, GitHub PR #392: Remove instantiation of redundant wrapper classes for primitives;add wrapper class constructors to forbiddenapis. (Michael Braun via Uwe Schindler)-* LUCENE-8306: Matches API now allows iteration over sub-matches in Spans (Alan Woodward,- Jim Ferenczi, David Smiley)-Other:* LUCENE-8366: Upgrade to ICU 62.1. Emoji handling now uses Unicode 11'sdiff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.javaindex cff723b9ab9..975199bd141 100644--- a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.java+++ b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMatchesIterator.java@@ -158,13 +158,4 @@ final class DisjunctionMatchesIterator implements MatchesIterator {return queue.top().endOffset();}- @Override- public MatchesIterator getSubMatches() throws IOException {- return queue.top().getSubMatches();- }-- @Override- public Object label() {- return queue.top().label();- }}diff --git a/lucene/core/src/java/org/apache/lucene/search/ExactPhraseMatcher.java b/lucene/core/src/java/org/apache/lucene/search/ExactPhraseMatcher.javaindex 6fb49b16d13..b95077d0971 100644--- a/lucene/core/src/java/org/apache/lucene/search/ExactPhraseMatcher.java+++ b/lucene/core/src/java/org/apache/lucene/search/ExactPhraseMatcher.java@@ -149,48 +149,4 @@ final class ExactPhraseMatcher extends PhraseMatcher {return postings[postings.length - 1].postings.endOffset();}- @Override- MatchesIterator getSubMatches() {- return new MatchesIterator() {-- int upTo = -1;-- @Override- public boolean next() throws IOException {- upTo++;- return upTo < postings.length;- }-- @Override- public int startPosition() {- return postings[upTo].pos;- }-- @Override- public int endPosition() {- return postings[upTo].pos;- }-- @Override- public int startOffset() throws IOException {- return postings[upTo].postings.startOffset();- }-- @Override- public int endOffset() throws IOException {- return postings[upTo].postings.endOffset();- }-- @Override- public MatchesIterator getSubMatches() throws IOException {- return MatchesIterator.EMPTY_ITERATOR;- }-- @Override- public Object label() {- return this;- }- };- }-}diff --git a/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.java b/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.javaindex 5814d7cb88b..450a352720e 100644--- a/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.java+++ b/lucene/core/src/java/org/apache/lucene/search/MatchesIterator.java@@ -28,9 +28,6 @@ import org.apache.lucene.index.LeafReaderContext;* positions and/or offsets after each call. You should not call the position or offset methods* before {@link #next()} has been called, or after {@link #next()} has returned {@code false}.*- * Matches from some queries may span multiple positions. You can retrieve the positions of- * individual matching terms on the current match by calling {@link #getSubMatches()}.- ** Matches are ordered by start position, and then by end position. Match intervals may overlap.** @see Weight#matches(LeafReaderContext, int)@@ -73,59 +70,4 @@ public interface MatchesIterator {*/int endOffset() throws IOException;- /**- * Returns a MatchesIterator that iterates over the positions and offsets of individual- * terms within the current match- *- * Should only be called after {@link #next()} has returned {@code true}- */- MatchesIterator getSubMatches() throws IOException;-- /**- * Returns a label identifying the leaf query causing the current match- *- * Should only be called after {@link #next()} has returned {@code true}- */- Object label();-- /**- * A MatchesIterator that is immediately exhausted- */- MatchesIterator EMPTY_ITERATOR = new MatchesIterator() {- @Override- public boolean next() throws IOException {- return false;- }-- @Override- public int startPosition() {- throw new UnsupportedOperationException();- }-- @Override- public int endPosition() {- throw new UnsupportedOperationException();- }-- @Override- public int startOffset() throws IOException {- throw new UnsupportedOperationException();- }-- @Override- public int endOffset() throws IOException {- throw new UnsupportedOperationException();- }-- @Override- public MatchesIterator getSubMatches() throws IOException {- throw new UnsupportedOperationException();- }-- @Override- public Object label() {- return this;- }- };-}diff --git a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.javaindex c8d22baec91..22b71279012 100644--- a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java@@ -269,7 +269,7 @@ public class MultiPhraseQuery extends Query {TermState termState = termStates.get(term).get(context);if (termState != null) {termsEnum.seekExact(term.bytes(), termState);- postings.add(termsEnum.postings(null, exposeOffsets ? PostingsEnum.ALL : PostingsEnum.POSITIONS));+ postings.add(termsEnum.postings(null, exposeOffsets ? PostingsEnum.OFFSETS : PostingsEnum.POSITIONS));totalMatchCost += PhraseQuery.termPositionsCost(termsEnum);}}@@ -294,7 +294,7 @@ public class MultiPhraseQuery extends Query {return new ExactPhraseMatcher(postingsFreqs, totalMatchCost);}else {- return new SloppyPhraseMatcher(postingsFreqs, slop, totalMatchCost, exposeOffsets);+ return new SloppyPhraseMatcher(postingsFreqs, slop, totalMatchCost);}}@@ -647,6 +647,5 @@ public class MultiPhraseQuery extends Query {public BytesRef getPayload() throws IOException {return posQueue.top().pe.getPayload();}-}}diff --git a/lucene/core/src/java/org/apache/lucene/search/PhraseMatcher.java b/lucene/core/src/java/org/apache/lucene/search/PhraseMatcher.javaindex c4b1e36684e..81040d517c4 100644--- a/lucene/core/src/java/org/apache/lucene/search/PhraseMatcher.java+++ b/lucene/core/src/java/org/apache/lucene/search/PhraseMatcher.java@@ -88,6 +88,4 @@ abstract class PhraseMatcher {public float getMatchCost() {return matchCost;}-- abstract MatchesIterator getSubMatches() throws IOException;}diff --git a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.javaindex 8f042716a65..70d2e09ef3b 100644--- a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java@@ -446,7 +446,7 @@ public class PhraseQuery extends Query {return null;}te.seekExact(t.bytes(), state);- PostingsEnum postingsEnum = te.postings(null, exposeOffsets ? PostingsEnum.ALL : PostingsEnum.POSITIONS);+ PostingsEnum postingsEnum = te.postings(null, exposeOffsets ? PostingsEnum.OFFSETS : PostingsEnum.POSITIONS);postingsFreqs[i] = new PostingsAndFreq(postingsEnum, positions[i], t);totalMatchCost += termPositionsCost(te);}@@ -457,7 +457,7 @@ public class PhraseQuery extends Query {return new ExactPhraseMatcher(postingsFreqs, totalMatchCost);}else {- return new SloppyPhraseMatcher(postingsFreqs, slop, totalMatchCost, exposeOffsets);+ return new SloppyPhraseMatcher(postingsFreqs, slop, totalMatchCost);}}diff --git a/lucene/core/src/java/org/apache/lucene/search/PhraseWeight.java b/lucene/core/src/java/org/apache/lucene/search/PhraseWeight.javaindex 2547b8c8c54..90fa537c366 100644--- a/lucene/core/src/java/org/apache/lucene/search/PhraseWeight.java+++ b/lucene/core/src/java/org/apache/lucene/search/PhraseWeight.java@@ -123,16 +123,6 @@ abstract class PhraseWeight extends Weight {public int endOffset() throws IOException {return matcher.endOffset();}-- @Override- public MatchesIterator getSubMatches() throws IOException {- return matcher.getSubMatches();- }-- @Override- public Object label() {- return matcher;- }};});}diff --git a/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseMatcher.java b/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseMatcher.javaindex e9c45195713..326816d1428 100644--- a/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseMatcher.java+++ b/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseMatcher.java@@ -54,14 +54,13 @@ final class SloppyPhraseMatcher extends PhraseMatcher {private final int slop;private final int numPostings;private final PhraseQueue pq; // for advancing min position- private final boolean captureLeadMatch;private int end; // current largest phrase positionprivate int leadPosition;private int leadOffset;- private int leadEndOffset;- private int leadOrd;+ private int currentEndPostings;+ private int advanceEndPostings;private boolean hasRpts; // flag indicating that there are repetitions (as checked in first candidate doc)private boolean checkedRpts; // flag to only check for repetitions in first candidate doc@@ -72,11 +71,10 @@ final class SloppyPhraseMatcher extends PhraseMatcher {private boolean positioned;private int matchLength;- SloppyPhraseMatcher(PhraseQuery.PostingsAndFreq[] postings, int slop, float matchCost, boolean captureLeadMatch) {+ SloppyPhraseMatcher(PhraseQuery.PostingsAndFreq[] postings, int slop, float matchCost) {super(approximation(postings), matchCost);this.slop = slop;this.numPostings = postings.length;- this.captureLeadMatch = captureLeadMatch;pq = new PhraseQueue(postings.length);phrasePositions = new PhrasePositions[postings.length];for (int i = 0; i < postings.length; ++i) {@@ -122,8 +120,10 @@ final class SloppyPhraseMatcher extends PhraseMatcher {return false;}PhrasePositions pp = pq.pop();- assert pp != null; // if the pq is not full, then positioned == false- captureLead(pp);+ assert pp != null; // if the pq is empty, then positioned == false+ leadPosition = pp.position + pp.offset;+ leadOffset = pp.postings.startOffset();+ currentEndPostings = advanceEndPostings;matchLength = end - pp.position;int next = pq.top().position;while (advancePP(pp)) {@@ -137,7 +137,6 @@ final class SloppyPhraseMatcher extends PhraseMatcher {}pp = pq.pop();next = pq.top().position;- assert pp != null; // if the pq is not full, then positioned == falsematchLength = end - pp.position;} else {int matchLength2 = end - pp.position;@@ -145,22 +144,14 @@ final class SloppyPhraseMatcher extends PhraseMatcher {matchLength = matchLength2;}}- captureLead(pp);+ leadPosition = pp.position + pp.offset;+ leadOffset = pp.postings.startOffset();+ currentEndPostings = advanceEndPostings;}positioned = false;return matchLength <= slop;}- private void captureLead(PhrasePositions pp) throws IOException {- if (captureLeadMatch == false) {- return;- }- leadOrd = pp.ord;- leadPosition = pp.position + pp.offset;- leadOffset = pp.postings.startOffset();- leadEndOffset = pp.postings.endOffset();- }-@Overridepublic int startPosition() {// when a match is detected, the top postings is advanced until it has moved@@ -169,7 +160,6 @@ final class SloppyPhraseMatcher extends PhraseMatcher {// However, the priority queue doesn't guarantee that the top postings is in fact the// earliest in the list, so we need to cycle through all terms to check.// this is slow, but Matches is slow anyway...- int leadPosition = this.leadPosition;for (PhrasePositions pp : phrasePositions) {leadPosition = Math.min(leadPosition, pp.position + pp.offset);}@@ -178,13 +168,7 @@ final class SloppyPhraseMatcher extends PhraseMatcher {@Overridepublic int endPosition() {- int endPosition = leadPosition;- for (PhrasePositions pp : phrasePositions) {- if (pp.ord != leadOrd) {- endPosition = Math.max(endPosition, pp.position + pp.offset);- }- }- return endPosition;+ return phrasePositions[currentEndPostings].position + phrasePositions[currentEndPostings].offset;}@Override@@ -195,7 +179,6 @@ final class SloppyPhraseMatcher extends PhraseMatcher {// However, the priority queue doesn't guarantee that the top postings is in fact the// earliest in the list, so we need to cycle through all terms to check// this is slow, but Matches is slow anyway...- int leadOffset = this.leadOffset;for (PhrasePositions pp : phrasePositions) {leadOffset = Math.min(leadOffset, pp.postings.startOffset());}@@ -204,69 +187,7 @@ final class SloppyPhraseMatcher extends PhraseMatcher {@Overridepublic int endOffset() throws IOException {- int endOffset = leadEndOffset;- for (PhrasePositions pp : phrasePositions) {- if (pp.ord != leadOrd) {- endOffset = Math.max(endOffset, pp.postings.endOffset());- }- }- return endOffset;- }-- @Override- MatchesIterator getSubMatches() throws IOException {- int[][] submatches = new int[phrasePositions.length][3];- for (PhrasePositions pp : phrasePositions) {- if (pp.ord == leadOrd) {- submatches[pp.ord][0] = leadPosition;- submatches[pp.ord][1] = leadOffset;- submatches[pp.ord][2] = leadEndOffset;- }- else {- submatches[pp.ord][0] = pp.position + pp.offset;- submatches[pp.ord][1] = pp.postings.startOffset();- submatches[pp.ord][2] = pp.postings.endOffset();- }- }- Arrays.sort(submatches, Comparator.comparingInt(a -> a[0]));- return new MatchesIterator() {- int upTo = -1;- @Override- public boolean next() throws IOException {- upTo++;- return upTo < submatches.length;- }-- @Override- public int startPosition() {- return submatches[upTo][0];- }-- @Override- public int endPosition() {- return submatches[upTo][0];- }-- @Override- public int startOffset() {- return submatches[upTo][1];- }-- @Override- public int endOffset() {- return submatches[upTo][2];- }-- @Override- public MatchesIterator getSubMatches() {- return MatchesIterator.EMPTY_ITERATOR;- }-- @Override- public Object label() {- return this;- }- };+ return phrasePositions[currentEndPostings].postings.endOffset();}/** advance a PhrasePosition and update 'end', return false if exhausted */@@ -276,6 +197,12 @@ final class SloppyPhraseMatcher extends PhraseMatcher {}if (pp.position > end) {end = pp.position;+ advanceEndPostings = pp.ord;+ }+ if (pp.position == end) {+ if (pp.ord > advanceEndPostings) {+ advanceEndPostings = pp.ord;+ }}return true;}@@ -380,6 +307,12 @@ final class SloppyPhraseMatcher extends PhraseMatcher {pp.firstPosition();if (pp.position > end) {end = pp.position;+ advanceEndPostings = pp.ord;+ }+ if (pp.position == end) {+ if (pp.ord > advanceEndPostings) {+ advanceEndPostings = pp.ord;+ }}pq.add(pp);}@@ -409,6 +342,12 @@ final class SloppyPhraseMatcher extends PhraseMatcher {for (PhrasePositions pp : phrasePositions) { // iterate cyclic list: done once handled maxif (pp.position > end) {end = pp.position;+ advanceEndPostings = pp.ord;+ }+ if (pp.position == end) {+ if (pp.ord > advanceEndPostings) {+ advanceEndPostings = pp.ord;+ }}pq.add(pp);}diff --git a/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.java b/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.javaindex 23858c6b5e8..defc3af5751 100644--- a/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.java+++ b/lucene/core/src/java/org/apache/lucene/search/TermMatchesIterator.java@@ -67,54 +67,4 @@ class TermMatchesIterator implements MatchesIterator {return pe.endOffset();}- @Override- public Object label() {- return pe;- }-- @Override- public MatchesIterator getSubMatches() throws IOException {- return new MatchesIterator() {-- boolean exhausted = false;-- @Override- public boolean next() {- if (exhausted) {- return false;- }- return exhausted = true;- }-- @Override- public int startPosition() {- return pos;- }-- @Override- public int endPosition() {- return pos;- }-- @Override- public int startOffset() throws IOException {- return pe.startOffset();- }-- @Override- public int endOffset() throws IOException {- return pe.endOffset();- }-- @Override- public MatchesIterator getSubMatches() {- return MatchesIterator.EMPTY_ITERATOR;- }-- @Override- public Object label() {- return this;- }- };- }}diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.javaindex ca789ce4c80..0313d56510c 100644--- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.java+++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.java@@ -18,8 +18,6 @@ package org.apache.lucene.search.spans;import java.io.IOException;-import java.util.Arrays;-import java.util.Comparator;import java.util.Map;import org.apache.lucene.index.LeafReaderContext;@@ -30,8 +28,6 @@ import org.apache.lucene.search.CollectionStatistics;import org.apache.lucene.search.Explanation;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.LeafSimScorer;-import org.apache.lucene.search.Matches;-import org.apache.lucene.search.MatchesIterator;import org.apache.lucene.search.TermStatistics;import org.apache.lucene.search.Weight;import org.apache.lucene.search.similarities.Similarity;@@ -165,131 +161,4 @@ public abstract class SpanWeight extends Weight {return Explanation.noMatch("no matching term");}-- @Override- public Matches matches(LeafReaderContext context, int doc) throws IOException {- return Matches.forField(field, () -> {- Spans spans = getSpans(context, Postings.OFFSETS);- if (spans == null) {- return null;- }- if (spans.advance(doc) != doc) {- return null;- }- return new MatchesIterator() {-- int innerTermCount = 0;- int[][] innerTerms = new int[2][3];- SpanCollector termCollector = new SpanCollector() {- @Override- public void collectLeaf(PostingsEnum postings, int position, Term term) throws IOException {- innerTermCount++;- if (innerTermCount > innerTerms.length) {- int[][] temp = new int[innerTermCount][3];- System.arraycopy(innerTerms, 0, temp, 0, innerTermCount - 1);- innerTerms = temp;- }- innerTerms[innerTermCount - 1][0] = position;- innerTerms[innerTermCount - 1][1] = postings.startOffset();- innerTerms[innerTermCount - 1][2] = postings.endOffset();- }-- @Override- public void reset() {- innerTermCount = 0;- }- };-- @Override- public boolean next() throws IOException {- innerTermCount = 0;- return spans.nextStartPosition() != Spans.NO_MORE_POSITIONS;- }-- @Override- public int startPosition() {- return spans.startPosition();- }-- @Override- public int endPosition() {- return spans.endPosition() - 1;- }-- @Override- public int startOffset() throws IOException {- if (innerTermCount == 0) {- collectInnerTerms();- }- return innerTerms[0][1];- }-- @Override- public int endOffset() throws IOException {- if (innerTermCount == 0) {- collectInnerTerms();- }- return innerTerms[innerTermCount - 1][2];- }-- @Override- public MatchesIterator getSubMatches() throws IOException {- if (innerTermCount == 0) {- collectInnerTerms();- }- return new MatchesIterator() {-- int upto = -1;-- @Override- public boolean next() throws IOException {- upto++;- return upto < innerTermCount;- }-- @Override- public int startPosition() {- return innerTerms[upto][0];- }-- @Override- public int endPosition() {- return innerTerms[upto][0];- }-- @Override- public int startOffset() throws IOException {- return innerTerms[upto][1];- }-- @Override- public int endOffset() throws IOException {- return innerTerms[upto][2];- }-- @Override- public MatchesIterator getSubMatches() throws IOException {- return MatchesIterator.EMPTY_ITERATOR;- }-- @Override- public Object label() {- return this;- }- };- }-- @Override- public Object label() {- return SpanWeight.this;- }-- void collectInnerTerms() throws IOException {- termCollector.reset();- spans.collect(termCollector);- Arrays.sort(innerTerms, 0, innerTermCount, Comparator.comparing(a -> a[0]));- }- };- });- }}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java b/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.javaindex 86e43994ed7..3855b04ad6b 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java@@ -18,12 +18,8 @@package org.apache.lucene.search;import java.io.IOException;-import java.util.Arrays;import java.util.HashSet;-import java.util.IdentityHashMap;-import java.util.Objects;import java.util.Set;-import java.util.stream.Collectors;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.Document;@@ -33,14 +29,9 @@ import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexOptions;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.LeafReaderContext;-import org.apache.lucene.index.PostingsEnum;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.ReaderUtil;import org.apache.lucene.index.Term;-import org.apache.lucene.search.spans.SpanNearQuery;-import org.apache.lucene.search.spans.SpanOrQuery;-import org.apache.lucene.search.spans.SpanQuery;-import org.apache.lucene.search.spans.SpanTermQuery;import org.apache.lucene.store.Directory;import org.apache.lucene.util.LuceneTestCase;@@ -108,7 +99,7 @@ public class TestMatchesIterator extends LuceneTestCase {"nothing matches this document"};- private void checkMatches(Query q, String field, int[][] expected) throws IOException {+ void checkMatches(Query q, String field, int[][] 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(expected[i][0], searcher.leafContexts));@@ -121,40 +112,14 @@ public class TestMatchesIterator extends LuceneTestCase {MatchesIterator it = matches.getMatches(field);if (expected[i].length == 1) {assertNull(it);- continue;+ return;}checkFieldMatches(it, expected[i]);checkFieldMatches(matches.getMatches(field), expected[i]); // test multiple calls}}- private void checkLabelCount(Query q, String field, int[] 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 to get matches on document " + i, 0, expected[i]);- continue;- }- MatchesIterator it = matches.getMatches(field);- if (expected[i] == 0) {- assertNull(it);- continue;- }- else {- assertNotNull(it);- }- IdentityHashMap<Object, Integer> labels = new IdentityHashMap<>();- while (it.next()) {- labels.put(it.label(), 1);- }- assertEquals(expected[i], labels.size());- }- }-- private void checkFieldMatches(MatchesIterator it, int[] expected) throws IOException {+ void checkFieldMatches(MatchesIterator it, int[] expected) throws IOException {int pos = 1;while (it.next()) {//System.out.println(expected[i][pos] + "->" + expected[i][pos + 1] + "[" + expected[i][pos + 2] + "->" + expected[i][pos + 3] + "]");@@ -167,7 +132,7 @@ public class TestMatchesIterator extends LuceneTestCase {assertEquals(expected.length, pos);}- private void checkNoPositionsMatches(Query q, String field, boolean[] expected) throws IOException {+ void checkNoPositionsMatches(Query q, String field, boolean[] 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));@@ -183,90 +148,8 @@ public class TestMatchesIterator extends LuceneTestCase {}}- private void checkTermMatches(Query q, String field, TermMatch[][][] 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 (expected[i].length == 0) {- assertNull(it);- continue;- }- checkTerms(expected[i], it);- }- }-- private void checkTerms(TermMatch[][] expected, MatchesIterator it) throws IOException {- int upTo = 0;- while (it.next()) {- Set<TermMatch> expectedMatches = new HashSet<>(Arrays.asList(expected[upTo]));- MatchesIterator submatches = it.getSubMatches();- while (submatches.next()) {- TermMatch tm = new TermMatch(submatches.startPosition(), submatches.startOffset(), submatches.endOffset());- if (expectedMatches.remove(tm) == false) {- fail("Unexpected term match: " + tm);- }- }- if (expectedMatches.size() != 0) {- fail("Missing term matches: " + expectedMatches.stream().map(Object::toString).collect(Collectors.joining(", ")));- }- upTo++;- }- if (upTo < expected.length - 1) {- fail("Missing expected match");- }- }-- static class TermMatch {-- public final int position;-- public final int startOffset;-- public final int endOffset;-- public TermMatch(PostingsEnum pe, int position) throws IOException {- this.position = position;- this.startOffset = pe.startOffset();- this.endOffset = pe.endOffset();- }-- public TermMatch(int position, int startOffset, int endOffset) {- this.position = position;- this.startOffset = startOffset;- this.endOffset = endOffset;- }-- @Override- public boolean equals(Object o) {- if (this == o) return true;- if (o == null || getClass() != o.getClass()) return false;- TermMatch termMatch = (TermMatch) o;- return position == termMatch.position &&- startOffset == termMatch.startOffset &&- endOffset == termMatch.endOffset;- }-- @Override- public int hashCode() {- return Objects.hash(position, startOffset, endOffset);- }-- @Override- public String toString() {- return position + "[" + startOffset + "->" + endOffset + "]";- }- }-public void testTermQuery() throws IOException {- Term t = new Term(FIELD_WITH_OFFSETS, "w1");- Query q = new TermQuery(t);+ Query q = new TermQuery(new Term(FIELD_WITH_OFFSETS, "w1"));checkMatches(q, FIELD_WITH_OFFSETS, new int[][]{{ 0, 0, 0, 0, 2 },{ 1, 0, 0, 0, 2 },@@ -274,14 +157,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, 0, 2, 2, 2, 6, 8 },{ 4 }});- checkTermMatches(q, FIELD_WITH_OFFSETS, new TermMatch[][][]{- { { new TermMatch(0, 0, 2) } },- { { new TermMatch(0, 0, 2) } },- { { new TermMatch(0, 0, 2) } },- { { new TermMatch(0, 0, 2) }, { new TermMatch(2, 6, 8) } },- {}- });- checkLabelCount(q, FIELD_WITH_OFFSETS, new int[]{ 1, 1, 1, 1, 0, 0 });}public void testTermQueryNoStoredOffsets() throws IOException {@@ -316,7 +191,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, 0, 2, 2, 2, 6, 8, 5, 5, 15, 17 },{ 4 }});- checkLabelCount(q, FIELD_WITH_OFFSETS, new int[]{ 2, 2, 1, 2, 0, 0 });}public void testDisjunctionNoPositions() throws IOException {@@ -341,7 +215,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, 0, 2, 2, 2, 6, 8, 5, 5, 15, 17 },{ 4 }});- checkLabelCount(q, FIELD_WITH_OFFSETS, new int[]{ 2, 2, 0, 2, 0, 0 });}public void testReqOptNoPositions() throws IOException {@@ -375,7 +248,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, 0, 2, 2, 2, 6, 8, 3, 3, 9, 11, 5, 5, 15, 17 },{ 4 }});- checkLabelCount(q, FIELD_WITH_OFFSETS, new int[]{ 3, 1, 3, 3, 0, 0 });}public void testMinShouldMatchNoPositions() throws IOException {@@ -459,7 +331,6 @@ public class TestMatchesIterator extends LuceneTestCase {{ 3, 0, 0, 0, 2, 1, 1, 3, 5, 2, 2, 6, 8, 4, 4, 12, 14 },{ 4 }});- checkLabelCount(rq, FIELD_WITH_OFFSETS, new int[]{ 2, 2, 2, 2, 0 });}@@ -521,55 +392,12 @@ public class TestMatchesIterator extends LuceneTestCase {// 0 1 2 3 4 5 6 7// "a phrase sentence with many phrase sentence iterations of a phrase sentence",- public void testSloppyPhraseQueryWithRepeats() throws IOException {- Term p = new Term(FIELD_WITH_OFFSETS, "phrase");- Term s = new Term(FIELD_WITH_OFFSETS, "sentence");- PhraseQuery pq = new PhraseQuery(10, FIELD_WITH_OFFSETS, "phrase", "sentence", "sentence");- checkMatches(pq, FIELD_WITH_OFFSETS, new int[][]{- { 0 }, { 1 }, { 2 }, { 3 },- { 4, 1, 6, 2, 43, 2, 11, 9, 75, 5, 11, 28, 75, 6, 11, 35, 75 }- });- checkLabelCount(pq, FIELD_WITH_OFFSETS, new int[]{ 0, 0, 0, 0, 1 });- checkTermMatches(pq, FIELD_WITH_OFFSETS, new TermMatch[][][]{- {}, {}, {}, {},- { {- new TermMatch(1, 2, 8),- new TermMatch(2, 9, 17),- new TermMatch(6, 35, 43)- }, {- new TermMatch(5, 28, 34),- new TermMatch(2, 9, 17),- new TermMatch(11, 67, 75)- }, {- new TermMatch(5, 28, 34),- new TermMatch(6, 35, 43),- new TermMatch(11, 67, 75)- }, {- new TermMatch(10, 60, 66),- new TermMatch(6, 35, 43),- new TermMatch(11, 67, 75)- } }- });- }-public void testSloppyPhraseQuery() throws IOException {- Term a = new Term(FIELD_WITH_OFFSETS, "a");- Term s = new Term(FIELD_WITH_OFFSETS, "sentence");PhraseQuery pq = new PhraseQuery(4, FIELD_WITH_OFFSETS, "a", "sentence");checkMatches(pq, FIELD_WITH_OFFSETS, new int[][]{{ 0 }, { 1 }, { 2 }, { 3 },{ 4, 0, 2, 0, 17, 6, 9, 35, 59, 9, 11, 58, 75 }});- checkTermMatches(pq, FIELD_WITH_OFFSETS, new TermMatch[][][]{- {}, {}, {}, {},- { {- new TermMatch(0, 0, 1), new TermMatch(2, 9, 17)- }, {- new TermMatch(9, 58, 59), new TermMatch(6, 35, 43)- }, {- new TermMatch(9, 58, 59), new TermMatch(11, 67, 75)- } }- });}public void testExactPhraseQuery() throws IOException {@@ -579,57 +407,28 @@ public class TestMatchesIterator extends LuceneTestCase {{ 4, 1, 2, 2, 17, 5, 6, 28, 43, 10, 11, 60, 75 }});- Term a = new Term(FIELD_WITH_OFFSETS, "a");- Term s = new Term(FIELD_WITH_OFFSETS, "sentence");PhraseQuery pq2 = new PhraseQuery.Builder()- .add(a)- .add(s, 2)+ .add(new Term(FIELD_WITH_OFFSETS, "a"))+ .add(new Term(FIELD_WITH_OFFSETS, "sentence"), 2).build();checkMatches(pq2, FIELD_WITH_OFFSETS, new int[][]{{ 0 }, { 1 }, { 2 }, { 3 },{ 4, 0, 2, 0, 17, 9, 11, 58, 75 }});- checkTermMatches(pq2, FIELD_WITH_OFFSETS, new TermMatch[][][]{- {}, {}, {}, {},- { {- new TermMatch(0, 0, 1), new TermMatch(2, 9, 17)- }, {- new TermMatch(9, 58, 59), new TermMatch(11, 67, 75)- } }- });}// 0 1 2 3 4 5 6 7// "a phrase sentence with many phrase sentence iterations of a phrase sentence",public void testSloppyMultiPhraseQuery() throws IOException {- Term p = new Term(FIELD_WITH_OFFSETS, "phrase");- Term s = new Term(FIELD_WITH_OFFSETS, "sentence");- Term i = new Term(FIELD_WITH_OFFSETS, "iterations");MultiPhraseQuery mpq = new MultiPhraseQuery.Builder()- .add(p)- .add(new Term[]{ s, i })+ .add(new Term(FIELD_WITH_OFFSETS, "phrase"))+ .add(new Term[]{ new Term(FIELD_WITH_OFFSETS, "sentence"), new Term(FIELD_WITH_OFFSETS, "iterations") }).setSlop(4).build();checkMatches(mpq, FIELD_WITH_OFFSETS, new int[][]{{ 0 }, { 1 }, { 2 }, { 3 },- { 4, 1, 2, 2, 17, 5, 6, 28, 43, 5, 7, 28, 54, 10, 11, 60, 75 }- });- checkTermMatches(mpq, FIELD_WITH_OFFSETS, new TermMatch[][][]{- {}, {}, {}, {},- { {- new TermMatch(1, 2, 8),- new TermMatch(2, 9, 17)- }, {- new TermMatch(5, 28, 34),- new TermMatch(6, 35, 43)- }, {- new TermMatch(5, 28, 34),- new TermMatch(7, 44, 54)- }, {- new TermMatch(10, 60, 66),- new TermMatch(11, 67, 75)- } }+ { 4, 1, 2, 2, 17, 5, 7, 28, 54, 5, 7, 28, 54, 10, 11, 60, 75 }});}@@ -653,35 +452,4 @@ public class TestMatchesIterator extends LuceneTestCase {});}- // 0 1 2 3 4 5 6 7- // "a phrase sentence with many phrase sentence iterations of a phrase sentence",-- public void testSpanQuery() throws IOException {- SpanQuery subq = SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)- .addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "with")))- .addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "many")))- .build();- Query q = SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)- .addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "sentence")))- .addClause(new SpanOrQuery(subq, new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "iterations"))))- .build();- checkMatches(q, FIELD_WITH_OFFSETS, new int[][]{- { 0 }, { 1 }, { 2 }, { 3 },- { 4, 2, 4, 9, 27, 6, 7, 35, 54 }- });- checkLabelCount(q, FIELD_WITH_OFFSETS, new int[]{ 0, 0, 0, 0, 1 });- checkTermMatches(q, FIELD_WITH_OFFSETS, new TermMatch[][][]{- {}, {}, {}, {},- {- {- new TermMatch(2, 9, 17),- new TermMatch(3, 18, 22),- new TermMatch(4, 23, 27)- }, {- new TermMatch(6, 35, 43), new TermMatch(7, 44, 54)- }- }- });- }-}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 5f7e3070040..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@@ -67,15 +67,4 @@ class AssertingMatchesIterator implements MatchesIterator {return in.endOffset();}- @Override- public MatchesIterator getSubMatches() throws IOException {- assert state == State.ITERATING : state;- return in.getSubMatches();- }-- @Override- public Object label() {- assert state == State.ITERATING : state;- return in.label();- }}