From 8690bb6fc8cc27c44364b772ccb7c9ab0a5a4067 Mon Sep 17 00:00:00 2001 From: Jakob Hahn Date: Fri, 29 Sep 2023 13:46:39 +0200 Subject: [PATCH] opensearchapi: add mget function to rootClient Signed-off-by: Jakob Hahn --- opensearchapi/api_mget-params.go | 102 +++++++++++++++++++++++++++++++ opensearchapi/api_mget.go | 92 ++++++++++++++++++++++++++++ opensearchapi/api_mget_test.go | 82 +++++++++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 opensearchapi/api_mget-params.go create mode 100644 opensearchapi/api_mget.go create mode 100644 opensearchapi/api_mget_test.go diff --git a/opensearchapi/api_mget-params.go b/opensearchapi/api_mget-params.go new file mode 100644 index 000000000..5d97faf6a --- /dev/null +++ b/opensearchapi/api_mget-params.go @@ -0,0 +1,102 @@ +// 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" + "strings" +) + +// MGetParams represents possible parameters for the MGetReq +type MGetParams struct { + Preference string + Realtime *bool + Refresh *bool + Routing string + Source interface{} + SourceExcludes []string + SourceIncludes []string + StoredFields []string + + Pretty bool + Human bool + ErrorTrace bool +} + +func (r MGetParams) get() map[string]string { + params := make(map[string]string) + + if r.Preference != "" { + params["preference"] = r.Preference + } + + if r.Realtime != nil { + params["realtime"] = strconv.FormatBool(*r.Realtime) + } + + if r.Refresh != nil { + params["refresh"] = strconv.FormatBool(*r.Refresh) + } + + if r.Routing != "" { + params["routing"] = r.Routing + } + + switch source := r.Source.(type) { + case bool: + params["_source"] = strconv.FormatBool(source) + case string: + if source != "" { + params["_source"] = source + } + case []string: + if len(source) > 0 { + params["_source"] = strings.Join(source, ",") + } + } + + if len(r.SourceExcludes) > 0 { + params["_source_excludes"] = strings.Join(r.SourceExcludes, ",") + } + + if len(r.SourceIncludes) > 0 { + params["_source_includes"] = strings.Join(r.SourceIncludes, ",") + } + + if len(r.StoredFields) > 0 { + params["stored_fields"] = strings.Join(r.StoredFields, ",") + } + + if r.Pretty { + params["pretty"] = "true" + } + + if r.Human { + params["human"] = "true" + } + + if r.ErrorTrace { + params["error_trace"] = "true" + } + + return params +} diff --git a/opensearchapi/api_mget.go b/opensearchapi/api_mget.go new file mode 100644 index 000000000..1c4904233 --- /dev/null +++ b/opensearchapi/api_mget.go @@ -0,0 +1,92 @@ +// 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" + "encoding/json" + "io" + "net/http" + "strings" + + "github.com/opensearch-project/opensearch-go/v2" +) + +// MGet executes a /_mget request with the optional MGetReq +func (c Client) MGet(ctx context.Context, req MGetReq) (*MGetResp, error) { + var ( + data MGetResp + err error + ) + if data.response, err = c.do(ctx, req, &data); err != nil { + return &data, err + } + + return &data, nil +} + +// MGetReq represents possible options for the /_mget request +type MGetReq struct { + Index string + + Body io.Reader + + Header http.Header + Params MGetParams +} + +// GetRequest returns the *http.Request that gets executed by the client +func (r MGetReq) GetRequest() (*http.Request, error) { + var path strings.Builder + path.Grow(len("//_mget") + len(r.Index)) + if len(r.Index) > 0 { + path.WriteString("/") + path.WriteString(r.Index) + } + path.WriteString("/_mget") + return opensearch.BuildRequest( + "POST", + path.String(), + r.Body, + r.Params.get(), + r.Header, + ) +} + +// MGetResp represents the returned struct of the /_mget response +type MGetResp struct { + Docs []struct { + Index string `json:"_index"` + ID string `json:"_id"` + Version int `json:"_version"` + SeqNo int `json:"_seq_no"` + PrimaryTerm int `json:"_primary_term"` + Found bool `json:"found"` + Source json.RawMessage `json:"_source"` + } `json:"docs"` + response *opensearch.Response +} + +// Inspect returns the Inspect type containing the raw *opensearch.Reponse +func (r MGetResp) Inspect() Inspect { + return Inspect{Response: r.response} +} diff --git a/opensearchapi/api_mget_test.go b/opensearchapi/api_mget_test.go new file mode 100644 index 000000000..e44701817 --- /dev/null +++ b/opensearchapi/api_mget_test.go @@ -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 TestMGet(t *testing.T) { + client, err := opensearchapi.NewDefaultClient() + require.Nil(t, err) + + testIndex := "test-mget" + 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.MGet( + nil, + opensearchapi.MGetReq{ + Index: testIndex, + Body: strings.NewReader(`{"docs":[{"_id":"1"},{"_id":"2"}]}`), + }, + ) + 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.MGet(nil, opensearchapi.MGetReq{}) + assert.NotNil(t, err) + assert.NotNil(t, res) + osapitest.VerifyInspect(t, res.Inspect()) + }) +}