github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.search_shards.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 newSearchShardsFunc(t Transport) SearchShards { 37 return func(o ...func(*SearchShardsRequest)) (*Response, error) { 38 var r = SearchShardsRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // SearchShards returns information about the indices and shards that a search request would be executed against. 49 // 50 // 51 type SearchShards func(o ...func(*SearchShardsRequest)) (*Response, error) 52 53 // SearchShardsRequest configures the Search Shards API request. 54 // 55 type SearchShardsRequest struct { 56 Index []string 57 58 AllowNoIndices *bool 59 ExpandWildcards string 60 IgnoreUnavailable *bool 61 Local *bool 62 Preference string 63 Routing string 64 65 Pretty bool 66 Human bool 67 ErrorTrace bool 68 FilterPath []string 69 70 Header http.Header 71 72 ctx context.Context 73 } 74 75 // Do executes the request and returns response or error. 76 // 77 func (r SearchShardsRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 78 var ( 79 method string 80 path strings.Builder 81 params map[string]string 82 ) 83 84 method = "POST" 85 86 path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_search_shards")) 87 if len(r.Index) > 0 { 88 path.WriteString("/") 89 path.WriteString(strings.Join(r.Index, ",")) 90 } 91 path.WriteString("/") 92 path.WriteString("_search_shards") 93 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.Preference != "" { 113 params["preference"] = r.Preference 114 } 115 116 if r.Routing != "" { 117 params["routing"] = r.Routing 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 SearchShards) WithContext(v context.Context) func(*SearchShardsRequest) { 182 return func(r *SearchShardsRequest) { 183 r.ctx = v 184 } 185 } 186 187 // WithIndex - a list of index names to search; use _all to perform the operation on all indices. 188 // 189 func (f SearchShards) WithIndex(v ...string) func(*SearchShardsRequest) { 190 return func(r *SearchShardsRequest) { 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 SearchShards) WithAllowNoIndices(v bool) func(*SearchShardsRequest) { 198 return func(r *SearchShardsRequest) { 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 SearchShards) WithExpandWildcards(v string) func(*SearchShardsRequest) { 206 return func(r *SearchShardsRequest) { 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 SearchShards) WithIgnoreUnavailable(v bool) func(*SearchShardsRequest) { 214 return func(r *SearchShardsRequest) { 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 SearchShards) WithLocal(v bool) func(*SearchShardsRequest) { 222 return func(r *SearchShardsRequest) { 223 r.Local = &v 224 } 225 } 226 227 // WithPreference - specify the node or shard the operation should be performed on (default: random). 228 // 229 func (f SearchShards) WithPreference(v string) func(*SearchShardsRequest) { 230 return func(r *SearchShardsRequest) { 231 r.Preference = v 232 } 233 } 234 235 // WithRouting - specific routing value. 236 // 237 func (f SearchShards) WithRouting(v string) func(*SearchShardsRequest) { 238 return func(r *SearchShardsRequest) { 239 r.Routing = v 240 } 241 } 242 243 // WithPretty makes the response body pretty-printed. 244 // 245 func (f SearchShards) WithPretty() func(*SearchShardsRequest) { 246 return func(r *SearchShardsRequest) { 247 r.Pretty = true 248 } 249 } 250 251 // WithHuman makes statistical values human-readable. 252 // 253 func (f SearchShards) WithHuman() func(*SearchShardsRequest) { 254 return func(r *SearchShardsRequest) { 255 r.Human = true 256 } 257 } 258 259 // WithErrorTrace includes the stack trace for errors in the response body. 260 // 261 func (f SearchShards) WithErrorTrace() func(*SearchShardsRequest) { 262 return func(r *SearchShardsRequest) { 263 r.ErrorTrace = true 264 } 265 } 266 267 // WithFilterPath filters the properties of the response body. 268 // 269 func (f SearchShards) WithFilterPath(v ...string) func(*SearchShardsRequest) { 270 return func(r *SearchShardsRequest) { 271 r.FilterPath = v 272 } 273 } 274 275 // WithHeader adds the headers to the HTTP request. 276 // 277 func (f SearchShards) WithHeader(h map[string]string) func(*SearchShardsRequest) { 278 return func(r *SearchShardsRequest) { 279 if r.Header == nil { 280 r.Header = make(http.Header) 281 } 282 for k, v := range h { 283 r.Header.Add(k, v) 284 } 285 } 286 } 287 288 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 289 // 290 func (f SearchShards) WithOpaqueID(s string) func(*SearchShardsRequest) { 291 return func(r *SearchShardsRequest) { 292 if r.Header == nil { 293 r.Header = make(http.Header) 294 } 295 r.Header.Set("X-Opaque-Id", s) 296 } 297 }