Skip to content

Commit

Permalink
Add missing _routing field in SearchHit interface (#516)
Browse files Browse the repository at this point in the history
* Add missing _routing field in SearchHit interface

Signed-off-by: Tushar <[email protected]>

* Added change in Changelog.md

Signed-off-by: Tushar <[email protected]>

* Added test case to verify routing field in search response

Signed-off-by: Tushar <[email protected]>

* added routing test in existing Test method, fixed changelog.md

Signed-off-by: Tushar <[email protected]>

---------

Signed-off-by: Tushar <[email protected]>
Signed-off-by: tushar2404 <[email protected]>
  • Loading branch information
tushar2404 authored Apr 23, 2024
1 parent 0f7eb6f commit b2d9c64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

- Adds the `SearchPipelines` field to `SearchParams` ([#532](https://github.com/opensearch-project/opensearch-go/pull/532))

Expand Down
1 change: 1 addition & 0 deletions opensearchapi/api_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
25 changes: 22 additions & 3 deletions opensearchapi/api_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
}

0 comments on commit b2d9c64

Please sign in to comment.