Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing _routing field in SearchHit interface #516

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Adds cluster.get-certs to copy admin certs out of the container ([#507](https://github.com/opensearch-project/opensearch-go/pull/507))
- Adds the `Fields` field containing stored fields to the `DocumentGetResp` struct ([#526](https://github.com/opensearch-project/opensearch-go/pull/526))
- Adds ism plugin ([#524](https://github.com/opensearch-project/opensearch-go/pull/524))
- Adds the `Routing` field in SearchHit interface. ([#516](https://github.com/opensearch-project/opensearch-go/pull/516))
Jakob3xD marked this conversation as resolved.
Show resolved Hide resolved

### Changed
- Uses docker compose v2 instead of v1 ([#506](https://github.com/opensearch-project/opensearch-go/pull/506))
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
40 changes: 40 additions & 0 deletions opensearchapi/api_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,43 @@ func TestSearch(t *testing.T) {
assert.Equal(t, fmt.Sprintf("/%s/_search", index), httpReq.URL.Path)
})
}

func TestSearchWithRouting(t *testing.T) {
client, err := ostest.NewClient()
require.Nil(t, err)

index := "test-index-search"

_, err = client.Index(
nil,
opensearchapi.IndexReq{
DocumentID: "foo",
Index: index,
Body: strings.NewReader(`{"foo": "bar"}`),
Params: opensearchapi.IndexParams{Refresh: "true", Routing: "foo"},
},
)
require.Nil(t, err)
t.Cleanup(func() {
client.Indices.Delete(nil, opensearchapi.IndicesDeleteReq{Indices: []string{index}})
})

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)
})
}
Loading