forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
399 lines (338 loc) · 10 KB
/
search.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2012-2014 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/olivere/elastic/uritemplates"
)
// Search for documents in Elasticsearch.
type SearchService struct {
client *Client
searchSource *SearchSource
pretty bool
searchType string
indices []string
queryHint string
routing string
preference string
types []string
debug bool
}
func NewSearchService(client *Client) *SearchService {
builder := &SearchService{
client: client,
searchSource: NewSearchSource(),
debug: false,
pretty: false,
}
return builder
}
func (s *SearchService) Index(index string) *SearchService {
if s.indices == nil {
s.indices = make([]string, 0)
}
s.indices = append(s.indices, index)
return s
}
func (s *SearchService) Indices(indices ...string) *SearchService {
if s.indices == nil {
s.indices = make([]string, 0)
}
s.indices = append(s.indices, indices...)
return s
}
func (s *SearchService) Type(typ string) *SearchService {
if s.types == nil {
s.types = []string{typ}
} else {
s.types = append(s.types, typ)
}
return s
}
func (s *SearchService) Types(types ...string) *SearchService {
if s.types == nil {
s.types = make([]string, len(types))
}
s.types = append(s.types, types...)
return s
}
func (s *SearchService) Pretty(pretty bool) *SearchService {
s.pretty = pretty
return s
}
func (s *SearchService) Debug(debug bool) *SearchService {
s.debug = debug
return s
}
func (s *SearchService) Timeout(timeout string) *SearchService {
s.searchSource = s.searchSource.Timeout(timeout)
return s
}
func (s *SearchService) TimeoutInMillis(timeoutInMillis int) *SearchService {
s.searchSource = s.searchSource.TimeoutInMillis(timeoutInMillis)
return s
}
func (s *SearchService) SearchType(searchType string) *SearchService {
s.searchType = searchType
return s
}
func (s *SearchService) Routing(routing string) *SearchService {
s.routing = routing
return s
}
func (s *SearchService) Preference(preference string) *SearchService {
s.preference = preference
return s
}
func (s *SearchService) QueryHint(queryHint string) *SearchService {
s.queryHint = queryHint
return s
}
func (s *SearchService) Query(query Query) *SearchService {
s.searchSource = s.searchSource.Query(query)
return s
}
// PostFilter is executed as the last filter. It only affects the
// search hits but not facets.
func (s *SearchService) PostFilter(postFilter Filter) *SearchService {
s.searchSource = s.searchSource.PostFilter(postFilter)
return s
}
func (s *SearchService) Highlight(highlight *Highlight) *SearchService {
s.searchSource = s.searchSource.Highlight(highlight)
return s
}
func (s *SearchService) GlobalSuggestText(globalText string) *SearchService {
s.searchSource = s.searchSource.GlobalSuggestText(globalText)
return s
}
func (s *SearchService) Suggester(suggester Suggester) *SearchService {
s.searchSource = s.searchSource.Suggester(suggester)
return s
}
func (s *SearchService) Facet(name string, facet Facet) *SearchService {
s.searchSource = s.searchSource.Facet(name, facet)
return s
}
func (s *SearchService) Aggregation(name string, aggregation Aggregation) *SearchService {
s.searchSource = s.searchSource.Aggregation(name, aggregation)
return s
}
func (s *SearchService) MinScore(minScore float64) *SearchService {
s.searchSource = s.searchSource.MinScore(minScore)
return s
}
func (s *SearchService) From(from int) *SearchService {
s.searchSource = s.searchSource.From(from)
return s
}
func (s *SearchService) Size(size int) *SearchService {
s.searchSource = s.searchSource.Size(size)
return s
}
func (s *SearchService) Explain(explain bool) *SearchService {
s.searchSource = s.searchSource.Explain(explain)
return s
}
func (s *SearchService) Version(version bool) *SearchService {
s.searchSource = s.searchSource.Version(version)
return s
}
func (s *SearchService) Sort(field string, ascending bool) *SearchService {
s.searchSource = s.searchSource.Sort(field, ascending)
return s
}
func (s *SearchService) SortWithInfo(info SortInfo) *SearchService {
s.searchSource = s.searchSource.SortWithInfo(info)
return s
}
func (s *SearchService) Fields(fields ...string) *SearchService {
s.searchSource = s.searchSource.Fields(fields...)
return s
}
func (s *SearchService) Do() (*SearchResult, error) {
// Build url
urls := "/"
// Indices part
indexPart := make([]string, 0)
for _, index := range s.indices {
index, err := uritemplates.Expand("{index}", map[string]string{
"index": index,
})
if err != nil {
return nil, err
}
indexPart = append(indexPart, index)
}
urls += strings.Join(indexPart, ",")
// Types part
if len(s.types) > 0 {
typesPart := make([]string, 0)
for _, typ := range s.types {
typ, err := uritemplates.Expand("{type}", map[string]string{
"type": typ,
})
if err != nil {
return nil, err
}
typesPart = append(typesPart, typ)
}
urls += "/"
urls += strings.Join(typesPart, ",")
}
// Search
urls += "/_search"
// Parameters
params := make(url.Values)
if s.pretty {
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
}
if s.searchType != "" {
params.Set("search_type", s.searchType)
}
if len(params) > 0 {
urls += "?" + params.Encode()
}
// Set up a new request
req, err := s.client.NewRequest("POST", urls)
if err != nil {
return nil, err
}
// Set body
req.SetBodyJson(s.searchSource.Source())
if s.debug {
out, _ := httputil.DumpRequestOut((*http.Request)(req), true)
fmt.Printf("%s\n", string(out))
}
// Get response
res, err := s.client.c.Do((*http.Request)(req))
if err != nil {
return nil, err
}
if err := checkResponse(res); err != nil {
return nil, err
}
defer res.Body.Close()
if s.debug {
out, _ := httputil.DumpResponse(res, true)
fmt.Printf("%s\n", string(out))
}
ret := new(SearchResult)
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
type SearchResult struct {
TookInMillis int64 `json:"took"`
ScrollId string `json:"_scroll_id"`
Hits *SearchHits `json:"hits"`
Suggest SearchSuggest `json:"suggest"`
Facets SearchFacets `json:"facets"`
Aggregations map[string]json.RawMessage `json:"aggregations"` // see search_aggs.go
TimedOut bool `json:"timed_out"`
Error string `json:"error,omitempty"` // used in MultiSearch only
}
// GetAggregation returns the aggregation with the specified name.
func (res *SearchResult) GetAggregation(name string) (*SearchAggregation, bool) {
agg, found := res.Aggregations[name]
if !found {
return nil, false
}
return NewSearchAggregation(name, agg), true
}
type SearchHits struct {
TotalHits int64 `json:"total"`
MaxScore *float64 `json:"max_score"`
Hits []*SearchHit `json:"hits"`
}
type SearchHit struct {
Score *float64 `json:"_score"`
Index string `json:"_index"`
Id string `json:"_id"`
Type string `json:"_type"`
Version *int64 `json:"_version"`
Sort *[]interface{} `json:"sort"`
Highlight SearchHitHighlight `json:"highlight"`
Source *json.RawMessage `json:"_source"`
Fields map[string]interface{} `json:"fields"`
// Explanation
// Shard
// HighlightFields
// SortValues
// MatchedFilters
}
// Suggest
type SearchSuggest map[string][]SearchSuggestion
type SearchSuggestion struct {
Text string `json:"text"`
Offset int `json:"offset"`
Length int `json:"length"`
Options []SearchSuggestionOption `json:"options"`
}
type SearchSuggestionOption struct {
Text string `json:"text"`
Score float32 `json:"score"`
Freq int `json:"freq"`
Payload interface{} `json:"payload"`
}
// Facets
type SearchFacets map[string]*SearchFacet
type SearchFacet struct {
Type string `json:"_type"`
Missing int `json:"missing"`
Total int `json:"total"`
Other int `json:"other"`
Terms []searchFacetTerm `json:"terms"`
Ranges []searchFacetRange `json:"ranges"`
Entries []searchFacetEntry `json:"entries"`
}
type searchFacetTerm struct {
Term string `json:"term"`
Count int `json:"count"`
}
type searchFacetRange struct {
From *float64 `json:"from"`
FromStr *string `json:"from_str"`
To *float64 `json:"to"`
ToStr *string `json:"to_str"`
Count int `json:"count"`
Min *float64 `json:"min"`
Max *float64 `json:"max"`
TotalCount int `json:"total_count"`
Total *float64 `json:"total"`
Mean *float64 `json:"mean"`
}
type searchFacetEntry struct {
// Key for this facet, e.g. in histograms
Key interface{} `json:"key"`
// Date histograms contain the number of milliseconds as date:
// If e.Time = 1293840000000, then: Time.at(1293840000000/1000) => 2011-01-01
Time int64 `json:"time"`
// Number of hits for this facet
Count int `json:"count"`
// Min is either a string like "Infinity" or a float64.
// This is returned with some DateHistogram facets.
Min interface{} `json:"min,omitempty"`
// Max is either a string like "-Infinity" or a float64
// This is returned with some DateHistogram facets.
Max interface{} `json:"max,omitempty"`
// Total is the sum of all entries on the recorded Time
// This is returned with some DateHistogram facets.
Total float64 `json:"total,omitempty"`
// TotalCount is the number of entries for Total
// This is returned with some DateHistogram facets.
TotalCount int `json:"total_count,omitempty"`
// Mean is the mean value
// This is returned with some DateHistogram facets.
Mean float64 `json:"mean,omitempty"`
}
// Aggregations (see search_aggs.go)
// Highlighting
type SearchHitHighlight map[string][]string