Skip to content

Commit

Permalink
opensearchapi: add msearch function to rootClient
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD committed Sep 29, 2023
1 parent 8690bb6 commit b1b0927
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 0 deletions.
87 changes: 87 additions & 0 deletions opensearchapi/api_msearch-params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
//
// Modifications Copyright OpenSearch Contributors. See
// GitHub history for details.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opensearchapi

import (
"strconv"
)

// MSearchParams represents possible parameters for the MSearchReq
type MSearchParams struct {
CcsMinimizeRoundtrips *bool
MaxConcurrentSearches *int
MaxConcurrentShardRequests *int
PreFilterShardSize *int
RestTotalHitsAsInt *bool
SearchType string
TypedKeys *bool

Pretty bool
Human bool
ErrorTrace bool
}

func (r MSearchParams) get() map[string]string {
params := make(map[string]string)

if r.CcsMinimizeRoundtrips != nil {
params["ccs_minimize_roundtrips"] = strconv.FormatBool(*r.CcsMinimizeRoundtrips)
}

if r.MaxConcurrentSearches != nil {
params["max_concurrent_searches"] = strconv.FormatInt(int64(*r.MaxConcurrentSearches), 10)
}

if r.MaxConcurrentShardRequests != nil {
params["max_concurrent_shard_requests"] = strconv.FormatInt(int64(*r.MaxConcurrentShardRequests), 10)
}

if r.PreFilterShardSize != nil {
params["pre_filter_shard_size"] = strconv.FormatInt(int64(*r.PreFilterShardSize), 10)
}

if r.RestTotalHitsAsInt != nil {
params["rest_total_hits_as_int"] = strconv.FormatBool(*r.RestTotalHitsAsInt)
}

if r.SearchType != "" {
params["search_type"] = r.SearchType
}

if r.TypedKeys != nil {
params["typed_keys"] = strconv.FormatBool(*r.TypedKeys)
}

if r.Pretty {
params["pretty"] = "true"
}

if r.Human {
params["human"] = "true"
}

if r.ErrorTrace {
params["error_trace"] = "true"
}

return params
}
104 changes: 104 additions & 0 deletions opensearchapi/api_msearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
//
// Modifications Copyright OpenSearch Contributors. See
// GitHub history for details.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opensearchapi

import (
"context"
"io"
"net/http"
"strings"

"github.com/opensearch-project/opensearch-go/v2"
)

// MSearch executes a /_msearch request with the optional MSearchReq
func (c Client) MSearch(ctx context.Context, req MSearchReq) (*MSearchResp, error) {
var (
data MSearchResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return &data, err
}

return &data, nil
}

// MSearchReq represents possible options for the /_msearch request
type MSearchReq struct {
Indices []string

Body io.Reader

Header http.Header
Params MSearchParams
}

// GetRequest returns the *http.Request that gets executed by the client
func (r MSearchReq) GetRequest() (*http.Request, error) {
indices := strings.Join(r.Indices, ",")
var path strings.Builder
path.Grow(len("//_msearch") + len(indices))
if len(r.Indices) > 0 {
path.WriteString("/")
path.WriteString(indices)
}
path.WriteString("/_msearch")
return opensearch.BuildRequest(
"POST",
path.String(),
r.Body,
r.Params.get(),
r.Header,
)
}

// MSearchResp represents the returned struct of the /_msearch response
type MSearchResp struct {
Took int `json:"took"`
Responses []struct {
Took int `json:"took"`
Timeout bool `json:"timed_out"`
Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Failed int `json:"failed"`
Failures int `json:"failures"` // Deprecated field
Skipped int `json:"skipped"`
} `json:"_shards"`
Hits struct {
Total struct {
Value int `json:"value"`
Relation string `json:"relation"`
} `json:"total"`
MaxScore float32 `json:"max_score"`
Hits []SearchHit `json:"hits"`
} `json:"hits"`
Status int `json:"status"`
} `json:"responses"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r MSearchResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
82 changes: 82 additions & 0 deletions opensearchapi/api_msearch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
//
// Modifications Copyright OpenSearch Contributors. See
// GitHub history for details.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//go:build integration

package opensearchapi_test

import (
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
osapitest "github.com/opensearch-project/opensearch-go/v2/opensearchapi/internal/test"
)

func TestMSearch(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
require.Nil(t, err)

testIndex := "test-msearch"
t.Cleanup(func() {
client.Indices.Delete(nil, opensearchapi.IndicesDeleteReq{Indices: []string{testIndex}})
})

for i := 1; i <= 2; i++ {
_, err = client.Document.Create(
nil,
opensearchapi.DocumentCreateReq{
Index: testIndex,
Body: strings.NewReader(`{"foo": "bar"}`),
DocumentID: strconv.Itoa(i),
Params: opensearchapi.DocumentCreateParams{Refresh: "true"},
},
)
require.Nil(t, err)
}

t.Run("with request", func(t *testing.T) {
resp, err := client.MSearch(
nil,
opensearchapi.MSearchReq{
Indices: []string{testIndex},
Body: strings.NewReader("{}\n{\"query\":{\"exists\":{\"field\":\"foo\"}}}\n"),
},
)
require.Nil(t, err)
assert.NotEmpty(t, resp)
osapitest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response)
})

t.Run("inspect", func(t *testing.T) {
failingClient, err := osapitest.CreateFailingClient()
require.Nil(t, err)

res, err := failingClient.MSearch(nil, opensearchapi.MSearchReq{})
assert.NotNil(t, err)
assert.NotNil(t, res)
osapitest.VerifyInspect(t, res.Inspect())
})
}

0 comments on commit b1b0927

Please sign in to comment.