github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.delete.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  	"time"
    35  )
    36  
    37  func newDeleteFunc(t Transport) Delete {
    38  	return func(index string, id string, o ...func(*DeleteRequest)) (*Response, error) {
    39  		var r = DeleteRequest{Index: index, DocumentID: id}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // Delete removes a document from the index.
    50  //
    51  //
    52  type Delete func(index string, id string, o ...func(*DeleteRequest)) (*Response, error)
    53  
    54  // DeleteRequest configures the Delete API request.
    55  //
    56  type DeleteRequest struct {
    57  	Index        string
    58  	DocumentID   string
    59  
    60  	IfPrimaryTerm       *int
    61  	IfSeqNo             *int
    62  	Refresh             string
    63  	Routing             string
    64  	Timeout             time.Duration
    65  	Version             *int
    66  	VersionType         string
    67  	WaitForActiveShards 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 DeleteRequest) 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 = "DELETE"
    89  
    90  	path.Grow(1 + len(r.Index) + 1 + len("_doc") + 1 + len(r.DocumentID))
    91  	path.WriteString("/")
    92  	path.WriteString(r.Index)
    93  	path.WriteString("/_doc")
    94  	path.WriteString("/")
    95  	path.WriteString(r.DocumentID)
    96  
    97  	params = make(map[string]string)
    98  
    99  	if r.IfPrimaryTerm != nil {
   100  		params["if_primary_term"] = strconv.FormatInt(int64(*r.IfPrimaryTerm), 10)
   101  	}
   102  
   103  	if r.IfSeqNo != nil {
   104  		params["if_seq_no"] = strconv.FormatInt(int64(*r.IfSeqNo), 10)
   105  	}
   106  
   107  	if r.Refresh != "" {
   108  		params["refresh"] = r.Refresh
   109  	}
   110  
   111  	if r.Routing != "" {
   112  		params["routing"] = r.Routing
   113  	}
   114  
   115  	if r.Timeout != 0 {
   116  		params["timeout"] = formatDuration(r.Timeout)
   117  	}
   118  
   119  	if r.Version != nil {
   120  		params["version"] = strconv.FormatInt(int64(*r.Version), 10)
   121  	}
   122  
   123  	if r.VersionType != "" {
   124  		params["version_type"] = r.VersionType
   125  	}
   126  
   127  	if r.WaitForActiveShards != "" {
   128  		params["wait_for_active_shards"] = r.WaitForActiveShards
   129  	}
   130  
   131  	if r.Pretty {
   132  		params["pretty"] = "true"
   133  	}
   134  
   135  	if r.Human {
   136  		params["human"] = "true"
   137  	}
   138  
   139  	if r.ErrorTrace {
   140  		params["error_trace"] = "true"
   141  	}
   142  
   143  	if len(r.FilterPath) > 0 {
   144  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   145  	}
   146  
   147  	req, err := newRequest(method, path.String(), nil)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  
   152  	if len(params) > 0 {
   153  		q := req.URL.Query()
   154  		for k, v := range params {
   155  			q.Set(k, v)
   156  		}
   157  		req.URL.RawQuery = q.Encode()
   158  	}
   159  
   160  	if len(r.Header) > 0 {
   161  		if len(req.Header) == 0 {
   162  			req.Header = r.Header
   163  		} else {
   164  			for k, vv := range r.Header {
   165  				for _, v := range vv {
   166  					req.Header.Add(k, v)
   167  				}
   168  			}
   169  		}
   170  	}
   171  
   172  	if ctx != nil {
   173  		req = req.WithContext(ctx)
   174  	}
   175  
   176  	res, err := transport.Perform(req)
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  
   181  	response := Response{
   182  		StatusCode: res.StatusCode,
   183  		Body:       res.Body,
   184  		Header:     res.Header,
   185  	}
   186  
   187  	return &response, nil
   188  }
   189  
   190  // WithContext sets the request context.
   191  //
   192  func (f Delete) WithContext(v context.Context) func(*DeleteRequest) {
   193  	return func(r *DeleteRequest) {
   194  		r.ctx = v
   195  	}
   196  }
   197  
   198  // WithIfPrimaryTerm - only perform the delete operation if the last operation that has changed the document has the specified primary term.
   199  //
   200  func (f Delete) WithIfPrimaryTerm(v int) func(*DeleteRequest) {
   201  	return func(r *DeleteRequest) {
   202  		r.IfPrimaryTerm = &v
   203  	}
   204  }
   205  
   206  // WithIfSeqNo - only perform the delete operation if the last operation that has changed the document has the specified sequence number.
   207  //
   208  func (f Delete) WithIfSeqNo(v int) func(*DeleteRequest) {
   209  	return func(r *DeleteRequest) {
   210  		r.IfSeqNo = &v
   211  	}
   212  }
   213  
   214  // WithRefresh - if `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes..
   215  //
   216  func (f Delete) WithRefresh(v string) func(*DeleteRequest) {
   217  	return func(r *DeleteRequest) {
   218  		r.Refresh = v
   219  	}
   220  }
   221  
   222  // WithRouting - specific routing value.
   223  //
   224  func (f Delete) WithRouting(v string) func(*DeleteRequest) {
   225  	return func(r *DeleteRequest) {
   226  		r.Routing = v
   227  	}
   228  }
   229  
   230  // WithTimeout - explicit operation timeout.
   231  //
   232  func (f Delete) WithTimeout(v time.Duration) func(*DeleteRequest) {
   233  	return func(r *DeleteRequest) {
   234  		r.Timeout = v
   235  	}
   236  }
   237  
   238  // WithVersion - explicit version number for concurrency control.
   239  //
   240  func (f Delete) WithVersion(v int) func(*DeleteRequest) {
   241  	return func(r *DeleteRequest) {
   242  		r.Version = &v
   243  	}
   244  }
   245  
   246  // WithVersionType - specific version type.
   247  //
   248  func (f Delete) WithVersionType(v string) func(*DeleteRequest) {
   249  	return func(r *DeleteRequest) {
   250  		r.VersionType = v
   251  	}
   252  }
   253  
   254  // WithWaitForActiveShards - sets the number of shard copies that must be active before proceeding with the delete operation. defaults to 1, meaning the primary shard only. set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1).
   255  //
   256  func (f Delete) WithWaitForActiveShards(v string) func(*DeleteRequest) {
   257  	return func(r *DeleteRequest) {
   258  		r.WaitForActiveShards = v
   259  	}
   260  }
   261  
   262  // WithPretty makes the response body pretty-printed.
   263  //
   264  func (f Delete) WithPretty() func(*DeleteRequest) {
   265  	return func(r *DeleteRequest) {
   266  		r.Pretty = true
   267  	}
   268  }
   269  
   270  // WithHuman makes statistical values human-readable.
   271  //
   272  func (f Delete) WithHuman() func(*DeleteRequest) {
   273  	return func(r *DeleteRequest) {
   274  		r.Human = true
   275  	}
   276  }
   277  
   278  // WithErrorTrace includes the stack trace for errors in the response body.
   279  //
   280  func (f Delete) WithErrorTrace() func(*DeleteRequest) {
   281  	return func(r *DeleteRequest) {
   282  		r.ErrorTrace = true
   283  	}
   284  }
   285  
   286  // WithFilterPath filters the properties of the response body.
   287  //
   288  func (f Delete) WithFilterPath(v ...string) func(*DeleteRequest) {
   289  	return func(r *DeleteRequest) {
   290  		r.FilterPath = v
   291  	}
   292  }
   293  
   294  // WithHeader adds the headers to the HTTP request.
   295  //
   296  func (f Delete) WithHeader(h map[string]string) func(*DeleteRequest) {
   297  	return func(r *DeleteRequest) {
   298  		if r.Header == nil {
   299  			r.Header = make(http.Header)
   300  		}
   301  		for k, v := range h {
   302  			r.Header.Add(k, v)
   303  		}
   304  	}
   305  }
   306  
   307  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   308  //
   309  func (f Delete) WithOpaqueID(s string) func(*DeleteRequest) {
   310  	return func(r *DeleteRequest) {
   311  		if r.Header == nil {
   312  			r.Header = make(http.Header)
   313  		}
   314  		r.Header.Set("X-Opaque-Id", s)
   315  	}
   316  }