-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.go
214 lines (201 loc) · 5.71 KB
/
scroll.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package esdump
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/miku/esdump/stringutil"
"github.com/sethgrid/pester"
)
// SearchResponse is an basic search response with an unparsed source field.
type SearchResponse struct {
Hits struct {
Hits []struct {
Id string `json:"_id"`
Index string `json:"_index"`
Score float64 `json:"_score"`
Source json.RawMessage `json:"_source"`
Type string `json:"_type"`
} `json:"hits"`
MaxScore float64 `json:"max_score"`
TotalValue interface{} `json:"total"`
} `json:"hits"`
ScrollID string `json:"_scroll_id"`
Shards struct {
Failed int64 `json:"failed"`
Skipped int64 `json:"skipped"`
Successful int64 `json:"successful"`
Total int64 `json:"total"`
} `json:"_shards"`
TimedOut bool `json:"timed_out"`
Took int64 `json:"took"`
}
// Total handles elasticsearch v6/v7 api changes.
func (s *SearchResponse) Total() int64 {
switch v := s.Hits.TotalValue.(type) {
case float64:
// v6
return int64(v)
case int:
// v6
return int64(v)
case int64:
// v6
return int64(v)
default:
// v7
if m, ok := s.Hits.TotalValue.(map[string]interface{}); ok {
if value, ok := m["value"]; ok {
if f, ok := value.(float64); ok {
return int64(f)
}
}
}
}
return 0
}
// BasicScroller abstracts iteration over larger result sets via
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-scroll.
// Not using the official library since esapi (may) use POST (and other verbs),
// whereas some endpoints disallow anything but GET requests.
type BasicScroller struct {
Server string // https://search.elastic.io
Index string
Query string // query_string query, will be url escaped, so ok to write: '(f:value) OR (g:"hi there")'
Scroll string // context timeout, e.g. "5m"
Size int // number of docs per request
MaxRetries int // Retry of stranger things, like "unexpected EOF"
id string // will be determined by first request, might change during the scroll
buf bytes.Buffer // buffer for response body
total int // docs already received
err error
started time.Time
}
// initialRequest returns a scroll identifier for a given index and query.
func (s *BasicScroller) initialRequest() (id string, err error) {
s.started = time.Now()
var (
link = fmt.Sprintf(`%s/%s/_search?scroll=%s&size=%d`, s.Server, s.Index, s.Scroll, s.Size)
req *http.Request
resp *http.Response
sr SearchResponse
)
log.Printf("init: %s", link)
req, err = http.NewRequest("GET", link, strings.NewReader(s.Query))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err = pester.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
s.buf.Reset()
tee := io.TeeReader(resp.Body, &s.buf)
if err = json.NewDecoder(tee).Decode(&sr); err != nil {
return
}
s.total += len(sr.Hits.Hits)
log.Printf("init: %s", stringutil.Trim(sr.ScrollID, 25, "..."))
return sr.ScrollID, nil
}
// Next fetches the next batch, which is accessible via Bytes or String
// methods. Returns true, if successful, false if stream ended or an error
// occured. The error can be accessed separately.
func (s *BasicScroller) Next() bool {
if s.err != nil {
return false
}
if s.id == "" {
s.id, s.err = s.initialRequest()
return s.err == nil
}
var (
retry = -5
sleep = 10 * time.Second
sr SearchResponse
)
// Only wrapped in a loop to escape unexpected EOF, other errors are not
// retried.
for {
if retry == s.MaxRetries {
s.err = fmt.Errorf("max retries exceeded")
return false
}
var (
payload = struct {
Scroll string `json:"scroll"`
ScrollID string `json:"scroll_id"`
}{
Scroll: s.Scroll,
ScrollID: s.id,
}
link = fmt.Sprintf("%s/_search/scroll", s.Server)
buf bytes.Buffer
req *http.Request
resp *http.Response
)
enc := json.NewEncoder(&buf)
if s.err = enc.Encode(payload); s.err != nil {
return false
}
req, s.err = http.NewRequest("GET", link, &buf)
if s.err != nil {
return false
}
req.Header.Add("Content-Type", "application/json")
log.Printf("%s [%d] [...]", req.URL, buf.Len())
resp, s.err = pester.Do(req)
if s.err != nil {
return false
}
defer resp.Body.Close()
s.buf.Reset()
_, s.err = io.Copy(&s.buf, resp.Body) // we get an occasional "unexpected EOF" here, but why?
if s.err == nil {
break
}
log.Printf("body was: %s", stringutil.Trim(s.buf.String(), 1024, fmt.Sprintf("... (%d)", s.buf.Len())))
log.Printf("failed to copy response body: %v (%s)", s.err, link)
log.Printf("retrying in %s", sleep)
time.Sleep(sleep)
retry++
}
if s.err = json.Unmarshal(s.buf.Bytes(), &sr); s.err != nil {
return false
}
s.id = sr.ScrollID
s.total += len(sr.Hits.Hits)
log.Printf("fetched=%d/%d (%0.2f%%), received=%d",
s.total, sr.Total(), float64(s.total)/float64(sr.Total())*100, s.buf.Len())
log.Println(stringutil.Shorten(s.id, 40))
if len(sr.Hits.Hits) == 0 && int64(s.total) != sr.Total() {
log.Printf("warn: partial result") // changes in the index?
}
return len(sr.Hits.Hits) > 0 && int64(s.total) <= sr.Total()
}
// Bytes returns the current response body.
func (s *BasicScroller) Bytes() []byte {
return s.buf.Bytes()
}
// String returns current request body as string.
func (s *BasicScroller) String() string {
return s.buf.String()
}
// Err returns any error.
func (s *BasicScroller) Err() error {
return s.err
}
// Elapsed returns the elasped time.
func (s *BasicScroller) Elapsed() time.Duration {
return time.Since(s.started)
}
// Total returns total documents retrieved.
func (s *BasicScroller) Total() int {
return s.total
}