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