github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.pointintime.create.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  	"encoding/json"
    32  	"net/http"
    33  	"strings"
    34  	"time"
    35  )
    36  
    37  func newPointInTimeCreateFunc(t Transport) PointInTimeCreate {
    38  	return func(o ...func(*PointInTimeCreateRequest)) (*Response, *PointInTimeCreateResp, error) {
    39  		var r = PointInTimeCreateRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // PointInTimeCreate let you create a pit for searching with pagination
    50  type PointInTimeCreate func(o ...func(*PointInTimeCreateRequest)) (*Response, *PointInTimeCreateResp, error)
    51  
    52  // PointInTimeCreateRequest configures the Point In Time Create API request.
    53  type PointInTimeCreateRequest struct {
    54  	Index []string
    55  
    56  	KeepAlive               time.Duration
    57  	Preference              string
    58  	Routing                 string
    59  	ExpandWildcards         string
    60  	AllowPartialPitCreation bool
    61  
    62  	Pretty     bool
    63  	Human      bool
    64  	ErrorTrace bool
    65  	FilterPath []string
    66  
    67  	Header http.Header
    68  
    69  	ctx context.Context
    70  }
    71  
    72  // PointInTimeCreateResp is a custom type to parse the Point In Time Create Reponse
    73  type PointInTimeCreateResp struct {
    74  	PitID  string `json:"pit_id"`
    75  	Shards struct {
    76  		Total      int `json:"total"`
    77  		Successful int `json:"successful"`
    78  		Skipped    int `json:"skipped"`
    79  		Failed     int `json:"failed"`
    80  	} `json:"_shards"`
    81  	CreationTime int `json:"creation_time"`
    82  }
    83  
    84  // Do executes the request and returns response, PointInTimeCreateResp and error.
    85  func (r PointInTimeCreateRequest) Do(ctx context.Context, transport Transport) (*Response, *PointInTimeCreateResp, error) {
    86  	var (
    87  		path   strings.Builder
    88  		params map[string]string
    89  
    90  		data PointInTimeCreateResp
    91  	)
    92  	method := "POST"
    93  
    94  	path.Grow(1 + len(strings.Join(r.Index, ",")) + len("/_search/point_in_time"))
    95  	path.WriteString("/")
    96  	path.WriteString(strings.Join(r.Index, ","))
    97  	path.WriteString("/_search/point_in_time")
    98  
    99  	params = make(map[string]string)
   100  
   101  	if r.KeepAlive != 0 {
   102  		params["keep_alive"] = formatDuration(r.KeepAlive)
   103  	}
   104  
   105  	if r.Preference != "" {
   106  		params["preference"] = r.Preference
   107  	}
   108  
   109  	if r.Routing != "" {
   110  		params["routing"] = r.Routing
   111  	}
   112  
   113  	if r.ExpandWildcards != "" {
   114  		params["expand_wildcards"] = r.ExpandWildcards
   115  	}
   116  
   117  	if r.AllowPartialPitCreation {
   118  		params["allow_partial_pit_creation"] = "true"
   119  	}
   120  
   121  	if r.Pretty {
   122  		params["pretty"] = "true"
   123  	}
   124  
   125  	if r.Human {
   126  		params["human"] = "true"
   127  	}
   128  
   129  	if r.ErrorTrace {
   130  		params["error_trace"] = "true"
   131  	}
   132  
   133  	if len(r.FilterPath) > 0 {
   134  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   135  	}
   136  
   137  	req, err := newRequest(method, path.String(), nil)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  
   142  	if len(params) > 0 {
   143  		q := req.URL.Query()
   144  		for k, v := range params {
   145  			q.Set(k, v)
   146  		}
   147  		req.URL.RawQuery = q.Encode()
   148  	}
   149  
   150  	if len(r.Header) > 0 {
   151  		if len(req.Header) == 0 {
   152  			req.Header = r.Header
   153  		} else {
   154  			for k, vv := range r.Header {
   155  				for _, v := range vv {
   156  					req.Header.Add(k, v)
   157  				}
   158  			}
   159  		}
   160  	}
   161  
   162  	if ctx != nil {
   163  		req = req.WithContext(ctx)
   164  	}
   165  
   166  	res, err := transport.Perform(req)
   167  	if err != nil {
   168  		return nil, nil, err
   169  	}
   170  
   171  	response := Response{
   172  		StatusCode: res.StatusCode,
   173  		Body:       res.Body,
   174  		Header:     res.Header,
   175  	}
   176  
   177  	if len(r.FilterPath) != 0 {
   178  		return &response, nil, nil
   179  	}
   180  
   181  	if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
   182  		return &response, nil, err
   183  	}
   184  	return &response, &data, nil
   185  }
   186  
   187  // WithIndex - a list of index names to search; use _all to perform the operation on all indices.
   188  func (f PointInTimeCreate) WithIndex(v ...string) func(*PointInTimeCreateRequest) {
   189  	return func(r *PointInTimeCreateRequest) {
   190  		r.Index = v
   191  	}
   192  }
   193  
   194  // WithContext sets the request context.
   195  func (f PointInTimeCreate) WithContext(v context.Context) func(*PointInTimeCreateRequest) {
   196  	return func(r *PointInTimeCreateRequest) {
   197  		r.ctx = v
   198  	}
   199  }
   200  
   201  // WithKeepAlive - specify the amount of time to keep the PIT.
   202  func (f PointInTimeCreate) WithKeepAlive(v time.Duration) func(*PointInTimeCreateRequest) {
   203  	return func(r *PointInTimeCreateRequest) {
   204  		r.KeepAlive = v
   205  	}
   206  }
   207  
   208  // WithPretty makes the response body pretty-printed.
   209  func (f PointInTimeCreate) WithPretty() func(*PointInTimeCreateRequest) {
   210  	return func(r *PointInTimeCreateRequest) {
   211  		r.Pretty = true
   212  	}
   213  }
   214  
   215  // WithHuman makes statistical values human-readable.
   216  func (f PointInTimeCreate) WithHuman() func(*PointInTimeCreateRequest) {
   217  	return func(r *PointInTimeCreateRequest) {
   218  		r.Human = true
   219  	}
   220  }
   221  
   222  // WithErrorTrace includes the stack trace for errors in the response body.
   223  func (f PointInTimeCreate) WithErrorTrace() func(*PointInTimeCreateRequest) {
   224  	return func(r *PointInTimeCreateRequest) {
   225  		r.ErrorTrace = true
   226  	}
   227  }
   228  
   229  // WithFilterPath filters the properties of the response body.
   230  func (f PointInTimeCreate) WithFilterPath(v ...string) func(*PointInTimeCreateRequest) {
   231  	return func(r *PointInTimeCreateRequest) {
   232  		r.FilterPath = v
   233  	}
   234  }
   235  
   236  // WithHeader adds the headers to the HTTP request.
   237  func (f PointInTimeCreate) WithHeader(h map[string]string) func(*PointInTimeCreateRequest) {
   238  	return func(r *PointInTimeCreateRequest) {
   239  		if r.Header == nil {
   240  			r.Header = make(http.Header)
   241  		}
   242  		for k, v := range h {
   243  			r.Header.Add(k, v)
   244  		}
   245  	}
   246  }
   247  
   248  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   249  func (f PointInTimeCreate) WithOpaqueID(s string) func(*PointInTimeCreateRequest) {
   250  	return func(r *PointInTimeCreateRequest) {
   251  		if r.Header == nil {
   252  			r.Header = make(http.Header)
   253  		}
   254  		r.Header.Set("X-Opaque-Id", s)
   255  	}
   256  }