diff --git a/CHANGELOG.md b/CHANGELOG.md index b4f8e1207..b1ec1aeb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] ### Added +- Adds the `Routing` field in SearchHit interface. ([#516](https://github.com/opensearch-project/opensearch-go/pull/516)) ### Changed diff --git a/opensearchapi/api_search.go b/opensearchapi/api_search.go index c4551dbea..a378562f9 100644 --- a/opensearchapi/api_search.go +++ b/opensearchapi/api_search.go @@ -89,6 +89,7 @@ func (r SearchResp) Inspect() Inspect { type SearchHit struct { Index string `json:"_index"` ID string `json:"_id"` + Routing string `json:"_routing"` Score float32 `json:"_score"` Source json.RawMessage `json:"_source"` Fields json.RawMessage `json:"fields"` diff --git a/opensearchapi/api_search_test.go b/opensearchapi/api_search_test.go index 75c4dbe00..6cda9babc 100644 --- a/opensearchapi/api_search_test.go +++ b/opensearchapi/api_search_test.go @@ -30,9 +30,10 @@ func TestSearch(t *testing.T) { _, err = client.Index( nil, opensearchapi.IndexReq{ - Index: index, - Body: strings.NewReader(`{"foo": "bar"}`), - Params: opensearchapi.IndexParams{Refresh: "true"}, + DocumentID: "foo", + Index: index, + Body: strings.NewReader(`{"foo": "bar"}`), + Params: opensearchapi.IndexParams{Refresh: "true", Routing: "foo"}, }, ) require.Nil(t, err) @@ -109,4 +110,22 @@ func TestSearch(t *testing.T) { require.NotNil(t, httpReq) assert.Equal(t, fmt.Sprintf("/%s/_search", index), httpReq.URL.Path) }) + t.Run("request to retrieve response with routing key", func(t *testing.T) { + resp, err := client.Search(nil, &opensearchapi.SearchReq{Indices: []string{index}, Body: strings.NewReader(`{ + "query": { + "match": { + "foo": "bar" + } + }, + "fields": [ + "foo" + ], + "_source": false + }`)}) + require.Nil(t, err) + assert.NotEmpty(t, resp.Hits.Hits) + assert.NotEmpty(t, resp.Hits.Hits[0].Fields) + assert.NotEmpty(t, resp.Hits.Hits[0].Routing) + assert.Equal(t, "foo", resp.Hits.Hits[0].Routing) + }) }