Skip to content

Commit

Permalink
opensearchapi: add mtermvercors 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 Oct 10, 2023
1 parent d84a742 commit d585724
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 0 deletions.
113 changes: 113 additions & 0 deletions opensearchapi/api_mtermvectors-params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// 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"
)

// MTermvectorsParams represents possible parameters for the MTermvectorsReq
type MTermvectorsParams struct {
Fields []string
FieldStatistics *bool
Ids []string
Offsets *bool
Payloads *bool
Positions *bool
Preference string
Realtime *bool
Routing string
TermStatistics *bool
Version *int
VersionType string

Pretty bool
Human bool
ErrorTrace bool
}

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

if len(r.Fields) > 0 {
params["fields"] = strings.Join(r.Fields, ",")
}

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

if len(r.Ids) > 0 {
params["ids"] = strings.Join(r.Ids, ",")
}

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

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

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

if r.Preference != "" {
params["preference"] = r.Preference
}

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

if r.Routing != "" {
params["routing"] = r.Routing
}

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

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

if r.VersionType != "" {
params["version_type"] = r.VersionType
}

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

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

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

return params
}
91 changes: 91 additions & 0 deletions opensearchapi/api_mtermvectors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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"
)

// MTermvectors executes a /_mtermvectors request with the required MTermvectorsReq
func (c Client) MTermvectors(ctx context.Context, req MTermvectorsReq) (*MTermvectorsResp, error) {
var (
data MTermvectorsResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return &data, err
}

return &data, nil
}

// MTermvectorsReq represents possible options for the /_mtermvectors request
type MTermvectorsReq struct {
Index string

Body io.Reader

Header http.Header
Params MTermvectorsParams
}

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

// MTermvectorsResp represents the returned struct of the /_mtermvectors response
type MTermvectorsResp struct {
Docs []struct {
Index string `json:"_index"`
ID string `json:"_id"`
Version int `json:"_version"`
Found bool `json:"found"`
Took int `json:"took"`
TermVectors json.RawMessage `json:"term_vectors"`
} `json:"docs"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r MTermvectorsResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
124 changes: 124 additions & 0 deletions opensearchapi/api_mtermvectors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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 TestMTermvectors(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
require.Nil(t, err)

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

_, err = client.Indices.Create(
nil,
opensearchapi.IndicesCreateReq{
Index: testIndex,
Body: strings.NewReader(`{ "mappings": {
"properties": {
"text": {
"type": "text",
"term_vector": "with_positions_offsets_payloads",
"store" : true,
"analyzer" : "fulltext_analyzer"
},
"fullname": {
"type": "text",
"term_vector": "with_positions_offsets_payloads",
"analyzer" : "fulltext_analyzer"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
},
"analysis": {
"analyzer": {
"fulltext_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"type_as_payload"
]
}
}
}
}
}`),
},
)
require.Nil(t, err)
docs := []string{`{"fullname":"John Doe","text":"test test test "}`, `{"fullname":"Jane Doe","text":"Another test ..."}`}
for i, doc := range docs {
_, err = client.Document.Create(
nil,
opensearchapi.DocumentCreateReq{
Index: testIndex,
Body: strings.NewReader(doc),
DocumentID: strconv.Itoa(i),
Params: opensearchapi.DocumentCreateParams{Refresh: "true"},
},
)
require.Nil(t, err)
}

t.Run("with request", func(t *testing.T) {
resp, err := client.MTermvectors(
nil,
opensearchapi.MTermvectorsReq{
Index: testIndex,
Body: strings.NewReader(`{"ids":[1,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.MTermvectors(nil, opensearchapi.MTermvectorsReq{})
assert.NotNil(t, err)
assert.NotNil(t, res)
osapitest.VerifyInspect(t, res.Inspect())
})
}

0 comments on commit d585724

Please sign in to comment.