github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.get_field_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  )
    35  
    36  func newIndicesGetFieldMappingFunc(t Transport) IndicesGetFieldMapping {
    37  	return func(fields []string, o ...func(*IndicesGetFieldMappingRequest)) (*Response, error) {
    38  		var r = IndicesGetFieldMappingRequest{Fields: fields}
    39  		for _, f := range o {
    40  			f(&r)
    41  		}
    42  		return r.Do(r.ctx, t)
    43  	}
    44  }
    45  
    46  // ----- API Definition -------------------------------------------------------
    47  
    48  // IndicesGetFieldMapping returns mapping for one or more fields.
    49  //
    50  //
    51  type IndicesGetFieldMapping func(fields []string, o ...func(*IndicesGetFieldMappingRequest)) (*Response, error)
    52  
    53  // IndicesGetFieldMappingRequest configures the Indices Get Field Mapping API request.
    54  //
    55  type IndicesGetFieldMappingRequest struct {
    56  	Index        []string
    57  
    58  	Fields []string
    59  
    60  	AllowNoIndices    *bool
    61  	ExpandWildcards   string
    62  	IgnoreUnavailable *bool
    63  	IncludeDefaults   *bool
    64  	Local             *bool
    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 IndicesGetFieldMappingRequest) 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") + 1 + len("field") + 1 + len(strings.Join(r.Fields, ",")))
    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  	path.WriteString("/")
    95  	path.WriteString("field")
    96  	path.WriteString("/")
    97  	path.WriteString(strings.Join(r.Fields, ","))
    98  
    99  	params = make(map[string]string)
   100  
   101  	if r.AllowNoIndices != nil {
   102  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
   103  	}
   104  
   105  	if r.ExpandWildcards != "" {
   106  		params["expand_wildcards"] = r.ExpandWildcards
   107  	}
   108  
   109  	if r.IgnoreUnavailable != nil {
   110  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   111  	}
   112  
   113  	if r.IncludeDefaults != nil {
   114  		params["include_defaults"] = strconv.FormatBool(*r.IncludeDefaults)
   115  	}
   116  
   117  	if r.Local != nil {
   118  		params["local"] = strconv.FormatBool(*r.Local)
   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, 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, err
   169  	}
   170  
   171  	response := Response{
   172  		StatusCode: res.StatusCode,
   173  		Body:       res.Body,
   174  		Header:     res.Header,
   175  	}
   176  
   177  	return &response, nil
   178  }
   179  
   180  // WithContext sets the request context.
   181  //
   182  func (f IndicesGetFieldMapping) WithContext(v context.Context) func(*IndicesGetFieldMappingRequest) {
   183  	return func(r *IndicesGetFieldMappingRequest) {
   184  		r.ctx = v
   185  	}
   186  }
   187  
   188  // WithIndex - a list of index names.
   189  //
   190  func (f IndicesGetFieldMapping) WithIndex(v ...string) func(*IndicesGetFieldMappingRequest) {
   191  	return func(r *IndicesGetFieldMappingRequest) {
   192  		r.Index = v
   193  	}
   194  }
   195  
   196  // 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).
   197  //
   198  func (f IndicesGetFieldMapping) WithAllowNoIndices(v bool) func(*IndicesGetFieldMappingRequest) {
   199  	return func(r *IndicesGetFieldMappingRequest) {
   200  		r.AllowNoIndices = &v
   201  	}
   202  }
   203  
   204  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   205  //
   206  func (f IndicesGetFieldMapping) WithExpandWildcards(v string) func(*IndicesGetFieldMappingRequest) {
   207  	return func(r *IndicesGetFieldMappingRequest) {
   208  		r.ExpandWildcards = v
   209  	}
   210  }
   211  
   212  // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
   213  //
   214  func (f IndicesGetFieldMapping) WithIgnoreUnavailable(v bool) func(*IndicesGetFieldMappingRequest) {
   215  	return func(r *IndicesGetFieldMappingRequest) {
   216  		r.IgnoreUnavailable = &v
   217  	}
   218  }
   219  
   220  // WithIncludeDefaults - whether the default mapping values should be returned as well.
   221  //
   222  func (f IndicesGetFieldMapping) WithIncludeDefaults(v bool) func(*IndicesGetFieldMappingRequest) {
   223  	return func(r *IndicesGetFieldMappingRequest) {
   224  		r.IncludeDefaults = &v
   225  	}
   226  }
   227  
   228  // WithLocal - return local information, do not retrieve the state from cluster-manager node (default: false).
   229  //
   230  func (f IndicesGetFieldMapping) WithLocal(v bool) func(*IndicesGetFieldMappingRequest) {
   231  	return func(r *IndicesGetFieldMappingRequest) {
   232  		r.Local = &v
   233  	}
   234  }
   235  
   236  // WithPretty makes the response body pretty-printed.
   237  //
   238  func (f IndicesGetFieldMapping) WithPretty() func(*IndicesGetFieldMappingRequest) {
   239  	return func(r *IndicesGetFieldMappingRequest) {
   240  		r.Pretty = true
   241  	}
   242  }
   243  
   244  // WithHuman makes statistical values human-readable.
   245  //
   246  func (f IndicesGetFieldMapping) WithHuman() func(*IndicesGetFieldMappingRequest) {
   247  	return func(r *IndicesGetFieldMappingRequest) {
   248  		r.Human = true
   249  	}
   250  }
   251  
   252  // WithErrorTrace includes the stack trace for errors in the response body.
   253  //
   254  func (f IndicesGetFieldMapping) WithErrorTrace() func(*IndicesGetFieldMappingRequest) {
   255  	return func(r *IndicesGetFieldMappingRequest) {
   256  		r.ErrorTrace = true
   257  	}
   258  }
   259  
   260  // WithFilterPath filters the properties of the response body.
   261  //
   262  func (f IndicesGetFieldMapping) WithFilterPath(v ...string) func(*IndicesGetFieldMappingRequest) {
   263  	return func(r *IndicesGetFieldMappingRequest) {
   264  		r.FilterPath = v
   265  	}
   266  }
   267  
   268  // WithHeader adds the headers to the HTTP request.
   269  //
   270  func (f IndicesGetFieldMapping) WithHeader(h map[string]string) func(*IndicesGetFieldMappingRequest) {
   271  	return func(r *IndicesGetFieldMappingRequest) {
   272  		if r.Header == nil {
   273  			r.Header = make(http.Header)
   274  		}
   275  		for k, v := range h {
   276  			r.Header.Add(k, v)
   277  		}
   278  	}
   279  }
   280  
   281  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   282  //
   283  func (f IndicesGetFieldMapping) WithOpaqueID(s string) func(*IndicesGetFieldMappingRequest) {
   284  	return func(r *IndicesGetFieldMappingRequest) {
   285  		if r.Header == nil {
   286  			r.Header = make(http.Header)
   287  		}
   288  		r.Header.Set("X-Opaque-Id", s)
   289  	}
   290  }