LUCENE-9031: Implement MatchesIterator.getQuery().

This commit is contained in:
Mikhail Khludnev 2019-11-14 16:56:26 +03:00
parent 340b238f1c
commit f4647f76a0
7 changed files with 1036 additions and 9 deletions

View File

@ -66,6 +66,8 @@ Bug Fixes
* LUCENE-9054: Fix reproduceJenkinsFailures.py to not overwrite junit XML files when retrying (hossman) * LUCENE-9054: Fix reproduceJenkinsFailures.py to not overwrite junit XML files when retrying (hossman)
* LUCENE-9031: UnsupportedOperationException on MatchesIterator.getQuery() (Alan Woodward, Mikhail Khludnev)
Other Other
* LUCENE-8979: Code Cleanup: Use entryset for map iteration wherever possible. - Part 2 (Koen De Groote) * LUCENE-8979: Code Cleanup: Use entryset for map iteration wherever possible. - Part 2 (Koen De Groote)

View File

@ -27,7 +27,8 @@ import org.apache.lucene.util.ArrayUtil;
class CachingMatchesIterator extends FilterMatchesIterator { class CachingMatchesIterator extends FilterMatchesIterator {
private boolean positioned = false; private boolean positioned = false;
private int[] posAndOffsets = new int[16]; private int[] posAndOffsets = new int[4*4];
private Query[] matchingQueries = new Query[4];
private int count = 0; private int count = 0;
CachingMatchesIterator(MatchesIterator in) { CachingMatchesIterator(MatchesIterator in) {
@ -43,16 +44,19 @@ class CachingMatchesIterator extends FilterMatchesIterator {
posAndOffsets[1] = in.endPosition(); posAndOffsets[1] = in.endPosition();
posAndOffsets[2] = in.startOffset(); posAndOffsets[2] = in.startOffset();
posAndOffsets[3] = in.endOffset(); posAndOffsets[3] = in.endOffset();
matchingQueries [0] = in.getQuery();
} }
else { else {
while (mi.next()) { while (mi.next()) {
if (count * 4 >= posAndOffsets.length) { if (count * 4 >= posAndOffsets.length) {
posAndOffsets = ArrayUtil.grow(posAndOffsets, (count + 1) * 4); posAndOffsets = ArrayUtil.grow(posAndOffsets, (count + 1) * 4);
matchingQueries = ArrayUtil.grow(matchingQueries, (count + 1));
} }
posAndOffsets[count * 4] = mi.startPosition(); posAndOffsets[count * 4] = mi.startPosition();
posAndOffsets[count * 4 + 1] = mi.endPosition(); posAndOffsets[count * 4 + 1] = mi.endPosition();
posAndOffsets[count * 4 + 2] = mi.startOffset(); posAndOffsets[count * 4 + 2] = mi.startOffset();
posAndOffsets[count * 4 + 3] = mi.endOffset(); posAndOffsets[count * 4 + 3] = mi.endOffset();
matchingQueries[count] = mi.getQuery();
count++; count++;
} }
} }
@ -124,7 +128,7 @@ class CachingMatchesIterator extends FilterMatchesIterator {
@Override @Override
public Query getQuery() { public Query getQuery() {
throw new UnsupportedOperationException(); return matchingQueries[upto];
} }
}; };
} }

View File

@ -74,7 +74,7 @@ final class IntervalMatches {
@Override @Override
public Query getQuery() { public Query getQuery() {
throw new UnsupportedOperationException(); return source.getQuery();
} }
}; };
} }

View File

