github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.cat.fielddata.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  
    28  package opensearchapi
    29  
    30  import (
    31  	"context"
    32  	"net/http"
    33  	"strconv"
    34  	"strings"
    35  )
    36  
    37  func newCatFielddataFunc(t Transport) CatFielddata {
    38  	return func(o ...func(*CatFielddataRequest)) (*Response, error) {
    39  		var r = CatFielddataRequest{}
    40  		for _, f := range o {
    41  			f(&r)
    42  		}
    43  		return r.Do(r.ctx, t)
    44  	}
    45  }
    46  
    47  // ----- API Definition -------------------------------------------------------
    48  
    49  // CatFielddata shows how much heap memory is currently being used by fielddata on every data node in the cluster.
    50  //
    51  //
    52  type CatFielddata func(o ...func(*CatFielddataRequest)) (*Response, error)
    53  
    54  // CatFielddataRequest configures the Cat Fielddata API request.
    55  //
    56  type CatFielddataRequest struct {
    57  	Fields []string
    58  
    59  	Bytes  string
    60  	Format string
    61  	H      []string
    62  	Help   *bool
    63  	S      []string
    64  	V      *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 CatFielddataRequest) 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("_cat") + 1 + len("fielddata") + 1 + len(strings.Join(r.Fields, ",")))
    88  	path.WriteString("/")
    89  	path.WriteString("_cat")
    90  	path.WriteString("/")
    91  	path.WriteString("fielddata")
    92  	if len(r.Fields) > 0 {
    93  		path.WriteString("/")
    94  		path.WriteString(strings.Join(r.Fields, ","))
    95  	}
    96  
    97  	params = make(map[string]string)
    98  
    99  	if r.Bytes != "" {
   100  		params["bytes"] = r.Bytes
   101  	}
   102  
   103  	if len(r.Fields) > 0 {
   104  		params["fields"] = strings.Join(r.Fields, ",")
   105  	}
   106  
   107  	if r.Format != "" {
   108  		params["format"] = r.Format
   109  	}
   110  
   111  	if len(r.H) > 0 {
   112  		params["h"] = strings.Join(r.H, ",")
   113  	}
   114  
   115  	if r.Help != nil {
   116  		params["help"] = strconv.FormatBool(*r.Help)
   117  	}
   118  
   119  	if len(r.S) > 0 {
   120  		params["s"] = strings.Join(r.S, ",")
   121  	}
   122  
   123  	if r.V != nil {
   124  		params["v"] = strconv.FormatBool(*r.V)
   125  	}
   126  
   127  	if r.Pretty {
   128  		params["pretty"] = "true"
   129  	}
   130  
   131  	if r.Human {
   132  		params["human"] = "true"
   133  	}
   134  
   135  	if r.ErrorTrace {
   136  		params["error_trace"] = "true"
   137  	}
   138  
   139  	if len(r.FilterPath) > 0 {
   140  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   141  	}
   142  
   143  	req, err := newRequest(method, path.String(), nil)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	if len(params) > 0 {
   149  		q := req.URL.Query()
   150  		for k, v := range params {
   151  			q.Set(k, v)
   152  		}
   153  		req.URL.RawQuery = q.Encode()
   154  	}
   155  
   156  	if len(r.Header) > 0 {
   157  		if len(req.Header) == 0 {
   158  			req.Header = r.Header
   159  		} else {
   160  			for k, vv := range r.Header {
   161  				for _, v := range vv {
   162  					req.Header.Add(k, v)
   163  				}
   164  			}
   165  		}
   166  	}
   167  
   168  	if ctx != nil {
   169  		req = req.WithContext(ctx)
   170  	}
   171  
   172  	res, err := transport.Perform(req)
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	response := Response{
   178  		StatusCode: res.StatusCode,
   179  		Body:       res.Body,
   180  		Header:     res.Header,
   181  	}
   182  
   183  	return &response, nil
   184  }
   185  
   186  // WithContext sets the request context.
   187  //
   188  func (f CatFielddata) WithContext(v context.Context) func(*CatFielddataRequest) {
   189  	return func(r *CatFielddataRequest) {
   190  		r.ctx = v
   191  	}
   192  }
   193  
   194  // WithFields - a list of fields to return the fielddata size.
   195  //
   196  func (f CatFielddata) WithFields(v ...string) func(*CatFielddataRequest) {
   197  	return func(r *CatFielddataRequest) {
   198  		r.Fields = v
   199  	}
   200  }
   201  
   202  // WithBytes - the unit in which to display byte values.
   203  //
   204  func (f CatFielddata) WithBytes(v string) func(*CatFielddataRequest) {
   205  	return func(r *CatFielddataRequest) {
   206  		r.Bytes = v
   207  	}
   208  }
   209  
   210  // WithFormat - a short version of the accept header, e.g. json, yaml.
   211  //
   212  func (f CatFielddata) WithFormat(v string) func(*CatFielddataRequest) {
   213  	return func(r *CatFielddataRequest) {
   214  		r.Format = v
   215  	}
   216  }
   217  
   218  // WithH - comma-separated list of column names to display.
   219  //
   220  func (f CatFielddata) WithH(v ...string) func(*CatFielddataRequest) {
   221  	return func(r *CatFielddataRequest) {
   222  		r.H = v
   223  	}
   224  }
   225  
   226  // WithHelp - return help information.
   227  //
   228  func (f CatFielddata) WithHelp(v bool) func(*CatFielddataRequest) {
   229  	return func(r *CatFielddataRequest) {
   230  		r.Help = &v
   231  	}
   232  }
   233  
   234  // WithS - comma-separated list of column names or column aliases to sort by.
   235  //
   236  func (f CatFielddata) WithS(v ...string) func(*CatFielddataRequest) {
   237  	return func(r *CatFielddataRequest) {
   238  		r.S = v
   239  	}
   240  }
   241  
   242  // WithV - verbose mode. display column headers.
   243  //
   244  func (f CatFielddata) WithV(v bool) func(*CatFielddataRequest) {
   245  	return func(r *CatFielddataRequest) {
   246  		r.V = &v
   247  	}
   248  }
   249  
   250  // WithPretty makes the response body pretty-printed.
   251  //
   252  func (f CatFielddata) WithPretty() func(*CatFielddataRequest) {
   253  	return func(r *CatFielddataRequest) {
   254  		r.Pretty = true
   255  	}
   256  }
   257  
   258  // WithHuman makes statistical values human-readable.
   259  //
   260  func (f CatFielddata) WithHuman() func(*CatFielddataRequest) {
   261  	return func(r *CatFielddataRequest) {
   262  		r.Human = true
   263  	}
   264  }
   265  
   266  // WithErrorTrace includes the stack trace for errors in the response body.
   267  //
   268  func (f CatFielddata) WithErrorTrace() func(*CatFielddataRequest) {
   269  	return func(r *CatFielddataRequest) {
   270  		r.ErrorTrace = true
   271  	}
   272  }
   273  
   274  // WithFilterPath filters the properties of the response body.
   275  //
   276  func (f CatFielddata) WithFilterPath(v ...string) func(*CatFielddataRequest) {
   277  	return func(r *CatFielddataRequest) {
   278  		r.FilterPath = v
   279  	}
   280  }
   281  
   282  // WithHeader adds the headers to the HTTP request.
   283  //
   284  func (f CatFielddata) WithHeader(h map[string]string) func(*CatFielddataRequest) {
   285  	return func(r *CatFielddataRequest) {
   286  		if r.Header == nil {
   287  			r.Header = make(http.Header)
   288  		}
   289  		for k, v := range h {
   290  			r.Header.Add(k, v)
   291  		}
   292  	}
   293  }
   294  
   295  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   296  //
   297  func (f CatFielddata) WithOpaqueID(s string) func(*CatFielddataRequest) {
   298  	return func(r *CatFielddataRequest) {
   299  		if r.Header == nil {
   300  			r.Header = make(http.Header)
   301  		}
   302  		r.Header.Set("X-Opaque-Id", s)
   303  	}
   304  }