github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.exists_source.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  	"net/http"
    32  	"strconv"
    33  	"strings"
    34  )
    35  
    36  func newExistsSourceFunc(t Transport) ExistsSource {
    37  	return func(index string, id string, o ...func(*ExistsSourceRequest)) (*Response, error) {
    38  		var r = ExistsSourceRequest{Index: index, DocumentID: id}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // ExistsSource returns information about whether a document source exists in an index.
    49  //
    50  //
    51  type ExistsSource func(index string, id string, o ...func(*ExistsSourceRequest)) (*Response, error)
    52  
    53  // ExistsSourceRequest configures the Exists Source API request.
    54  //
    55  type ExistsSourceRequest struct {
    56  	Index      string
    57  	DocumentID string
    58  
    59  	Preference     string
    60  	Realtime       *bool
    61  	Refresh        *bool
    62  	Routing        string
    63  	Source         interface{}
    64  	SourceExcludes []string
    65  	SourceIncludes []string
    66  	Version        *int
    67  	VersionType    string
    68  
    69  	Pretty     bool
    70  	Human      bool
    71  	ErrorTrace bool
    72  	FilterPath []string
    73  
    74  	Header http.Header
    75  
    76  	ctx context.Context
    77  }
    78  
    79  // Do executes the request and returns response or error.
    80  //
    81  func (r ExistsSourceRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    82  	var (
    83  		method string
    84  		path   strings.Builder
    85  		params map[string]string
    86  	)
    87  
    88  	method = "HEAD"
    89  
    90  	path.Grow(1 + len(r.Index) + 1 + len(r.DocumentID) + 1 + len("_source"))
    91  	path.WriteString("/")
    92  	path.WriteString(r.Index)
    93  	path.WriteString("/")
    94  	path.WriteString("_source")
    95  	path.WriteString("/")
    96  	path.WriteString(r.DocumentID)
    97  
    98  	params = make(map[string]string)
    99  
   100  	if r.Preference != "" {
   101  		params["preference"] = r.Preference
   102  	}
   103  
   104  	if r.Realtime != nil {
   105  		params["realtime"] = strconv.FormatBool(*r.Realtime)
   106  	}
   107  
   108  	if r.Refresh != nil {
   109  		params["refresh"] = strconv.FormatBool(*r.Refresh)
   110  	}
   111  
   112  	if r.Routing != "" {
   113  		params["routing"] = r.Routing
   114  	}
   115  
   116  	if source, ok := r.Source.(bool); ok {
   117  		params["_source"] = strconv.FormatBool(source)
   118  	} else if source, ok := r.Source.(string); ok && source != "" {
   119  		params["_source"] = source
   120  	} else if sources, ok := r.Source.([]string); ok && len(sources) > 0 {
   121  		params["_source"] = strings.Join(sources, ",")
   122  	}
   123  
   124  	if len(r.SourceExcludes) > 0 {
   125  		params["_source_excludes"] = strings.Join(r.SourceExcludes, ",")
   126  	}
   127  
   128  	if len(r.SourceIncludes) > 0 {
   129  		params["_source_includes"] = strings.Join(r.SourceIncludes, ",")
   130  	}
   131  
   132  	if r.Version != nil {
   133  		params["version"] = strconv.FormatInt(int64(*r.Version), 10)
   134  	}
   135  
   136  	if r.VersionType != "" {
   137  		params["version_type"] = r.VersionType
   138  	}
   139  
   140  	if r.Pretty {
   141  		params["pretty"] = "true"
   142  	}
   143  
   144  	if r.Human {
   145  		params["human"] = "true"
   146  	}
   147  
   148  	if r.ErrorTrace {
   149  		params["error_trace"] = "true"
   150  	}
   151  
   152  	if len(r.FilterPath) > 0 {
   153  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   154  	}
   155  
   156  	req, err := newRequest(method, path.String(), nil)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	if len(params) > 0 {
   162  		q := req.URL.Query()
   163  		for k, v := range params {
   164  			q.Set(k, v)
   165  		}
   166  		req.URL.RawQuery = q.Encode()
   167  	}
   168  
   169  	if len(r.Header) > 0 {
   170  		if len(req.Header) == 0 {
   171  			req.Header = r.Header
   172  		} else {
   173  			for k, vv := range r.Header {
   174  				for _, v := range vv {
   175  					req.Header.Add(k, v)
   176  				}
   177  			}
   178  		}
   179  	}
   180  
   181  	if ctx != nil {
   182  		req = req.WithContext(ctx)
   183  	}
   184  
   185  	res, err := transport.Perform(req)
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	response := Response{
   191  		StatusCode: res.StatusCode,
   192  		Body:       res.Body,
   193  		Header:     res.Header,
   194  	}
   195  
   196  	return &response, nil
   197  }
   198  
   199  // WithContext sets the request context.
   200  //
   201  func (f ExistsSource) WithContext(v context.Context) func(*ExistsSourceRequest) {
   202  	return func(r *ExistsSourceRequest) {
   203  		r.ctx = v
   204  	}
   205  }
   206  
   207  // WithPreference - specify the node or shard the operation should be performed on (default: random).
   208  //
   209  func (f ExistsSource) WithPreference(v string) func(*ExistsSourceRequest) {
   210  	return func(r *ExistsSourceRequest) {
   211  		r.Preference = v
   212  	}
   213  }
   214  
   215  // WithRealtime - specify whether to perform the operation in realtime or search mode.
   216  //
   217  func (f ExistsSource) WithRealtime(v bool) func(*ExistsSourceRequest) {
   218  	return func(r *ExistsSourceRequest) {
   219  		r.Realtime = &v
   220  	}
   221  }
   222  
   223  // WithRefresh - refresh the shard containing the document before performing the operation.
   224  //
   225  func (f ExistsSource) WithRefresh(v bool) func(*ExistsSourceRequest) {
   226  	return func(r *ExistsSourceRequest) {
   227  		r.Refresh = &v
   228  	}
   229  }
   230  
   231  // WithRouting - specific routing value.
   232  //
   233  func (f ExistsSource) WithRouting(v string) func(*ExistsSourceRequest) {
   234  	return func(r *ExistsSourceRequest) {
   235  		r.Routing = v
   236  	}
   237  }
   238  
   239  // WithSource - true or false to return the _source field or not, or a list of fields to return.
   240  //
   241  func (f ExistsSource) WithSource(v interface{}) func(*ExistsSourceRequest) {
   242  	return func(r *ExistsSourceRequest) {
   243  		r.Source = v
   244  	}
   245  }
   246  
   247  // WithSourceExcludes - a list of fields to exclude from the returned _source field.
   248  //
   249  func (f ExistsSource) WithSourceExcludes(v ...string) func(*ExistsSourceRequest) {
   250  	return func(r *ExistsSourceRequest) {
   251  		r.SourceExcludes = v
   252  	}
   253  }
   254  
   255  // WithSourceIncludes - a list of fields to extract and return from the _source field.
   256  //
   257  func (f ExistsSource) WithSourceIncludes(v ...string) func(*ExistsSourceRequest) {
   258  	return func(r *ExistsSourceRequest) {
   259  		r.SourceIncludes = v
   260  	}
   261  }
   262  
   263  // WithVersion - explicit version number for concurrency control.
   264  //
   265  func (f ExistsSource) WithVersion(v int) func(*ExistsSourceRequest) {
   266  	return func(r *ExistsSourceRequest) {
   267  		r.Version = &v
   268  	}
   269  }
   270  
   271  // WithVersionType - specific version type.
   272  //
   273  func (f ExistsSource) WithVersionType(v string) func(*ExistsSourceRequest) {
   274  	return func(r *ExistsSourceRequest) {
   275  		r.VersionType = v
   276  	}
   277  }
   278  
   279  // WithPretty makes the response body pretty-printed.
   280  //
   281  func (f ExistsSource) WithPretty() func(*ExistsSourceRequest) {
   282  	return func(r *ExistsSourceRequest) {
   283  		r.Pretty = true
   284  	}
   285  }
   286  
   287  // WithHuman makes statistical values human-readable.
   288  //
   289  func (f ExistsSource) WithHuman() func(*ExistsSourceRequest) {
   290  	return func(r *ExistsSourceRequest) {
   291  		r.Human = true
   292  	}
   293  }
   294  
   295  // WithErrorTrace includes the stack trace for errors in the response body.
   296  //
   297  func (f ExistsSource) WithErrorTrace() func(*ExistsSourceRequest) {
   298  	return func(r *ExistsSourceRequest) {
   299  		r.ErrorTrace = true
   300  	}
   301  }
   302  
   303  // WithFilterPath filters the properties of the response body.
   304  //
   305  func (f ExistsSource) WithFilterPath(v ...string) func(*ExistsSourceRequest) {
   306  	return func(r *ExistsSourceRequest) {
   307  		r.FilterPath = v
   308  	}
   309  }
   310  
   311  // WithHeader adds the headers to the HTTP request.
   312  //
   313  func (f ExistsSource) WithHeader(h map[string]string) func(*ExistsSourceRequest) {
   314  	return func(r *ExistsSourceRequest) {
   315  		if r.Header == nil {
   316  			r.Header = make(http.Header)
   317  		}
   318  		for k, v := range h {
   319  			r.Header.Add(k, v)
   320  		}
   321  	}
   322  }
   323  
   324  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   325  //
   326  func (f ExistsSource) WithOpaqueID(s string) func(*ExistsSourceRequest) {
   327  	return func(r *ExistsSourceRequest) {
   328  		if r.Header == nil {
   329  			r.Header = make(http.Header)
   330  		}
   331  		r.Header.Set("X-Opaque-Id", s)
   332  	}
   333  }