Skip to content

Commit f7cd93c

Browse files
Add Highlight field to SearchHit struct
Signed-off-by: Tim Holdaway <timothy.holdaway@clever.com>
1 parent f81866a commit f7cd93c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

opensearchapi/api_search.go

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type SearchHit struct {
9999
Explanation *DocumentExplainDetails `json:"_explanation"`
100100
SeqNo *int `json:"_seq_no"`
101101
PrimaryTerm *int `json:"_primary_term"`
102+
Highlight map[string][]string `json:"highlight"`
102103
}
103104

104105
// Suggest is a sub type of SearchResp containing information of the suggest field

opensearchapi/api_search_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,28 @@ func TestSearch(t *testing.T) {
181181
require.Nil(t, err)
182182
assert.NotEmpty(t, resp.Suggest)
183183
})
184+
185+
t.Run("request with highlight", func(t *testing.T) {
186+
resp, err := client.Search(
187+
nil,
188+
&opensearchapi.SearchReq{
189+
Indices: []string{index},
190+
Body: strings.NewReader(`{
191+
"query": {
192+
"match": {
193+
"foo": "bar"
194+
}
195+
},
196+
"highlight": {
197+
"fields": {
198+
"foo": {}
199+
}
200+
}
201+
}`),
202+
},
203+
)
204+
require.Nil(t, err)
205+
assert.NotEmpty(t, resp.Hits.Hits)
206+
assert.Equal(t, map[string][]string{"foo": []string{"<em>bar</em>"}}, resp.Hits.Hits[0].Highlight)
207+
})
184208
}

0 commit comments

Comments
 (0)