forked from miku/esbulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadministration.go
102 lines (94 loc) · 2.86 KB
/
administration.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
// Copyright 2021 by Leipzig University Library, http://ub.uni-leipzig.de
// The Finc Authors, http://finc.info
// Martin Czygan, <martin.czygan@uni-leipzig.de>
//
// This file is part of some open source application.
//
// Some open source application is free software: you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// Some open source application is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
//
// @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
package esbulk
import (
"fmt"
"log"
"net/http"
"github.com/segmentio/encoding/json"
"github.com/sethgrid/pester"
)
// FlushIndex flushes index.
func FlushIndex(idx int, options Options) error {
server := options.Servers[idx]
link := fmt.Sprintf("%s/%s/_flush", server, options.Index)
req, err := http.NewRequest("POST", link, nil)
if err != nil {
return err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return err
}
if options.Verbose {
log.Printf("index flushed: %s\n", resp.Status)
}
return nil
}
// GetSettings fetches the settings of the index.
func GetSettings(idx int, options Options) (map[string]interface{}, error) {
server := options.Servers[idx]
link := fmt.Sprintf("%s/%s/_settings", server, options.Index)
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("could not get settings: %s", link)
}
doc := make(map[string]interface{})
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&doc); err != nil {
return nil, fmt.Errorf("failed to decode settings: %v", err)
}
// Example response.
// {
// "ai": {
// "settings": {
// "index": {
// "refresh_interval": "1s",
// "number_of_shards": "5",
// "provided_name": "ai",
// "creation_date": "1523372145102",
// "number_of_replicas": "1",
// "uuid": "5k-id0OZTKKU4A7DeeUNdQ",
// "version": {
// "created": "6020399"
// }
// }
// }
// }
// }
return doc, nil
}