Skip to content

Commit

Permalink
opensearchapi: add mget 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 9d14b8f commit 8690bb6
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 0 deletions.
102 changes: 102 additions & 0 deletions opensearchapi/api_mget-params.go
Original file line number Diff line number Diff line change
@@ -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
}
92 changes: 92 additions & 0 deletions opensearchapi/api_mget.go
Original file line number Diff line number Diff line change
@@ -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}
}
82 changes: 82 additions & 0 deletions opensearchapi/api_mget_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 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())
})
}

0 comments on commit 8690bb6

Please sign in to comment.