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

Fix search request missing a slash when no indices are given #471

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -18,6 +18,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed

### Fixed
- Fix search request missing a slash when no indices are given ([#470](https://github.com/opensearch-project/opensearch-go/pull/469))

### Security

Expand Down
2 changes: 1 addition & 1 deletion opensearchapi/api_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r SearchReq) GetRequest() (*http.Request, error) {
if len(r.Indices) > 0 {
path = fmt.Sprintf("/%s/_search", strings.Join(r.Indices, ","))
} else {
path = "_search"
path = "/_search"
}

return opensearch.BuildRequest(
Expand Down
43 changes: 32 additions & 11 deletions opensearchapi/api_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package opensearchapi_test

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -73,19 +74,39 @@ func TestSearch(t *testing.T) {
})

t.Run("request with retrieve specific fields", 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
}`)})
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)
})

t.Run("url path", func(t *testing.T) {
req := &opensearchapi.SearchReq{}
httpReq, err := req.GetRequest()
assert.Nil(t, err)
require.NotNil(t, httpReq)
assert.Equal(t, "/_search", httpReq.URL.Path)

req = &opensearchapi.SearchReq{Indices: []string{index}}
httpReq, err = req.GetRequest()
assert.Nil(t, err)
require.NotNil(t, httpReq)
assert.Equal(t, fmt.Sprintf("/%s/_search", index), httpReq.URL.Path)
})
}
Loading