Skip to content

Commit

Permalink
Fix search request missing a slash when no indices are given (#471)
Browse files Browse the repository at this point in the history
* Fix search request missing a slash when no indices are given

Signed-off-by: Jakob Hahn <[email protected]>

* opensearchapi: add test to to verify url path for search

Signed-off-by: Jakob Hahn <[email protected]>

---------

Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD authored Apr 6, 2024
1 parent ed0ae19 commit 06a6dc8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
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)
})
}

0 comments on commit 06a6dc8

Please sign in to comment.