/
delete_scroll_id.go
56 lines (50 loc) · 1.19 KB
/
delete_scroll_id.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
/*
Package dump provides functions for saving indexes.
*/
package dump
import (
"bytes"
"encoding/json"
"log"
"os"
elasticsearch "github.com/elastic/go-elasticsearch/v7"
)
/*
deleteScrollID is a function that deletes the scroll_id
passed as an argument from the target Elasticsearch.
*/
func deleteScrollID(es *elasticsearch.Client, scrollID string) {
var buf bytes.Buffer
query := map[string]interface{}{
"scroll_id": scrollID,
}
err := json.NewEncoder(&buf).Encode(query)
if err != nil {
log.SetOutput(os.Stderr)
log.Fatal(err)
}
res, err := es.ClearScroll(
es.ClearScroll.WithBody(&buf),
)
if err != nil {
log.SetOutput(os.Stderr)
log.Fatal(err)
}
defer res.Body.Close()
if res.IsError() {
var e map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
log.SetOutput(os.Stderr)
log.Fatalf("Error parsing the response body: %s", err)
} else {
// Print the response status and error information.
log.SetOutput(os.Stderr)
log.Fatalf("[%s] %s: %s",
res.Status(),
e["error"].(map[string]interface{})["type"],
e["error"].(map[string]interface{})["reason"],
)
}
}
log.Println("The scroll id was successfully deleted.")
}