github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.rank_eval.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  package opensearchapi
    28  
    29  import (
    30  	"context"
    31  	"io"
    32  	"net/http"
    33  	"strconv"
    34  	"strings"
    35  )
    36  
    37  func newRankEvalFunc(t Transport) RankEval {
    38  	return func(body io.Reader, o ...func(*RankEvalRequest)) (*Response, error) {
    39  		var r = RankEvalRequest{Body: body}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // RankEval allows to evaluate the quality of ranked search results over a set of typical search queries
    50  //
    51  // This API is experimental.
    52  //
    53  //
    54  type RankEval func(body io.Reader, o ...func(*RankEvalRequest)) (*Response, error)
    55  
    56  // RankEvalRequest configures the Rank Eval API request.
    57  //
    58  type RankEvalRequest struct {
    59  	Index []string
    60  
    61  	Body io.Reader
    62  
    63  	AllowNoIndices    *bool
    64  	ExpandWildcards   string
    65  	IgnoreUnavailable *bool
    66  	SearchType        string
    67  
    68  	Pretty     bool
    69  	Human      bool
    70  	ErrorTrace bool
    71  	FilterPath []string
    72  
    73  	Header http.Header
    74  
    75  	ctx context.Context
    76  }
    77  
    78  // Do executes the request and returns response or error.
    79  //
    80  func (r RankEvalRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    81  	var (
    82  		method string
    83  		path   strings.Builder
    84  		params map[string]string
    85  	)
    86  
    87  	method = "POST"
    88  
    89  	path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_rank_eval"))
    90  	if len(r.Index) > 0 {
    91  		path.WriteString("/")
    92  		path.WriteString(strings.Join(r.Index, ","))
    93  	}
    94  	path.WriteString("/")
    95  	path.WriteString("_rank_eval")
    96  
    97  	params = make(map[string]string)
    98  
    99  	if r.AllowNoIndices != nil {
   100  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
   101  	}
   102  
   103  	if r.ExpandWildcards != "" {
   104  		params["expand_wildcards"] = r.ExpandWildcards
   105  	}
   106  
   107  	if r.IgnoreUnavailable != nil {
   108  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   109  	}
   110  
   111  	if r.SearchType != "" {
   112  		params["search_type"] = r.SearchType
   113  	}
   114  
   115  	if r.Pretty {
   116  		params["pretty"] = "true"
   117  	}
   118  
   119  	if r.Human {
   120  		params["human"] = "true"
   121  	}
   122  
   123  	if r.ErrorTrace {
   124  		params["error_trace"] = "true"
   125  	}
   126  
   127  	if len(r.FilterPath) > 0 {
   128  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   129  	}
   130  
   131  	req, err := newRequest(method, path.String(), r.Body)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	if len(params) > 0 {
   137  		q := req.URL.Query()
   138  		for k, v := range params {
   139  			q.Set(k, v)
   140  		}
   141  		req.URL.RawQuery = q.Encode()
   142  	}
   143  
   144  	if r.Body != nil {
   145  		req.Header[headerContentType] = headerContentTypeJSON
   146  	}
   147  
   148  	if len(r.Header) > 0 {
   149  		if len(req.Header) == 0 {
   150  			req.Header = r.Header
   151  		} else {
   152  			for k, vv := range r.Header {
   153  				for _, v := range vv {
   154  					req.Header.Add(k, v)
   155  				}
   156  			}
   157  		}
   158  	}
   159  
   160  	if ctx != nil {
   161  		req = req.WithContext(ctx)
   162  	}
   163  
   164  	res, err := transport.Perform(req)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	response := Response{
   170  		StatusCode: res.StatusCode,
   171  		Body:       res.Body,
   172  		Header:     res.Header,
   173  	}
   174  
   175  	return &response, nil
   176  }
   177  
   178  // WithContext sets the request context.
   179  //
   180  func (f RankEval) WithContext(v context.Context) func(*RankEvalRequest) {
   181  	return func(r *RankEvalRequest) {
   182  		r.ctx = v
   183  	}
   184  }
   185  
   186  // WithIndex - a list of index names to search; use _all to perform the operation on all indices.
   187  //
   188  func (f RankEval) WithIndex(v ...string) func(*RankEvalRequest) {
   189  	return func(r *RankEvalRequest) {
   190  		r.Index = v
   191  	}
   192  }
   193  
   194  // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified).
   195  //
   196  func (f RankEval) WithAllowNoIndices(v bool) func(*RankEvalRequest) {
   197  	return func(r *RankEvalRequest) {
   198  		r.AllowNoIndices = &v
   199  	}
   200  }
   201  
   202  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   203  //
   204  func (f RankEval) WithExpandWildcards(v string) func(*RankEvalRequest) {
   205  	return func(r *RankEvalRequest) {
   206  		r.ExpandWildcards = v
   207  	}
   208  }
   209  
   210  // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
   211  //
   212  func (f RankEval) WithIgnoreUnavailable(v bool) func(*RankEvalRequest) {
   213  	return func(r *RankEvalRequest) {
   214  		r.IgnoreUnavailable = &v
   215  	}
   216  }
   217  
   218  // WithSearchType - search operation type.
   219  //
   220  func (f RankEval) WithSearchType(v string) func(*RankEvalRequest) {
   221  	return func(r *RankEvalRequest) {
   222  		r.SearchType = v
   223  	}
   224  }
   225  
   226  // WithPretty makes the response body pretty-printed.
   227  //
   228  func (f RankEval) WithPretty() func(*RankEvalRequest) {
   229  	return func(r *RankEvalRequest) {
   230  		r.Pretty = true
   231  	}
   232  }
   233  
   234  // WithHuman makes statistical values human-readable.
   235  //
   236  func (f RankEval) WithHuman() func(*RankEvalRequest) {
   237  	return func(r *RankEvalRequest) {
   238  		r.Human = true
   239  	}
   240  }
   241  
   242  // WithErrorTrace includes the stack trace for errors in the response body.
   243  //
   244  func (f RankEval) WithErrorTrace() func(*RankEvalRequest) {
   245  	return func(r *RankEvalRequest) {
   246  		r.ErrorTrace = true
   247  	}
   248  }
   249  
   250  // WithFilterPath filters the properties of the response body.
   251  //
   252  func (f RankEval) WithFilterPath(v ...string) func(*RankEvalRequest) {
   253  	return func(r *RankEvalRequest) {
   254  		r.FilterPath = v
   255  	}
   256  }
   257  
   258  // WithHeader adds the headers to the HTTP request.
   259  //
   260  func (f RankEval) WithHeader(h map[string]string) func(*RankEvalRequest) {
   261  	return func(r *RankEvalRequest) {
   262  		if r.Header == nil {
   263  			r.Header = make(http.Header)
   264  		}
   265  		for k, v := range h {
   266  			r.Header.Add(k, v)
   267  		}
   268  	}
   269  }
   270  
   271  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   272  //
   273  func (f RankEval) WithOpaqueID(s string) func(*RankEvalRequest) {
   274  	return func(r *RankEvalRequest) {
   275  		if r.Header == nil {
   276  			r.Header = make(http.Header)
   277  		}
   278  		r.Header.Set("X-Opaque-Id", s)
   279  	}
   280  }