github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.get_mapping.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 newIndicesGetMappingFunc(t Transport) IndicesGetMapping {
    38  	return func(o ...func(*IndicesGetMappingRequest)) (*Response, error) {
    39  		var r = IndicesGetMappingRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // IndicesGetMapping returns mappings for one or more indices.
    50  //
    51  //
    52  type IndicesGetMapping func(o ...func(*IndicesGetMappingRequest)) (*Response, error)
    53  
    54  // IndicesGetMappingRequest configures the Indices Get Mapping API request.
    55  //
    56  type IndicesGetMappingRequest struct {
    57  	Index []string
    58  
    59  	AllowNoIndices        *bool
    60  	ExpandWildcards       string
    61  	IgnoreUnavailable     *bool
    62  	Local                 *bool
    63  	MasterTimeout         time.Duration
    64  	ClusterManagerTimeout time.Duration
    65  
    66  	Pretty     bool
    67  	Human      bool
    68  	ErrorTrace bool
    69  	FilterPath []string
    70  
    71  	Header http.Header
    72  
    73  	ctx context.Context
    74  }
    75  
    76  // Do executes the request and returns response or error.
    77  //
    78  func (r IndicesGetMappingRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    79  	var (
    80  		method string
    81  		path   strings.Builder
    82  		params map[string]string
    83  	)
    84  
    85  	method = "GET"
    86  
    87  	path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_mapping"))
    88  	if len(r.Index) > 0 {
    89  		path.WriteString("/")
    90  		path.WriteString(strings.Join(r.Index, ","))
    91  	}
    92  	path.WriteString("/")
    93  	path.WriteString("_mapping")
    94  	params = make(map[string]string)
    95  
    96  	if r.AllowNoIndices != nil {
    97  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
    98  	}
    99  
   100  	if r.ExpandWildcards != "" {
   101  		params["expand_wildcards"] = r.ExpandWildcards
   102  	}
   103  
   104  	if r.IgnoreUnavailable != nil {
   105  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   106  	}
   107  
   108  	if r.Local != nil {
   109  		params["local"] = strconv.FormatBool(*r.Local)
   110  	}
   111  
   112  	if r.MasterTimeout != 0 {
   113  		params["master_timeout"] = formatDuration(r.MasterTimeout)
   114  	}
   115  
   116  	if r.ClusterManagerTimeout != 0 {
   117  		params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
   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(), nil)
   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 len(r.Header) > 0 {
   150  		if len(req.Header) == 0 {
   151  			req.Header = r.Header
   152  		} else {
   153  			for k, vv := range r.Header {
   154  				for _, v := range vv {
   155  					req.Header.Add(k, v)
   156  				}
   157  			}
   158  		}
   159  	}
   160  
   161  	if ctx != nil {
   162  		req = req.WithContext(ctx)
   163  	}
   164  
   165  	res, err := transport.Perform(req)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	response := Response{
   171  		StatusCode: res.StatusCode,
   172  		Body:       res.Body,
   173  		Header:     res.Header,
   174  	}
   175  
   176  	return &response, nil
   177  }
   178  
   179  // WithContext sets the request context.
   180  //
   181  func (f IndicesGetMapping) WithContext(v context.Context) func(*IndicesGetMappingRequest) {
   182  	return func(r *IndicesGetMappingRequest) {
   183  		r.ctx = v
   184  	}
   185  }
   186  
   187  // WithIndex - a list of index names.
   188  //
   189  func (f IndicesGetMapping) WithIndex(v ...string) func(*IndicesGetMappingRequest) {
   190  	return func(r *IndicesGetMappingRequest) {
   191  		r.Index = v
   192  	}
   193  }
   194  
   195  // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified).
   196  //
   197  func (f IndicesGetMapping) WithAllowNoIndices(v bool) func(*IndicesGetMappingRequest) {
   198  	return func(r *IndicesGetMappingRequest) {
   199  		r.AllowNoIndices = &v
   200  	}
   201  }
   202  
   203  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   204  //
   205  func (f IndicesGetMapping) WithExpandWildcards(v string) func(*IndicesGetMappingRequest) {
   206  	return func(r *IndicesGetMappingRequest) {
   207  		r.ExpandWildcards = v
   208  	}
   209  }
   210  
   211  // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
   212  //
   213  func (f IndicesGetMapping) WithIgnoreUnavailable(v bool) func(*IndicesGetMappingRequest) {
   214  	return func(r *IndicesGetMappingRequest) {
   215  		r.IgnoreUnavailable = &v
   216  	}
   217  }
   218  
   219  // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false).
   220  //
   221  func (f IndicesGetMapping) WithLocal(v bool) func(*IndicesGetMappingRequest) {
   222  	return func(r *IndicesGetMappingRequest) {
   223  		r.Local = &v
   224  	}
   225  }
   226  
   227  // WithMasterTimeout - explicit operation timeout for connection to cluster-manager node.
   228  //
   229  // Deprecated: To promote inclusive language, use WithClusterManagerTimeout instead.
   230  //
   231  func (f IndicesGetMapping) WithMasterTimeout(v time.Duration) func(*IndicesGetMappingRequest) {
   232  	return func(r *IndicesGetMappingRequest) {
   233  		r.MasterTimeout = v
   234  	}
   235  }
   236  
   237  // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node.
   238  //
   239  func (f IndicesGetMapping) WithClusterManagerTimeout(v time.Duration) func(*IndicesGetMappingRequest) {
   240  	return func(r *IndicesGetMappingRequest) {
   241  		r.ClusterManagerTimeout = v
   242  	}
   243  }
   244  
   245  // WithPretty makes the response body pretty-printed.
   246  //
   247  func (f IndicesGetMapping) WithPretty() func(*IndicesGetMappingRequest) {
   248  	return func(r *IndicesGetMappingRequest) {
   249  		r.Pretty = true
   250  	}
   251  }
   252  
   253  // WithHuman makes statistical values human-readable.
   254  //
   255  func (f IndicesGetMapping) WithHuman() func(*IndicesGetMappingRequest) {
   256  	return func(r *IndicesGetMappingRequest) {
   257  		r.Human = true
   258  	}
   259  }
   260  
   261  // WithErrorTrace includes the stack trace for errors in the response body.
   262  //
   263  func (f IndicesGetMapping) WithErrorTrace() func(*IndicesGetMappingRequest) {
   264  	return func(r *IndicesGetMappingRequest) {
   265  		r.ErrorTrace = true
   266  	}
   267  }
   268  
   269  // WithFilterPath filters the properties of the response body.
   270  //
   271  func (f IndicesGetMapping) WithFilterPath(v ...string) func(*IndicesGetMappingRequest) {
   272  	return func(r *IndicesGetMappingRequest) {
   273  		r.FilterPath = v
   274  	}
   275  }
   276  
   277  // WithHeader adds the headers to the HTTP request.
   278  //
   279  func (f IndicesGetMapping) WithHeader(h map[string]string) func(*IndicesGetMappingRequest) {
   280  	return func(r *IndicesGetMappingRequest) {
   281  		if r.Header == nil {
   282  			r.Header = make(http.Header)
   283  		}
   284  		for k, v := range h {
   285  			r.Header.Add(k, v)
   286  		}
   287  	}
   288  }
   289  
   290  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   291  //
   292  func (f IndicesGetMapping) WithOpaqueID(s string) func(*IndicesGetMappingRequest) {
   293  	return func(r *IndicesGetMappingRequest) {
   294  		if r.Header == nil {
   295  			r.Header = make(http.Header)
   296  		}
   297  		r.Header.Set("X-Opaque-Id", s)
   298  	}
   299  }