github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.msearch_template.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 newMsearchTemplateFunc(t Transport) MsearchTemplate {
    38  	return func(body io.Reader, o ...func(*MsearchTemplateRequest)) (*Response, error) {
    39  		var r = MsearchTemplateRequest{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  // MsearchTemplate allows to execute several search template operations in one request.
    50  //
    51  //
    52  type MsearchTemplate func(body io.Reader, o ...func(*MsearchTemplateRequest)) (*Response, error)
    53  
    54  // MsearchTemplateRequest configures the Msearch Template API request.
    55  //
    56  type MsearchTemplateRequest struct {
    57  	Index        []string
    58  
    59  	Body io.Reader
    60  
    61  	CcsMinimizeRoundtrips *bool
    62  	MaxConcurrentSearches *int
    63  	RestTotalHitsAsInt    *bool
    64  	SearchType            string
    65  	TypedKeys             *bool
    66  
    67  	Pretty     bool
    68  	Human      bool
    69  	ErrorTrace bool
    70  	FilterPath []string
    71  
    72  	Header http.Header
    73  
    74  	ctx context.Context
    75  }
    76  
    77  // Do executes the request and returns response or error.
    78  //
    79  func (r MsearchTemplateRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    80  	var (
    81  		method string
    82  		path   strings.Builder
    83  		params map[string]string
    84  	)
    85  
    86  	method = "POST"
    87  
    88  	path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_msearch") + 1 + len("template"))
    89  	if len(r.Index) > 0 {
    90  		path.WriteString("/")
    91  		path.WriteString(strings.Join(r.Index, ","))
    92  	}
    93  	path.WriteString("/")
    94  	path.WriteString("_msearch")
    95  	path.WriteString("/")
    96  	path.WriteString("template")
    97  
    98  	params = make(map[string]string)
    99  
   100  	if r.CcsMinimizeRoundtrips != nil {
   101  		params["ccs_minimize_roundtrips"] = strconv.FormatBool(*r.CcsMinimizeRoundtrips)
   102  	}
   103  
   104  	if r.MaxConcurrentSearches != nil {
   105  		params["max_concurrent_searches"] = strconv.FormatInt(int64(*r.MaxConcurrentSearches), 10)
   106  	}
   107  
   108  	if r.RestTotalHitsAsInt != nil {
   109  		params["rest_total_hits_as_int"] = strconv.FormatBool(*r.RestTotalHitsAsInt)
   110  	}
   111  
   112  	if r.SearchType != "" {
   113  		params["search_type"] = r.SearchType
   114  	}
   115  
   116  	if r.TypedKeys != nil {
   117  		params["typed_keys"] = strconv.FormatBool(*r.TypedKeys)
   118  	}
   119  
   120  	if r.Pretty {
   121  		params["pretty"] = "true"
   122  	}
   123  
   124  	if r.Human {
   125  		params["human"] = "true"
   126  	}
   127  
   128  	if r.ErrorTrace {
   129  		params["error_trace"] = "true"
   130  	}
   131  
   132  	if len(r.FilterPath) > 0 {
   133  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   134  	}
   135  
   136  	req, err := newRequest(method, path.String(), r.Body)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	if len(params) > 0 {
   142  		q := req.URL.Query()
   143  		for k, v := range params {
   144  			q.Set(k, v)
   145  		}
   146  		req.URL.RawQuery = q.Encode()
   147  	}
   148  
   149  	if r.Body != nil {
   150  		req.Header[headerContentType] = headerContentTypeJSON
   151  	}
   152  
   153  	if len(r.Header) > 0 {
   154  		if len(req.Header) == 0 {
   155  			req.Header = r.Header
   156  		} else {
   157  			for k, vv := range r.Header {
   158  				for _, v := range vv {
   159  					req.Header.Add(k, v)
   160  				}
   161  			}
   162  		}
   163  	}
   164  
   165  	if ctx != nil {
   166  		req = req.WithContext(ctx)
   167  	}
   168  
   169  	res, err := transport.Perform(req)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  
   174  	response := Response{
   175  		StatusCode: res.StatusCode,
   176  		Body:       res.Body,
   177  		Header:     res.Header,
   178  	}
   179  
   180  	return &response, nil
   181  }
   182  
   183  // WithContext sets the request context.
   184  //
   185  func (f MsearchTemplate) WithContext(v context.Context) func(*MsearchTemplateRequest) {
   186  	return func(r *MsearchTemplateRequest) {
   187  		r.ctx = v
   188  	}
   189  }
   190  
   191  // WithIndex - a list of index names to use as default.
   192  //
   193  func (f MsearchTemplate) WithIndex(v ...string) func(*MsearchTemplateRequest) {
   194  	return func(r *MsearchTemplateRequest) {
   195  		r.Index = v
   196  	}
   197  }
   198  
   199  // WithCcsMinimizeRoundtrips - indicates whether network round-trips should be minimized as part of cross-cluster search requests execution.
   200  //
   201  func (f MsearchTemplate) WithCcsMinimizeRoundtrips(v bool) func(*MsearchTemplateRequest) {
   202  	return func(r *MsearchTemplateRequest) {
   203  		r.CcsMinimizeRoundtrips = &v
   204  	}
   205  }
   206  
   207  // WithMaxConcurrentSearches - controls the maximum number of concurrent searches the multi search api will execute.
   208  //
   209  func (f MsearchTemplate) WithMaxConcurrentSearches(v int) func(*MsearchTemplateRequest) {
   210  	return func(r *MsearchTemplateRequest) {
   211  		r.MaxConcurrentSearches = &v
   212  	}
   213  }
   214  
   215  // WithRestTotalHitsAsInt - indicates whether hits.total should be rendered as an integer or an object in the rest search response.
   216  //
   217  func (f MsearchTemplate) WithRestTotalHitsAsInt(v bool) func(*MsearchTemplateRequest) {
   218  	return func(r *MsearchTemplateRequest) {
   219  		r.RestTotalHitsAsInt = &v
   220  	}
   221  }
   222  
   223  // WithSearchType - search operation type.
   224  //
   225  func (f MsearchTemplate) WithSearchType(v string) func(*MsearchTemplateRequest) {
   226  	return func(r *MsearchTemplateRequest) {
   227  		r.SearchType = v
   228  	}
   229  }
   230  
   231  // WithTypedKeys - specify whether aggregation and suggester names should be prefixed by their respective types in the response.
   232  //
   233  func (f MsearchTemplate) WithTypedKeys(v bool) func(*MsearchTemplateRequest) {
   234  	return func(r *MsearchTemplateRequest) {
   235  		r.TypedKeys = &v
   236  	}
   237  }
   238  
   239  // WithPretty makes the response body pretty-printed.
   240  //
   241  func (f MsearchTemplate) WithPretty() func(*MsearchTemplateRequest) {
   242  	return func(r *MsearchTemplateRequest) {
   243  		r.Pretty = true
   244  	}
   245  }
   246  
   247  // WithHuman makes statistical values human-readable.
   248  //
   249  func (f MsearchTemplate) WithHuman() func(*MsearchTemplateRequest) {
   250  	return func(r *MsearchTemplateRequest) {
   251  		r.Human = true
   252  	}
   253  }
   254  
   255  // WithErrorTrace includes the stack trace for errors in the response body.
   256  //
   257  func (f MsearchTemplate) WithErrorTrace() func(*MsearchTemplateRequest) {
   258  	return func(r *MsearchTemplateRequest) {
   259  		r.ErrorTrace = true
   260  	}
   261  }
   262  
   263  // WithFilterPath filters the properties of the response body.
   264  //
   265  func (f MsearchTemplate) WithFilterPath(v ...string) func(*MsearchTemplateRequest) {
   266  	return func(r *MsearchTemplateRequest) {
   267  		r.FilterPath = v
   268  	}
   269  }
   270  
   271  // WithHeader adds the headers to the HTTP request.
   272  //
   273  func (f MsearchTemplate) WithHeader(h map[string]string) func(*MsearchTemplateRequest) {
   274  	return func(r *MsearchTemplateRequest) {
   275  		if r.Header == nil {
   276  			r.Header = make(http.Header)
   277  		}
   278  		for k, v := range h {
   279  			r.Header.Add(k, v)
   280  		}
   281  	}
   282  }
   283  
   284  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   285  //
   286  func (f MsearchTemplate) WithOpaqueID(s string) func(*MsearchTemplateRequest) {
   287  	return func(r *MsearchTemplateRequest) {
   288  		if r.Header == nil {
   289  			r.Header = make(http.Header)
   290  		}
   291  		r.Header.Set("X-Opaque-Id", s)
   292  	}
   293  }