github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.scroll.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 "time" 36 ) 37 38 func newScrollFunc(t Transport) Scroll { 39 return func(o ...func(*ScrollRequest)) (*Response, error) { 40 var r = ScrollRequest{} 41 for _, f := range o { 42 f(&r) 43 } 44 return r.Do(r.ctx, t) 45 } 46 } 47 48 // ----- API Definition ------------------------------------------------------- 49 50 // Scroll allows to retrieve a large numbers of results from a single search request. 51 // 52 // 53 type Scroll func(o ...func(*ScrollRequest)) (*Response, error) 54 55 // ScrollRequest configures the Scroll API request. 56 // 57 type ScrollRequest struct { 58 Body io.Reader 59 60 ScrollID string 61 62 RestTotalHitsAsInt *bool 63 Scroll time.Duration 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 ScrollRequest) 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(len("/_search/scroll")) 87 path.WriteString("/_search/scroll") 88 89 params = make(map[string]string) 90 91 if r.RestTotalHitsAsInt != nil { 92 params["rest_total_hits_as_int"] = strconv.FormatBool(*r.RestTotalHitsAsInt) 93 } 94 95 if r.Scroll != 0 { 96 params["scroll"] = formatDuration(r.Scroll) 97 } 98 99 if r.ScrollID != "" { 100 params["scroll_id"] = r.ScrollID 101 } 102 103 if r.Pretty { 104 params["pretty"] = "true" 105 } 106 107 if r.Human { 108 params["human"] = "true" 109 } 110 111 if r.ErrorTrace { 112 params["error_trace"] = "true" 113 } 114 115 if len(r.FilterPath) > 0 { 116 params["filter_path"] = strings.Join(r.FilterPath, ",") 117 } 118 119 req, err := newRequest(method, path.String(), r.Body) 120 if err != nil { 121 return nil, err 122 } 123 124 if len(params) > 0 { 125 q := req.URL.Query() 126 for k, v := range params { 127 q.Set(k, v) 128 } 129 req.URL.RawQuery = q.Encode() 130 } 131 132 if r.Body != nil { 133 req.Header[headerContentType] = headerContentTypeJSON 134 } 135 136 if len(r.Header) > 0 { 137 if len(req.Header) == 0 { 138 req.Header = r.Header 139 } else { 140 for k, vv := range r.Header { 141 for _, v := range vv { 142 req.Header.Add(k, v) 143 } 144 } 145 } 146 } 147 148 if ctx != nil { 149 req = req.WithContext(ctx) 150 } 151 152 res, err := transport.Perform(req) 153 if err != nil { 154 return nil, err 155 } 156 157 response := Response{ 158 StatusCode: res.StatusCode, 159 Body: res.Body, 160 Header: res.Header, 161 } 162 163 return &response, nil 164 } 165 166 // WithContext sets the request context. 167 // 168 func (f Scroll) WithContext(v context.Context) func(*ScrollRequest) { 169 return func(r *ScrollRequest) { 170 r.ctx = v 171 } 172 } 173 174 // WithBody - The scroll ID if not passed by URL or query parameter.. 175 // 176 func (f Scroll) WithBody(v io.Reader) func(*ScrollRequest) { 177 return func(r *ScrollRequest) { 178 r.Body = v 179 } 180 } 181 182 // WithScrollID - the scroll ID. 183 // 184 func (f Scroll) WithScrollID(v string) func(*ScrollRequest) { 185 return func(r *ScrollRequest) { 186 r.ScrollID = v 187 } 188 } 189 190 // WithRestTotalHitsAsInt - indicates whether hits.total should be rendered as an integer or an object in the rest search response. 191 // 192 func (f Scroll) WithRestTotalHitsAsInt(v bool) func(*ScrollRequest) { 193 return func(r *ScrollRequest) { 194 r.RestTotalHitsAsInt = &v 195 } 196 } 197 198 // WithScroll - specify how long a consistent view of the index should be maintained for scrolled search. 199 // 200 func (f Scroll) WithScroll(v time.Duration) func(*ScrollRequest) { 201 return func(r *ScrollRequest) { 202 r.Scroll = v 203 } 204 } 205 206 // WithPretty makes the response body pretty-printed. 207 // 208 func (f Scroll) WithPretty() func(*ScrollRequest) { 209 return func(r *ScrollRequest) { 210 r.Pretty = true 211 } 212 } 213 214 // WithHuman makes statistical values human-readable. 215 // 216 func (f Scroll) WithHuman() func(*ScrollRequest) { 217 return func(r *ScrollRequest) { 218 r.Human = true 219 } 220 } 221 222 // WithErrorTrace includes the stack trace for errors in the response body. 223 // 224 func (f Scroll) WithErrorTrace() func(*ScrollRequest) { 225 return func(r *ScrollRequest) { 226 r.ErrorTrace = true 227 } 228 } 229 230 // WithFilterPath filters the properties of the response body. 231 // 232 func (f Scroll) WithFilterPath(v ...string) func(*ScrollRequest) { 233 return func(r *ScrollRequest) { 234 r.FilterPath = v 235 } 236 } 237 238 // WithHeader adds the headers to the HTTP request. 239 // 240 func (f Scroll) WithHeader(h map[string]string) func(*ScrollRequest) { 241 return func(r *ScrollRequest) { 242 if r.Header == nil { 243 r.Header = make(http.Header) 244 } 245 for k, v := range h { 246 r.Header.Add(k, v) 247 } 248 } 249 } 250 251 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 252 // 253 func (f Scroll) WithOpaqueID(s string) func(*ScrollRequest) { 254 return func(r *ScrollRequest) { 255 if r.Header == nil { 256 r.Header = make(http.Header) 257 } 258 r.Header.Set("X-Opaque-Id", s) 259 } 260 }