-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opensearchapi: add rank_eval function to rootClient
Signed-off-by: Jakob Hahn <[email protected]>
- Loading branch information
Showing
3 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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" | ||
) | ||
|
||
// RankEvalParams represents possible parameters for the RankEvalReq | ||
type RankEvalParams struct { | ||
AllowNoIndices *bool | ||
ExpandWildcards string | ||
IgnoreUnavailable *bool | ||
SearchType string | ||
|
||
Pretty bool | ||
Human bool | ||
ErrorTrace bool | ||
} | ||
|
||
func (r RankEvalParams) get() map[string]string { | ||
params := make(map[string]string) | ||
|
||
if r.AllowNoIndices != nil { | ||
params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices) | ||
} | ||
|
||
if r.ExpandWildcards != "" { | ||
params["expand_wildcards"] = r.ExpandWildcards | ||
} | ||
|
||
if r.IgnoreUnavailable != nil { | ||
params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable) | ||
} | ||
|
||
if r.SearchType != "" { | ||
params["search_type"] = r.SearchType | ||
} | ||
|
||
if r.Pretty { | ||
params["pretty"] = "true" | ||
} | ||
|
||
if r.Human { | ||
params["human"] = "true" | ||
} | ||
|
||
if r.ErrorTrace { | ||
params["error_trace"] = "true" | ||
} | ||
|
||
return params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/opensearch-project/opensearch-go/v2" | ||
) | ||
|
||
// RankEval executes a /_rank_eval request with the required RankEvalReq | ||
func (c Client) RankEval(ctx context.Context, req RankEvalReq) (*RankEvalResp, error) { | ||
var ( | ||
data RankEvalResp | ||
err error | ||
) | ||
if data.response, err = c.do(ctx, req, &data); err != nil { | ||
return &data, err | ||
} | ||
|
||
return &data, nil | ||
} | ||
|
||
// RankEvalReq represents possible options for the /_rank_eval request | ||
type RankEvalReq struct { | ||
Indices []string | ||
|
||
Body io.Reader | ||
|
||
Header http.Header | ||
Params RankEvalParams | ||
} | ||
|
||
// GetRequest returns the *http.Request that gets executed by the client | ||
func (r RankEvalReq) GetRequest() (*http.Request, error) { | ||
indices := strings.Join(r.Indices, ",") | ||
var path strings.Builder | ||
path.Grow(len("//_rank_eval") + len(indices)) | ||
if len(r.Indices) > 0 { | ||
path.WriteString("/") | ||
path.WriteString(indices) | ||
} | ||
path.WriteString("/_rank_eval") | ||
return opensearch.BuildRequest( | ||
"GET", | ||
path.String(), | ||
r.Body, | ||
r.Params.get(), | ||
r.Header, | ||
) | ||
} | ||
|
||
// RankEvalResp represents the returned struct of the /_rank_eval response | ||
type RankEvalResp struct { | ||
MetricScore float64 `json:"metric_score"` | ||
Details json.RawMessage `json:"details"` | ||
Failures json.RawMessage `json:"failures"` | ||
response *opensearch.Response | ||
} | ||
|
||
// Inspect returns the Inspect type containing the raw *opensearch.Reponse | ||
func (r RankEvalResp) Inspect() Inspect { | ||
return Inspect{Response: r.response} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TestRankEval(t *testing.T) { | ||
client, err := opensearchapi.NewDefaultClient() | ||
require.Nil(t, err) | ||
|
||
testIndex := "test-rank_eval" | ||
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.RankEval( | ||
nil, | ||
opensearchapi.RankEvalReq{ | ||
Indices: []string{testIndex}, | ||
Body: strings.NewReader(`{"requests":[{"id":"test","request":{"query":{"match_all":{}}},"ratings":[]}],"metric":{"expected_reciprocal_rank":{"maximum_relevance":3,"k":20}}}`), | ||
}, | ||
) | ||
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.RankEval(nil, opensearchapi.RankEvalReq{}) | ||
assert.NotNil(t, err) | ||
assert.NotNil(t, res) | ||
osapitest.VerifyInspect(t, res.Inspect()) | ||
}) | ||
} |