Skip to content

Commit

Permalink
Higlighters: Fix MultiPhrasePrefixQuery rewriting (#25103)
Browse files Browse the repository at this point in the history
The unified highlighter rewrites MultiPhrasePrefixQuery to SpanNearQuer even when there is a single term in the phrase.
Though SpanNearQuery throws an exception when the number of clauses is less than 2.
This change returns a simple PrefixQuery when there is a single term and builds the SpanNearQuery otherwise.

Relates #25088
  • Loading branch information
jimczi committed Jun 7, 2017
1 parent a67c8c0 commit c0092b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Expand Up @@ -182,13 +182,16 @@ private Collection<Query> rewriteCustomQuery(Query query) {
positionSpanQueries[i] = innerQueries[0];
}
}

if (positionSpanQueries.length == 1) {
return Collections.singletonList(positionSpanQueries[0]);
}
// sum position increments beyond 1
int positionGaps = 0;
if (positions.length >= 2) {
// positions are in increasing order. max(0,...) is just a safeguard.
positionGaps = Math.max(0, positions[positions.length - 1] - positions[0] - positions.length + 1);
}

//if original slop is 0 then require inOrder
boolean inorder = (mpq.getSlop() == 0);
return Collections.singletonList(new SpanNearQuery(positionSpanQueries,
Expand Down
Expand Up @@ -121,6 +121,19 @@ public void testNoMatchSize() throws Exception {
BreakIterator.getSentenceInstance(Locale.ROOT), 100, inputs);
}

public void testMultiPhrasePrefixQuerySingleTerm() throws Exception {
final String[] inputs = {
"The quick brown fox."
};
final String[] outputs = {
"The quick <b>brown</b> fox."
};
MultiPhrasePrefixQuery query = new MultiPhrasePrefixQuery();
query.add(new Term("text", "bro"));
assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT,
BreakIterator.getSentenceInstance(Locale.ROOT), 0, outputs);
}

public void testMultiPhrasePrefixQuery() throws Exception {
final String[] inputs = {
"The quick brown fox."
Expand Down
Expand Up @@ -1638,11 +1638,20 @@ public void testPhrasePrefix() throws IOException {

for (String type : UNIFIED_AND_NULL) {
SearchSourceBuilder source = searchSource()
.query(matchPhrasePrefixQuery("field0", "quick bro"))
.query(matchPhrasePrefixQuery("field0", "bro"))
.highlighter(highlight().field("field0").order("score").preTags("<x>").postTags("</x>").highlighterType(type));

SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet();

assertHighlight(searchResponse, 0, "field0", 0, 1, equalTo("The quick <x>brown</x> fox jumps over the lazy dog"));


source = searchSource()
.query(matchPhrasePrefixQuery("field0", "quick bro"))
.highlighter(highlight().field("field0").order("score").preTags("<x>").postTags("</x>").highlighterType(type));

searchResponse = client().search(searchRequest("test").source(source)).actionGet();

assertHighlight(searchResponse, 0, "field0", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog"));

logger.info("--> highlighting and searching on field1");
Expand Down

0 comments on commit c0092b8

Please sign in to comment.