-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathelasticClientScroll.go
More file actions
151 lines (129 loc) · 3.24 KB
/
elasticClientScroll.go
File metadata and controls
151 lines (129 loc) · 3.24 KB
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
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/tidwall/gjson"
)
// DoCountRequest will get the number of elements that correspond with the provided query
func (ec *elasticClient) DoCountRequest(ctx context.Context, index string, body []byte) (uint64, error) {
res, err := ec.client.Count(
ec.client.Count.WithIndex(index),
ec.client.Count.WithBody(bytes.NewBuffer(body)),
ec.client.Count.WithContext(ctx),
)
if err != nil {
return 0, err
}
bodyBytes, err := getBytesFromResponse(res)
if err != nil {
return 0, err
}
countRes := gjson.Get(string(bodyBytes), "count")
return countRes.Uint(), nil
}
// DoScrollRequest will perform a documents request using scroll api
func (ec *elasticClient) DoScrollRequest(
ctx context.Context,
index string,
body []byte,
withSource bool,
handlerFunc func(responseBytes []byte) error,
) error {
ec.countScroll++
res, err := ec.client.Search(
ec.client.Search.WithSize(9000),
ec.client.Search.WithScroll(10*time.Minute+time.Duration(ec.countScroll)*time.Millisecond),
ec.client.Search.WithContext(context.Background()),
ec.client.Search.WithIndex(index),
ec.client.Search.WithBody(bytes.NewBuffer(body)),
ec.client.Search.WithSource(strconv.FormatBool(withSource)),
ec.client.Search.WithContext(ctx),
)
if err != nil {
return err
}
bodyBytes, err := getBytesFromResponse(res)
if err != nil {
return err
}
err = handlerFunc(bodyBytes)
if err != nil {
return err
}
scrollID := gjson.Get(string(bodyBytes), "_scroll_id")
return ec.iterateScroll(scrollID.String(), handlerFunc)
}
func (ec *elasticClient) iterateScroll(
scrollID string,
handlerFunc func(responseBytes []byte) error,
) error {
if scrollID == "" {
return nil
}
defer func() {
err := ec.clearScroll(scrollID)
if err != nil {
log.Warn("cannot clear scroll", "error", err)
}
}()
for {
scrollBodyBytes, errScroll := ec.getScrollResponse(scrollID)
if errScroll != nil {
return errScroll
}
numberOfHits := gjson.Get(string(scrollBodyBytes), "hits.hits.#")
if numberOfHits.Int() < 1 {
return nil
}
err := handlerFunc(scrollBodyBytes)
if err != nil {
return err
}
}
}
func (ec *elasticClient) getScrollResponse(scrollID string) ([]byte, error) {
ec.countScroll++
res, err := ec.client.Scroll(
ec.client.Scroll.WithScrollID(scrollID),
ec.client.Scroll.WithScroll(2*time.Minute+time.Duration(ec.countScroll)*time.Millisecond),
)
if err != nil {
return nil, err
}
return getBytesFromResponse(res)
}
func (ec *elasticClient) clearScroll(scrollID string) error {
resp, err := ec.client.ClearScroll(
ec.client.ClearScroll.WithScrollID(scrollID),
)
if err != nil {
return err
}
defer closeBody(resp)
if resp.IsError() && resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("error response: %s", resp)
}
return nil
}
func getBytesFromResponse(res *esapi.Response) ([]byte, error) {
if res.IsError() {
return nil, fmt.Errorf("error response: %s", res)
}
defer closeBody(res)
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return bodyBytes, nil
}
func closeBody(res *esapi.Response) {
if res != nil && res.Body != nil {
_ = res.Body.Close()
}
}