forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
202 lines (178 loc) · 5.97 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
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
package elasticsearch
import (
"encoding/json"
"github.com/elastic/beats/libbeat/logp"
)
// QueryResult contains the result of a query.
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"`
Acknowledged bool `json:"acknowledged"`
Matches []string `json:"matches"`
}
// SearchResults contains the results of a search.
type SearchResults struct {
Took int `json:"took"`
Shards json.RawMessage `json:"_shards"`
Hits Hits `json:"hits"`
Aggs map[string]json.RawMessage `json:"aggregations"`
}
// Hits contains the hits.
type Hits struct {
Total int
Hits []json.RawMessage `json:"hits"`
}
// CountResults contains the count of results.
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 withQueryResult(status int, resp []byte, err error) (int, *QueryResult, error) {
if err != nil {
return status, nil, err
}
result, err := readQueryResult(resp)
return status, result, err
}
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"
}
return withQueryResult(es.apiCall(method, index, docType, id, "", params, body))
}
// Ingest pushes a pipeline of updates.
func (es *Connection) Ingest(
index, docType, pipeline, id string,
params map[string]string,
body interface{},
) (int, *QueryResult, error) {
method := "PUT"
if id == "" {
method = "POST"
}
return withQueryResult(es.apiCall(method, index, docType, id, pipeline, params, body))
}
// Refresh an index. Call this after doing inserts or creating/deleting
// indexes in unit tests.
func (es *Connection) Refresh(index string) (int, *QueryResult, error) {
return withQueryResult(es.apiCall("POST", index, "", "_refresh", "", nil, nil))
}
// 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) {
return withQueryResult(es.apiCall("PUT", index, "", "", "", nil, body))
}
// 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) {
return withQueryResult(es.apiCall("DELETE", index, docType, id, "", params, nil))
}
// CreatePipeline create a new ingest pipeline with name id.
// Implements: https://www.elastic.co/guide/en/elasticsearch/reference/current/put-pipeline-api.html
func (es *Connection) CreatePipeline(
id string,
params map[string]string,
body interface{},
) (int, *QueryResult, error) {
return withQueryResult(es.apiCall("PUT", "_ingest", "pipeline", id, "", params, body))
}
// DeletePipeline deletes an ingest pipeline by id.
// Implements: https://www.elastic.co/guide/en/elasticsearch/reference/current/delete-pipeline-api.html
func (es *Connection) DeletePipeline(
id string,
params map[string]string,
) (int, *QueryResult, error) {
return withQueryResult(es.apiCall("DELETE", "_ingest", "pipeline", id, "", params, nil))
}
// SearchURI executes a search request 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
}
// CountSearchURI counts the results for a search request.
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, pipeline 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, pipeline, params, body)
}