forked from StephanU/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
177 lines (156 loc) · 4.8 KB
/
api.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
package elasticsearch
import (
"encoding/json"
"github.com/elastic/beats/libbeat/logp"
)
type QueryResult struct {
Ok bool `json:"ok"`
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Source json.RawMessage `json:"_source"`
Version int `json:"_version"`
Found bool `json:"found"`
Exists bool `json:"exists"`
Created bool `json:"created"`
Matches []string `json:"matches"`
}
type SearchResults struct {
Took int `json:"took"`
Shards json.RawMessage `json:"_shards"`
Hits Hits `json:"hits"`
Aggs map[string]json.RawMessage `json:"aggregations"`
}
type Hits struct {
Total int
Hits []json.RawMessage `json:"hits"`
}
type CountResults struct {
Count int `json:"count"`
Shards json.RawMessage `json:"_shards"`
}
func (r QueryResult) String() string {
out, err := json.Marshal(r)
if err != nil {
logp.Warn("failed to marshal QueryResult (%v): %#v", err, r)
return "ERROR"
}
return string(out)
}
func readQueryResult(obj []byte) (*QueryResult, error) {
var result QueryResult
if obj == nil {
return nil, nil
}
err := json.Unmarshal(obj, &result)
if err != nil {
return nil, err
}
return &result, err
}
func readSearchResult(obj []byte) (*SearchResults, error) {
var result SearchResults
if obj == nil {
return nil, nil
}
err := json.Unmarshal(obj, &result)
if err != nil {
return nil, err
}
return &result, err
}
func readCountResult(obj []byte) (*CountResults, error) {
if obj == nil {
return nil, nil
}
var result CountResults
err := json.Unmarshal(obj, &result)
if err != nil {
return nil, err
}
return &result, err
}
// Index adds or updates a typed JSON document in a specified index, making it
// searchable. In case id is empty, a new id is created over a HTTP POST request.
// Otherwise, a HTTP PUT request is issued.
// Implements: http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
func (es *Connection) Index(
index, docType, id string,
params map[string]string,
body interface{},
) (int, *QueryResult, error) {
method := "PUT"
if id == "" {
method = "POST"
}
status, resp, err := es.apiCall(method, index, docType, id, params, body)
if err != nil {
return status, nil, err
}
result, err := readQueryResult(resp)
return status, result, err
}
// Refresh an index. Call this after doing inserts or creating/deleting
// indexes in unit tests.
func (es *Connection) Refresh(index string) (int, *QueryResult, error) {
status, resp, err := es.apiCall("POST", index, "", "_refresh", nil, nil)
if err != nil {
return status, nil, err
}
result, err := readQueryResult(resp)
return status, result, err
}
// CreateIndex creates a new index, optionally with settings and mappings passed in
// the body.
// Implements: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
//
func (es *Connection) CreateIndex(index string, body interface{}) (int, *QueryResult, error) {
status, resp, err := es.apiCall("PUT", index, "", "", nil, body)
if err != nil {
return status, nil, err
}
result, err := readQueryResult(resp)
return status, result, err
}
// Delete deletes a typed JSON document from a specific index based on its id.
// Implements: http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
func (es *Connection) Delete(index string, docType string, id string, params map[string]string) (int, *QueryResult, error) {
status, resp, err := es.apiCall("DELETE", index, docType, id, params, nil)
if err != nil {
return status, nil, err
}
result, err := readQueryResult(resp)
return status, result, err
}
// A search request can be executed purely using a URI by providing request parameters.
// Implements: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html
func (es *Connection) SearchURI(index string, docType string, params map[string]string) (int, *SearchResults, error) {
status, resp, err := es.apiCall("GET", index, docType, "_search", params, nil)
if err != nil {
return status, nil, err
}
result, err := readSearchResult(resp)
return status, result, err
}
func (es *Connection) CountSearchURI(
index string, docType string,
params map[string]string,
) (int, *CountResults, error) {
status, resp, err := es.apiCall("GET", index, docType, "_count", params, nil)
if err != nil {
return status, nil, err
}
result, err := readCountResult(resp)
return status, result, err
}
func (es *Connection) apiCall(
method, index, docType, id string,
params map[string]string,
body interface{},
) (int, []byte, error) {
path, err := makePath(index, docType, id)
if err != nil {
return 0, nil, err
}
return es.request(method, path, params, body)
}