Skip to content

Commit 739a4b9

Browse files
committed
Fix Unified highlighter for nested fields when using matchPhrasePrefixQuery
Signed-off-by: sjs004 <[email protected]>
1 parent 83b2a6d commit 739a4b9

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8787
- Fix cardinality agg pruning optimization by self collecting ([#19473](https://github.com/opensearch-project/OpenSearch/pull/19473))
8888
- Implement SslHandler retrieval logic for transport-reactor-netty4 plugin ([#19458](https://github.com/opensearch-project/OpenSearch/pull/19458))
8989
- Cache serialised cluster state based on cluster state version and node version.([#19307](https://github.com/opensearch-project/OpenSearch/pull/19307))
90-
9190
- Handle negative search request nodes stats ([#19340](https://github.com/opensearch-project/OpenSearch/pull/19340))
91+
- Fix Unified highlighter for nested fields when using matchPhrasePrefixQuery ([#19442](https://github.com/opensearch-project/OpenSearch/pull/19442))
9292

9393
### Dependencies
9494
- Bump `com.gradleup.shadow:shadow-gradle-plugin` from 8.3.5 to 8.3.9 ([#19400](https://github.com/opensearch-project/OpenSearch/pull/19400))

server/src/internalClusterTest/java/org/opensearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
import static org.opensearch.index.query.QueryBuilders.constantScoreQuery;
100100
import static org.opensearch.index.query.QueryBuilders.existsQuery;
101101
import static org.opensearch.index.query.QueryBuilders.fuzzyQuery;
102+
import static org.opensearch.index.query.QueryBuilders.matchPhrasePrefixQuery;
102103
import static org.opensearch.index.query.QueryBuilders.matchPhraseQuery;
103104
import static org.opensearch.index.query.QueryBuilders.matchQuery;
104105
import static org.opensearch.index.query.QueryBuilders.multiMatchQuery;
@@ -3518,7 +3519,7 @@ public void testWithNestedQuery() throws Exception {
35183519
jsonBuilder().startObject()
35193520
.startArray("foo")
35203521
.startObject()
3521-
.field("text", "brown")
3522+
.field("text", "brown cat")
35223523
.endObject()
35233524
.startObject()
35243525
.field("text", "cow")
@@ -3539,7 +3540,7 @@ public void testWithNestedQuery() throws Exception {
35393540
assertHitCount(searchResponse, 1);
35403541
HighlightField field = searchResponse.getHits().getAt(0).getHighlightFields().get("foo.text");
35413542
assertThat(field.getFragments().length, equalTo(2));
3542-
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
3543+
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em> cat"));
35433544
assertThat(field.getFragments()[1].string(), equalTo("<em>cow</em>"));
35443545

35453546
searchResponse = client().prepareSearch()
@@ -3549,16 +3550,25 @@ public void testWithNestedQuery() throws Exception {
35493550
assertHitCount(searchResponse, 1);
35503551
field = searchResponse.getHits().getAt(0).getHighlightFields().get("foo.text");
35513552
assertThat(field.getFragments().length, equalTo(1));
3552-
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
3553+
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em> cat"));
35533554

35543555
searchResponse = client().prepareSearch()
3555-
.setQuery(nestedQuery("foo", prefixQuery("foo.text", "bro"), ScoreMode.None))
3556-
.highlighter(new HighlightBuilder().field(new Field("foo.text").highlighterType("plain")))
3556+
.setQuery(nestedQuery("foo", matchPhraseQuery("foo.text", "brown cat"), ScoreMode.None))
3557+
.highlighter(new HighlightBuilder().field(new Field("foo.text").highlighterType(type)))
35573558
.get();
35583559
assertHitCount(searchResponse, 1);
35593560
field = searchResponse.getHits().getAt(0).getHighlightFields().get("foo.text");
35603561
assertThat(field.getFragments().length, equalTo(1));
3561-
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
3562+
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em> <em>cat</em>"));
3563+
3564+
searchResponse = client().prepareSearch()
3565+
.setQuery(nestedQuery("foo", matchPhrasePrefixQuery("foo.text", "bro"), ScoreMode.None))
3566+
.highlighter(new HighlightBuilder().field(new Field("foo.text").highlighterType(type)))
3567+
.get();
3568+
assertHitCount(searchResponse, 1);
3569+
field = searchResponse.getHits().getAt(0).getHighlightFields().get("foo.text");
3570+
assertThat(field.getFragments().length, equalTo(1));
3571+
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em> cat"));
35623572
}
35633573

35643574
// For unified and fvh highlighters we just check that the nested query is correctly extracted

server/src/main/java/org/opensearch/lucene/search/uhighlight/CustomUnifiedHighlighter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.opensearch.common.Nullable;
5353
import org.opensearch.common.lucene.search.MultiPhrasePrefixQuery;
5454
import org.opensearch.index.IndexSettings;
55+
import org.opensearch.index.search.OpenSearchToParentBlockJoinQuery;
5556

5657
import java.io.IOException;
5758
import java.text.BreakIterator;
@@ -262,6 +263,8 @@ private Collection<Query> rewriteCustomQuery(Query query) {
262263
// if original slop is 0 then require inOrder
263264
boolean inorder = (mpq.getSlop() == 0);
264265
return Collections.singletonList(new SpanNearQuery(positionSpanQueries, mpq.getSlop() + positionGaps, inorder));
266+
} else if (query instanceof OpenSearchToParentBlockJoinQuery) {
267+
return Collections.singletonList(((OpenSearchToParentBlockJoinQuery) query).getChildQuery());
265268
} else {
266269
return null;
267270
}

0 commit comments

Comments
 (0)