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