@ -83,7 +83,7 @@ class MultiTermIntervalsSource extends IntervalsSource {
BytesRef term; BytesRef term;
int count = 0; int count = 0;
while ((term = te.next()) != null) { while ((term = te.next()) != null) {
MatchesIterator mi = TermIntervalsSource.matches(te, doc); MatchesIterator mi = TermIntervalsSource.matches(te, doc, field);
if (mi != null) { if (mi != null) {
subMatches.add(mi); subMatches.add(mi);
if (count++ > maxExpansions) { if (count++ > maxExpansions) {

View File

@ -34,6 +34,7 @@ import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.MatchesIterator; import org.apache.lucene.search.MatchesIterator;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor; import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TwoPhaseIterator; import org.apache.lucene.search.TwoPhaseIterator;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
@ -149,10 +150,11 @@ class TermIntervalsSource extends IntervalsSource {
if (te.seekExact(term) == false) { if (te.seekExact(term) == false) {
return null; return null;
} }
return matches(te, doc); return matches(te, doc, field);
} }
static MatchesIterator matches(TermsEnum te, int doc) throws IOException { static MatchesIterator matches(TermsEnum te, int doc, String field) throws IOException {
TermQuery query = new TermQuery(new Term(field, te.term()));
PostingsEnum pe = te.postings(null, PostingsEnum.OFFSETS); PostingsEnum pe = te.postings(null, PostingsEnum.OFFSETS);
if (pe.advance(doc) != doc) { if (pe.advance(doc) != doc) {
return null; return null;
@ -200,7 +202,7 @@ class TermIntervalsSource extends IntervalsSource {
@Override @Override
public Query getQuery() { public Query getQuery() {
throw new UnsupportedOperationException(); return query;
} }
}; };
} }

View File

@ -46,6 +46,7 @@ import org.apache.lucene.search.MatchesIterator;
import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor; import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
@ -228,8 +229,12 @@ public class TestIntervals extends LuceneTestCase {
assertNull(getMatches(source, 2, "no_such_field")); assertNull(getMatches(source, 2, "no_such_field"));
MatchesIterator mi = getMatches(source, 2, "field1"); MatchesIterator mi = getMatches(source, 2, "field1");
assertMatch(mi, 1, 1, 6, 14); assertMatch(mi, 1, 1, 6, 14);
final TermQuery porridge = new TermQuery(new Term("field1","porridge"));
assertEquals(porridge, mi.getQuery());
assertMatch(mi, 4, 4, 27, 35); assertMatch(mi, 4, 4, 27, 35);
assertEquals(porridge, mi.getQuery());
assertMatch(mi, 7, 7, 47, 55); assertMatch(mi, 7, 7, 47, 55);
assertEquals(porridge, mi.getQuery());
assertFalse(mi.next()); assertFalse(mi.next());
assertEquals(1, source.minExtent()); assertEquals(1, source.minExtent());
@ -282,9 +287,18 @@ public class TestIntervals extends LuceneTestCase {
assertMatch(mi, 3, 4, 20, 34); assertMatch(mi, 3, 4, 20, 34);
MatchesIterator sub = mi.getSubMatches(); MatchesIterator sub = mi.getSubMatches();
assertMatch(sub, 3, 3, 20, 25); assertMatch(sub, 3, 3, 20, 25);
assertEquals(new TermQuery( new Term("field1", "pease")), sub.getQuery());
assertMatch(sub, 4, 4, 26, 34); assertMatch(sub, 4, 4, 26, 34);
assertEquals(new TermQuery( new Term("field1", "porridge")), sub.getQuery());
assertFalse(sub.next()); assertFalse(sub.next());
assertMatch(mi, 6, 7, 41, 55); assertMatch(mi, 6, 7, 41, 55);
sub = mi.getSubMatches();
assertTrue(sub.next());
assertEquals(new TermQuery( new Term("field1", "pease")), sub.getQuery());
assertTrue(sub.next());
assertEquals(new TermQuery( new Term("field1", "porridge")), sub.getQuery());
assertFalse(sub.next());
assertEquals(2, source.minExtent()); assertEquals(2, source.minExtent());
@ -334,8 +348,10 @@ public class TestIntervals extends LuceneTestCase {
assertNull(getMatches(source, 0, "field1")); assertNull(getMatches(source, 0, "field1"));
MatchesIterator mi = getMatches(source, 3, "field1"); MatchesIterator mi = getMatches(source, 3, "field1");
assertMatch(mi, 3, 3, 15, 18); assertMatch(mi, 3, 3, 15, 18);
assertEquals(new TermQuery(new Term("field1","hot")), mi.getQuery());
assertNull(mi.getSubMatches()); assertNull(mi.getSubMatches());
assertMatch(mi, 7, 7, 31, 36); assertMatch(mi, 7, 7, 31, 36);
assertEquals(new TermQuery(new Term("field1","pease")), mi.getQuery());
assertNull(mi.getSubMatches()); assertNull(mi.getSubMatches());
assertFalse(mi.next()); assertFalse(mi.next());
@ -354,7 +370,6 @@ public class TestIntervals extends LuceneTestCase {
{ 3, 8 }, { 3, 8 },
{}, {}, {}, {} {}, {}, {}, {}
}); });
assertEquals(2, source.minExtent()); assertEquals(2, source.minExtent());
checkVisits(source, 5, "alph", "sacred", "measureless"); checkVisits(source, 5, "alph", "sacred", "measureless");
@ -448,9 +463,13 @@ public class TestIntervals extends LuceneTestCase {
assertMatch(it, 6, 21, 41, 118); assertMatch(it, 6, 21, 41, 118);
MatchesIterator sub = it.getSubMatches(); MatchesIterator sub = it.getSubMatches();
assertMatch(sub, 6, 6, 41, 46); assertMatch(sub, 6, 6, 41, 46);
assertEquals(new TermQuery(new Term("field1", "pease")), sub.getQuery());
assertMatch(sub, 19, 19, 106, 110); assertMatch(sub, 19, 19, 106, 110);
assertEquals(new TermQuery(new Term("field1", "like")), sub.getQuery());
assertMatch(sub, 20, 20, 111, 113); assertMatch(sub, 20, 20, 111, 113);
assertEquals(new TermQuery(new Term("field1", "it")),sub.getQuery());
assertMatch(sub, 21, 21, 114, 118); assertMatch(sub, 21, 21, 114, 118);
assertEquals(new TermQuery(new Term("field1", "cold")),sub.getQuery());
assertFalse(sub.next()); assertFalse(sub.next());
assertFalse(it.next()); assertFalse(it.next());
assertEquals(4, source.minExtent()); assertEquals(4, source.minExtent());