From d5857240773aaaa824a073048a94c9fcf0b2266e Mon Sep 17 00:00:00 2001 From: Jakob Hahn Date: Tue, 10 Oct 2023 13:28:10 +0200 Subject: [PATCH] opensearchapi: add mtermvercors function to rootClient Signed-off-by: Jakob Hahn --- opensearchapi/api_mtermvectors-params.go | 113 +++++++++++++++++++++ opensearchapi/api_mtermvectors.go | 91 +++++++++++++++++ opensearchapi/api_mtermvectors_test.go | 124 +++++++++++++++++++++++ 3 files changed, 328 insertions(+) create mode 100644 opensearchapi/api_mtermvectors-params.go create mode 100644 opensearchapi/api_mtermvectors.go create mode 100644 opensearchapi/api_mtermvectors_test.go diff --git a/opensearchapi/api_mtermvectors-params.go b/opensearchapi/api_mtermvectors-params.go new file mode 100644 index 000000000..e3ddacd2c --- /dev/null +++ b/opensearchapi/api_mtermvectors-params.go @@ -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 +} diff --git a/opensearchapi/api_mtermvectors.go b/opensearchapi/api_mtermvectors.go new file mode 100644 index 000000000..923beb06d --- /dev/null +++ b/opensearchapi/api_mtermvectors.go @@ -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} +} diff --git a/opensearchapi/api_mtermvectors_test.go b/opensearchapi/api_mtermvectors_test.go new file mode 100644 index 000000000..25da1cd80 --- /dev/null +++ b/opensearchapi/api_mtermvectors_test.go @@ -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()) + }) +}