Skip to content

Commit

Permalink
Add Highlight field to SearchHit struct
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Holdaway <[email protected]>
  • Loading branch information
tim-holdaway-clever committed Jan 16, 2025
1 parent f81866a commit f7cd93c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions opensearchapi/api_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type SearchHit struct {
Explanation *DocumentExplainDetails `json:"_explanation"`
SeqNo *int `json:"_seq_no"`
PrimaryTerm *int `json:"_primary_term"`
Highlight map[string][]string `json:"highlight"`
}

// Suggest is a sub type of SearchResp containing information of the suggest field
Expand Down
24 changes: 24 additions & 0 deletions opensearchapi/api_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,28 @@ func TestSearch(t *testing.T) {
require.Nil(t, err)
assert.NotEmpty(t, resp.Suggest)
})

t.Run("request with highlight", func(t *testing.T) {
resp, err := client.Search(
nil,
&opensearchapi.SearchReq{
Indices: []string{index},
Body: strings.NewReader(`{
"query": {
"match": {
"foo": "bar"
}
},
"highlight": {
"fields": {
"foo": {}
}
}
}`),
},
)
require.Nil(t, err)
assert.NotEmpty(t, resp.Hits.Hits)
assert.Equal(t, map[string][]string{"foo": []string{"<em>bar</em>"}}, resp.Hits.Hits[0].Highlight)
})
}

0 comments on commit f7cd93c

Please sign in to comment.