Skip to content

Commit 8c9554d

Browse files
authored
Add highlighting for match_only_text type (opensearch-project#17101) (opensearch-project#17214)
1 parent 93c072b commit 8c9554d

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
116116
- Fix GRPC AUX_TRANSPORT_PORT and SETTING_GRPC_PORT settings and remove lingering HTTP terminology ([#17037](https://github.com/opensearch-project/OpenSearch/pull/17037))
117117
- [WLM] Fix the QueryGroupTask logging bug ([#17169](https://github.com/opensearch-project/OpenSearch/pull/17169))
118118
- Use OpenSearch version to deserialize remote custom metadata([#16494](https://github.com/opensearch-project/OpenSearch/pull/16494))
119+
- Add highlighting for wildcard search on `match_only_text` field ([#17101](https://github.com/opensearch-project/OpenSearch/pull/17101))
119120
- Fix the failing CI's with `Failed to load eclipse jdt formatter` error ([#17172](https://github.com/opensearch-project/OpenSearch/pull/17172))
120121
- Fix AutoDateHistogramAggregator rounding assertion failure ([#17023](https://github.com/opensearch-project/OpenSearch/pull/17023))
121122

modules/analysis-common/src/yamlRestTest/resources/rest-api-spec/test/search.query/11_match_field_match_only_text.yml

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# integration tests for queries with specific analysis chains
2-
2+
---
33
"match query with stacked stems":
44
- skip:
55
version: " - 2.11.99"
@@ -68,3 +68,80 @@
6868
query: fox runs
6969
operator: AND
7070
- match: {hits.total: 2}
71+
72+
---
73+
"wildcard highlighting on match_only_text":
74+
- skip:
75+
version: " - 2.99.99"
76+
reason: "wildcard highlighting on match_only_text type was added in 2.19"
77+
- do:
78+
indices.create:
79+
index: test
80+
body:
81+
settings:
82+
number_of_shards: 1
83+
number_of_replicas: 1
84+
analysis:
85+
analyzer:
86+
index:
87+
tokenizer: standard
88+
filter: [lowercase]
89+
search:
90+
rest_total_hits_as_int: true
91+
tokenizer: standard
92+
filter: [lowercase, keyword_repeat, porter_stem, unique_stem]
93+
filter:
94+
unique_stem:
95+
type: unique
96+
only_on_same_position: true
97+
mappings:
98+
properties:
99+
text:
100+
type: match_only_text
101+
analyzer: index
102+
search_analyzer: search
103+
104+
- do:
105+
index:
106+
index: test
107+
id: 1
108+
body: { "text": "the fox runs across the street" }
109+
refresh: true
110+
111+
- do:
112+
search:
113+
rest_total_hits_as_int: true
114+
body:
115+
query:
116+
match:
117+
text:
118+
query: fox runs
119+
operator: AND
120+
highlight:
121+
fields:
122+
- text: {}
123+
- match: {hits.total: 1}
124+
- match: {hits.hits.0.highlight.text.0: "the <em>fox</em> <em>runs</em> across the street"}
125+
126+
- do:
127+
index:
128+
index: test
129+
id: 2
130+
body: { "text": "run fox run" }
131+
refresh: true
132+
133+
- do:
134+
search:
135+
rest_total_hits_as_int: true
136+
body:
137+
query:
138+
match:
139+
text:
140+
query: fox runs
141+
operator: AND
142+
highlight:
143+
fields:
144+
- text: {}
145+
- match: {hits.total: 2}
146+
- match: {hits.hits.0.highlight.text.0: "the <em>fox</em> <em>runs</em> across the street"}
147+
- match: {hits.hits.1.highlight.text.0: "<em>run</em> <em>fox</em> <em>run</em>"}

server/src/main/java/org/opensearch/search/fetch/subphase/highlight/HighlightPhase.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.opensearch.common.regex.Regex;
3838
import org.opensearch.index.mapper.KeywordFieldMapper;
3939
import org.opensearch.index.mapper.MappedFieldType;
40+
import org.opensearch.index.mapper.MatchOnlyTextFieldMapper;
4041
import org.opensearch.index.mapper.SourceFieldMapper;
4142
import org.opensearch.index.mapper.TextFieldMapper;
4243
import org.opensearch.search.fetch.FetchContext;
@@ -152,7 +153,8 @@ private Map<String, Function<HitContext, FieldHighlightContext>> contextBuilders
152153
continue;
153154
}
154155

155-
// We should prevent highlighting if a field is anything but a text or keyword field.
156+
// We should prevent highlighting if a field is anything but a text, match_only_text
157+
// or keyword field.
156158
// However, someone might implement a custom field type that has text and still want to
157159
// highlight on that. We cannot know in advance if the highlighter will be able to
158160
// highlight such a field and so we do the following:
@@ -162,7 +164,8 @@ private Map<String, Function<HitContext, FieldHighlightContext>> contextBuilders
162164
// what they were doing and try to highlight anyway.
163165
if (fieldNameContainsWildcards) {
164166
if (fieldType.typeName().equals(TextFieldMapper.CONTENT_TYPE) == false
165-
&& fieldType.typeName().equals(KeywordFieldMapper.CONTENT_TYPE) == false) {
167+
&& fieldType.typeName().equals(KeywordFieldMapper.CONTENT_TYPE) == false
168+
&& fieldType.typeName().equals(MatchOnlyTextFieldMapper.CONTENT_TYPE) == false) {
166169
continue;
167170
}
168171
if (highlighter.canHighlight(fieldType) == false) {

0 commit comments

Comments
 (0)