mirror of https://github.com/apache/lucene.git
LUCENE-7481: Fix SpanPayloadCheckQuery rewrite
This commit is contained in:
parent
28aeeadb5f
commit
72e616dd52
|
@ -69,6 +69,10 @@ public class SpanPayloadCheckQuery extends SpanQuery {
|
|||
|
||||
@Override
|
||||
public Query rewrite(IndexReader reader) throws IOException {
|
||||
Query matchRewritten = match.rewrite(reader);
|
||||
if (match != matchRewritten && matchRewritten instanceof SpanQuery) {
|
||||
return new SpanPayloadCheckQuery((SpanQuery)matchRewritten, payloadToMatch);
|
||||
}
|
||||
return super.rewrite(reader);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@ import org.apache.lucene.index.Term;
|
|||
import org.apache.lucene.search.CheckHits;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanPositionRangeQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
|
@ -186,7 +188,6 @@ public class TestPayloadCheckQuery extends LuceneTestCase {
|
|||
payloads.add(pay4);
|
||||
query = new SpanPayloadCheckQuery(oneThousHunThree, payloads);
|
||||
checkHits(query, new int[]{1103, 1203,1303,1403,1503,1603,1703,1803,1903});
|
||||
|
||||
}
|
||||
|
||||
public void testEquality() {
|
||||
|
@ -208,4 +209,24 @@ public class TestPayloadCheckQuery extends LuceneTestCase {
|
|||
assertFalse(query2.equals(query4));
|
||||
assertFalse(query3.equals(query4));
|
||||
}
|
||||
|
||||
public void testRewrite() throws IOException {
|
||||
SpanMultiTermQueryWrapper fiv = new SpanMultiTermQueryWrapper(new WildcardQuery(new Term("field", "fiv*")));
|
||||
SpanMultiTermQueryWrapper hund = new SpanMultiTermQueryWrapper(new WildcardQuery(new Term("field", "hund*")));
|
||||
SpanMultiTermQueryWrapper twent = new SpanMultiTermQueryWrapper(new WildcardQuery(new Term("field", "twent*")));
|
||||
SpanMultiTermQueryWrapper nin = new SpanMultiTermQueryWrapper(new WildcardQuery(new Term("field", "nin*")));
|
||||
|
||||
SpanNearQuery sq = new SpanNearQuery(new SpanQuery[] {fiv, hund, twent, nin}, 0, true);
|
||||
|
||||
List<BytesRef> payloads = new ArrayList<>();
|
||||
payloads.add(new BytesRef("pos: 0"));
|
||||
payloads.add(new BytesRef("pos: 1"));
|
||||
payloads.add(new BytesRef("pos: 2"));
|
||||
payloads.add(new BytesRef("pos: 3"));
|
||||
|
||||
SpanPayloadCheckQuery query = new SpanPayloadCheckQuery(sq, payloads);
|
||||
|
||||
// if query wasn't rewritten properly, the query would have failed with "Rewrite first!"
|
||||
checkHits(query, new int[]{529});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue