github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.field_caps.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  	"io"
    32  	"net/http"
    33  	"strconv"
    34  	"strings"
    35  )
    36  
    37  func newFieldCapsFunc(t Transport) FieldCaps {
    38  	return func(o ...func(*FieldCapsRequest)) (*Response, error) {
    39  		var r = FieldCapsRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // FieldCaps returns the information about the capabilities of fields among multiple indices.
    50  //
    51  //
    52  type FieldCaps func(o ...func(*FieldCapsRequest)) (*Response, error)
    53  
    54  // FieldCapsRequest configures the Field Caps API request.
    55  //
    56  type FieldCapsRequest struct {
    57  	Index []string
    58  
    59  	Body io.Reader
    60  
    61  	AllowNoIndices    *bool
    62  	ExpandWildcards   string
    63  	Fields            []string
    64  	IgnoreUnavailable *bool
    65  	IncludeUnmapped   *bool
    66  
    67  	Pretty     bool
    68  	Human      bool
    69  	ErrorTrace bool
    70  	FilterPath []string
    71  
    72  	Header http.Header
    73  
    74  	ctx context.Context
    75  }
    76  
    77  // Do executes the request and returns response or error.
    78  //
    79  func (r FieldCapsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    80  	var (
    81  		method string
    82  		path   strings.Builder
    83  		params map[string]string
    84  	)
    85  
    86  	method = "POST"
    87  
    88  	path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_field_caps"))
    89  	if len(r.Index) > 0 {
    90  		path.WriteString("/")
    91  		path.WriteString(strings.Join(r.Index, ","))
    92  	}
    93  	path.WriteString("/")
    94  	path.WriteString("_field_caps")
    95  
    96  	params = make(map[string]string)
    97  
    98  	if r.AllowNoIndices != nil {
    99  		params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
   100  	}
   101  
   102  	if r.ExpandWildcards != "" {
   103  		params["expand_wildcards"] = r.ExpandWildcards
   104  	}
   105  
   106  	if len(r.Fields) > 0 {
   107  		params["fields"] = strings.Join(r.Fields, ",")
   108  	}
   109  
   110  	if r.IgnoreUnavailable != nil {
   111  		params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
   112  	}
   113  
   114  	if r.IncludeUnmapped != nil {
   115  		params["include_unmapped"] = strconv.FormatBool(*r.IncludeUnmapped)
   116  	}
   117  
   118  	if r.Pretty {
   119  		params["pretty"] = "true"
   120  	}
   121  
   122  	if r.Human {
   123  		params["human"] = "true"
   124  	}
   125  
   126  	if r.ErrorTrace {
   127  		params["error_trace"] = "true"
   128  	}
   129  
   130  	if len(r.FilterPath) > 0 {
   131  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   132  	}
   133  
   134  	req, err := newRequest(method, path.String(), r.Body)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	if len(params) > 0 {
   140  		q := req.URL.Query()
   141  		for k, v := range params {
   142  			q.Set(k, v)
   143  		}
   144  		req.URL.RawQuery = q.Encode()
   145  	}
   146  
   147  	if r.Body != nil {
   148  		req.Header[headerContentType] = headerContentTypeJSON
   149  	}
   150  
   151  	if len(r.Header) > 0 {
   152  		if len(req.Header) == 0 {
   153  			req.Header = r.Header
   154  		} else {
   155  			for k, vv := range r.Header {
   156  				for _, v := range vv {
   157  					req.Header.Add(k, v)
   158  				}
   159  			}
   160  		}
   161  	}
   162  
   163  	if ctx != nil {
   164  		req = req.WithContext(ctx)
   165  	}
   166  
   167  	res, err := transport.Perform(req)
   168  	if err != nil {
   169  		return nil, err
   170  	}
   171  
   172  	response := Response{
   173  		StatusCode: res.StatusCode,
   174  		Body:       res.Body,
   175  		Header:     res.Header,
   176  	}
   177  
   178  	return &response, nil
   179  }
   180  
   181  // WithContext sets the request context.
   182  //
   183  func (f FieldCaps) WithContext(v context.Context) func(*FieldCapsRequest) {
   184  	return func(r *FieldCapsRequest) {
   185  		r.ctx = v
   186  	}
   187  }
   188  
   189  // WithBody - An index filter specified with the Query DSL.
   190  //
   191  func (f FieldCaps) WithBody(v io.Reader) func(*FieldCapsRequest) {
   192  	return func(r *FieldCapsRequest) {
   193  		r.Body = v
   194  	}
   195  }
   196  
   197  // WithIndex - a list of index names; use _all to perform the operation on all indices.
   198  //
   199  func (f FieldCaps) WithIndex(v ...string) func(*FieldCapsRequest) {
   200  	return func(r *FieldCapsRequest) {
   201  		r.Index = v
   202  	}
   203  }
   204  
   205  // 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).
   206  //
   207  func (f FieldCaps) WithAllowNoIndices(v bool) func(*FieldCapsRequest) {
   208  	return func(r *FieldCapsRequest) {
   209  		r.AllowNoIndices = &v
   210  	}
   211  }
   212  
   213  // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
   214  //
   215  func (f FieldCaps) WithExpandWildcards(v string) func(*FieldCapsRequest) {
   216  	return func(r *FieldCapsRequest) {
   217  		r.ExpandWildcards = v
   218  	}
   219  }
   220  
   221  // WithFields - a list of field names.
   222  //
   223  func (f FieldCaps) WithFields(v ...string) func(*FieldCapsRequest) {
   224  	return func(r *FieldCapsRequest) {
   225  		r.Fields = v
   226  	}
   227  }
   228  
   229  // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
   230  //
   231  func (f FieldCaps) WithIgnoreUnavailable(v bool) func(*FieldCapsRequest) {
   232  	return func(r *FieldCapsRequest) {
   233  		r.IgnoreUnavailable = &v
   234  	}
   235  }
   236  
   237  // WithIncludeUnmapped - indicates whether unmapped fields should be included in the response..
   238  //
   239  func (f FieldCaps) WithIncludeUnmapped(v bool) func(*FieldCapsRequest) {
   240  	return func(r *FieldCapsRequest) {
   241  		r.IncludeUnmapped = &v
   242  	}
   243  }
   244  
   245  // WithPretty makes the response body pretty-printed.
   246  //
   247  func (f FieldCaps) WithPretty() func(*FieldCapsRequest) {
   248  	return func(r *FieldCapsRequest) {
   249  		r.Pretty = true
   250  	}
   251  }
   252  
   253  // WithHuman makes statistical values human-readable.
   254  //
   255  func (f FieldCaps) WithHuman() func(*FieldCapsRequest) {
   256  	return func(r *FieldCapsRequest) {
   257  		r.Human = true
   258  	}
   259  }
   260  
   261  // WithErrorTrace includes the stack trace for errors in the response body.
   262  //
   263  func (f FieldCaps) WithErrorTrace() func(*FieldCapsRequest) {
   264  	return func(r *FieldCapsRequest) {
   265  		r.ErrorTrace = true
   266  	}
   267  }
   268  
   269  // WithFilterPath filters the properties of the response body.
   270  //
   271  func (f FieldCaps) WithFilterPath(v ...string) func(*FieldCapsRequest) {
   272  	return func(r *FieldCapsRequest) {
   273  		r.FilterPath = v
   274  	}
   275  }
   276  
   277  // WithHeader adds the headers to the HTTP request.
   278  //
   279  func (f FieldCaps) WithHeader(h map[string]string) func(*FieldCapsRequest) {
   280  	return func(r *FieldCapsRequest) {
   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 FieldCaps) WithOpaqueID(s string) func(*FieldCapsRequest) {
   293  	return func(r *FieldCapsRequest) {
   294  		if r.Header == nil {
   295  			r.Header = make(http.Header)
   296  		}
   297  		r.Header.Set("X-Opaque-Id", s)
   298  	}
   299  }