Skip to content

Commit c0092b8

Browse files
committed
Higlighters: Fix MultiPhrasePrefixQuery rewriting (#25103)
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
1 parent a67c8c0 commit c0092b8

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

core/src/main/java/org/apache/lucene/search/uhighlight/CustomUnifiedHighlighter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,16 @@ private Collection<Query> rewriteCustomQuery(Query query) {
182182
positionSpanQueries[i] = innerQueries[0];
183183
}
184184
}
185+
186+
if (positionSpanQueries.length == 1) {
187+
return Collections.singletonList(positionSpanQueries[0]);
188+
}
185189
// sum position increments beyond 1
186190
int positionGaps = 0;
187191
if (positions.length >= 2) {
188192
// positions are in increasing order. max(0,...) is just a safeguard.
189193
positionGaps = Math.max(0, positions[positions.length - 1] - positions[0] - positions.length + 1);
190194
}
191-
192195
//if original slop is 0 then require inOrder
193196
boolean inorder = (mpq.getSlop() == 0);
194197
return Collections.singletonList(new SpanNearQuery(positionSpanQueries,

core/src/test/java/org/apache/lucene/search/uhighlight/CustomUnifiedHighlighterTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ public void testNoMatchSize() throws Exception {
121121
BreakIterator.getSentenceInstance(Locale.ROOT), 100, inputs);
122122
}
123123

124+
public void testMultiPhrasePrefixQuerySingleTerm() throws Exception {
125+
final String[] inputs = {
126+
"The quick brown fox."
127+
};
128+
final String[] outputs = {
129+
"The quick <b>brown</b> fox."
130+
};
131+
MultiPhrasePrefixQuery query = new MultiPhrasePrefixQuery();
132+
query.add(new Term("text", "bro"));
133+
assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT,
134+
BreakIterator.getSentenceInstance(Locale.ROOT), 0, outputs);
135+
}
136+
124137
public void testMultiPhrasePrefixQuery() throws Exception {
125138
final String[] inputs = {
126139
"The quick brown fox."

core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1638,11 +1638,20 @@ public void testPhrasePrefix() throws IOException {
16381638

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

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

1646+
assertHighlight(searchResponse, 0, "field0", 0, 1, equalTo("The quick <x>brown</x> fox jumps over the lazy dog"));
1647+
1648+
1649+
source = searchSource()
1650+
.query(matchPhrasePrefixQuery("field0", "quick bro"))
1651+
.highlighter(highlight().field("field0").order("score").preTags("<x>").postTags("</x>").highlighterType(type));
1652+
1653+
searchResponse = client().search(searchRequest("test").source(source)).actionGet();
1654+
16461655
assertHighlight(searchResponse, 0, "field0", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog"));
16471656

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

0 commit comments

Comments
 (0)