forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_source.go
495 lines (425 loc) · 11.9 KB
/
search_source.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright 2012-2015 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 (
"fmt"
)
// SearchSource enables users to build the search source.
// It resembles the SearchSourceBuilder in Elasticsearch.
type SearchSource struct {
query Query
postFilter Filter
from int
size int
explain *bool
version *bool
sorts []SortInfo
sorters []Sorter
trackScores bool
minScore *float64
timeout string
fieldNames []string
fieldDataFields []string
scriptFields []*ScriptField
partialFields []*PartialField
fetchSourceContext *FetchSourceContext
facets map[string]Facet
aggregations map[string]Aggregation
highlight *Highlight
globalSuggestText string
suggesters []Suggester
rescores []*Rescore
defaultRescoreWindowSize *int
indexBoosts map[string]float64
stats []string
innerHits map[string]*InnerHit
}
func NewSearchSource() *SearchSource {
return &SearchSource{
from: -1,
size: -1,
trackScores: false,
sorts: make([]SortInfo, 0),
sorters: make([]Sorter, 0),
fieldDataFields: make([]string, 0),
scriptFields: make([]*ScriptField, 0),
partialFields: make([]*PartialField, 0),
facets: make(map[string]Facet),
aggregations: make(map[string]Aggregation),
rescores: make([]*Rescore, 0),
indexBoosts: make(map[string]float64),
stats: make([]string, 0),
innerHits: make(map[string]*InnerHit),
}
}
// Query sets the query to use with this search source.
func (s *SearchSource) Query(query Query) *SearchSource {
s.query = query
return s
}
// PostFilter is executed as the last filter. It only affects the
// search hits but not facets.
func (s *SearchSource) PostFilter(postFilter Filter) *SearchSource {
s.postFilter = postFilter
return s
}
func (s *SearchSource) From(from int) *SearchSource {
s.from = from
return s
}
func (s *SearchSource) Size(size int) *SearchSource {
s.size = size
return s
}
func (s *SearchSource) MinScore(minScore float64) *SearchSource {
s.minScore = &minScore
return s
}
func (s *SearchSource) Explain(explain bool) *SearchSource {
s.explain = &explain
return s
}
func (s *SearchSource) Version(version bool) *SearchSource {
s.version = &version
return s
}
func (s *SearchSource) Timeout(timeout string) *SearchSource {
s.timeout = timeout
return s
}
func (s *SearchSource) TimeoutInMillis(timeoutInMillis int) *SearchSource {
s.timeout = fmt.Sprintf("%dms", timeoutInMillis)
return s
}
func (s *SearchSource) Sort(field string, ascending bool) *SearchSource {
s.sorts = append(s.sorts, SortInfo{Field: field, Ascending: ascending})
return s
}
func (s *SearchSource) SortWithInfo(info SortInfo) *SearchSource {
s.sorts = append(s.sorts, info)
return s
}
func (s *SearchSource) SortBy(sorter ...Sorter) *SearchSource {
s.sorters = append(s.sorters, sorter...)
return s
}
func (s *SearchSource) TrackScores(trackScores bool) *SearchSource {
s.trackScores = trackScores
return s
}
func (s *SearchSource) Facet(name string, facet Facet) *SearchSource {
s.facets[name] = facet
return s
}
func (s *SearchSource) Aggregation(name string, aggregation Aggregation) *SearchSource {
s.aggregations[name] = aggregation
return s
}
func (s *SearchSource) DefaultRescoreWindowSize(defaultRescoreWindowSize int) *SearchSource {
s.defaultRescoreWindowSize = &defaultRescoreWindowSize
return s
}
func (s *SearchSource) Highlight(highlight *Highlight) *SearchSource {
s.highlight = highlight
return s
}
func (s *SearchSource) Highlighter() *Highlight {
if s.highlight == nil {
s.highlight = NewHighlight()
}
return s.highlight
}
func (s *SearchSource) GlobalSuggestText(text string) *SearchSource {
s.globalSuggestText = text
return s
}
func (s *SearchSource) Suggester(suggester Suggester) *SearchSource {
s.suggesters = append(s.suggesters, suggester)
return s
}
func (s *SearchSource) AddRescore(rescore *Rescore) *SearchSource {
s.rescores = append(s.rescores, rescore)
return s
}
func (s *SearchSource) ClearRescores() *SearchSource {
s.rescores = make([]*Rescore, 0)
return s
}
func (s *SearchSource) FetchSource(fetchSource bool) *SearchSource {
if s.fetchSourceContext == nil {
s.fetchSourceContext = NewFetchSourceContext(fetchSource)
} else {
s.fetchSourceContext.SetFetchSource(fetchSource)
}
return s
}
func (s *SearchSource) FetchSourceContext(fetchSourceContext *FetchSourceContext) *SearchSource {
s.fetchSourceContext = fetchSourceContext
return s
}
func (s *SearchSource) Fields(fieldNames ...string) *SearchSource {
if s.fieldNames == nil {
s.fieldNames = make([]string, 0)
}
s.fieldNames = append(s.fieldNames, fieldNames...)
return s
}
func (s *SearchSource) Field(fieldName string) *SearchSource {
if s.fieldNames == nil {
s.fieldNames = make([]string, 0)
}
s.fieldNames = append(s.fieldNames, fieldName)
return s
}
func (s *SearchSource) NoFields() *SearchSource {
s.fieldNames = make([]string, 0)
return s
}
func (s *SearchSource) FieldDataFields(fieldDataFields ...string) *SearchSource {
s.fieldDataFields = append(s.fieldDataFields, fieldDataFields...)
return s
}
func (s *SearchSource) FieldDataField(fieldDataField string) *SearchSource {
s.fieldDataFields = append(s.fieldDataFields, fieldDataField)
return s
}
func (s *SearchSource) ScriptFields(scriptFields ...*ScriptField) *SearchSource {
s.scriptFields = append(s.scriptFields, scriptFields...)
return s
}
func (s *SearchSource) ScriptField(scriptField *ScriptField) *SearchSource {
s.scriptFields = append(s.scriptFields, scriptField)
return s
}
func (s *SearchSource) PartialFields(partialFields ...*PartialField) *SearchSource {
s.partialFields = append(s.partialFields, partialFields...)
return s
}
func (s *SearchSource) PartialField(partialField *PartialField) *SearchSource {
s.partialFields = append(s.partialFields, partialField)
return s
}
func (s *SearchSource) IndexBoost(index string, boost float64) *SearchSource {
s.indexBoosts[index] = boost
return s
}
func (s *SearchSource) Stats(statsGroup ...string) *SearchSource {
s.stats = append(s.stats, statsGroup...)
return s
}
func (s *SearchSource) InnerHit(name string, innerHit *InnerHit) *SearchSource {
s.innerHits[name] = innerHit
return s
}
func (s *SearchSource) Source() interface{} {
source := make(map[string]interface{})
if s.from != -1 {
source["from"] = s.from
}
if s.size != -1 {
source["size"] = s.size
}
if s.timeout != "" {
source["timeout"] = s.timeout
}
if s.query != nil {
source["query"] = s.query.Source()
}
if s.postFilter != nil {
source["post_filter"] = s.postFilter.Source()
}
if s.minScore != nil {
source["min_score"] = *s.minScore
}
if s.version != nil {
source["version"] = *s.version
}
if s.explain != nil {
source["explain"] = *s.explain
}
if s.fetchSourceContext != nil {
source["_source"] = s.fetchSourceContext.Source()
}
if s.fieldNames != nil {
switch len(s.fieldNames) {
case 1:
source["fields"] = s.fieldNames[0]
default:
source["fields"] = s.fieldNames
}
}
if len(s.fieldDataFields) > 0 {
source["fielddata_fields"] = s.fieldDataFields
}
if len(s.partialFields) > 0 {
pfmap := make(map[string]interface{})
for _, partialField := range s.partialFields {
pfmap[partialField.Name] = partialField.Source()
}
source["partial_fields"] = pfmap
}
if len(s.scriptFields) > 0 {
sfmap := make(map[string]interface{})
for _, scriptField := range s.scriptFields {
sfmap[scriptField.FieldName] = scriptField.Source()
}
source["script_fields"] = sfmap
}
if len(s.sorters) > 0 {
sortarr := make([]interface{}, 0)
for _, sorter := range s.sorters {
sortarr = append(sortarr, sorter.Source())
}
source["sort"] = sortarr
} else if len(s.sorts) > 0 {
sortarr := make([]interface{}, 0)
for _, sort := range s.sorts {
sortarr = append(sortarr, sort.Source())
}
source["sort"] = sortarr
}
if s.trackScores {
source["track_scores"] = s.trackScores
}
if len(s.indexBoosts) > 0 {
source["indices_boost"] = s.indexBoosts
}
if len(s.facets) > 0 {
facetsMap := make(map[string]interface{})
for field, facet := range s.facets {
facetsMap[field] = facet.Source()
}
source["facets"] = facetsMap
}
if len(s.aggregations) > 0 {
aggsMap := make(map[string]interface{})
for name, aggregate := range s.aggregations {
aggsMap[name] = aggregate.Source()
}
source["aggregations"] = aggsMap
}
if s.highlight != nil {
source["highlight"] = s.highlight.Source()
}
if len(s.suggesters) > 0 {
suggesters := make(map[string]interface{})
for _, s := range s.suggesters {
suggesters[s.Name()] = s.Source(false)
}
if s.globalSuggestText != "" {
suggesters["text"] = s.globalSuggestText
}
source["suggest"] = suggesters
}
if len(s.rescores) > 0 {
// Strip empty rescores from request
rescores := make([]*Rescore, 0)
for _, r := range s.rescores {
if !r.IsEmpty() {
rescores = append(rescores, r)
}
}
if len(rescores) == 1 {
rescores[0].defaultRescoreWindowSize = s.defaultRescoreWindowSize
source["rescore"] = rescores[0].Source()
} else {
slice := make([]interface{}, 0)
for _, r := range rescores {
r.defaultRescoreWindowSize = s.defaultRescoreWindowSize
slice = append(slice, r.Source())
}
source["rescore"] = slice
}
}
if len(s.stats) > 0 {
source["stats"] = s.stats
}
if len(s.innerHits) > 0 {
// Top-level inner hits
// See http://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-request-inner-hits.html#top-level-inner-hits
// "inner_hits": {
// "<inner_hits_name>": {
// "<path|type>": {
// "<path-to-nested-object-field|child-or-parent-type>": {
// <inner_hits_body>,
// [,"inner_hits" : { [<sub_inner_hits>]+ } ]?
// }
// }
// },
// [,"<inner_hits_name_2>" : { ... } ]*
// }
m := make(map[string]interface{})
for name, hit := range s.innerHits {
if hit.path != "" {
path := make(map[string]interface{})
path[hit.path] = hit.Source()
m[name] = map[string]interface{}{
"path": path,
}
} else if hit.typ != "" {
typ := make(map[string]interface{})
typ[hit.typ] = hit.Source()
m[name] = map[string]interface{}{
"type": typ,
}
} else {
// TODO the Java client throws here, because either path or typ must be specified
}
}
source["inner_hits"] = m
}
return source
}
// -- Script Field --
type ScriptField struct {
FieldName string
script string
lang string
params map[string]interface{}
}
func NewScriptField(fieldName, script, lang string, params map[string]interface{}) *ScriptField {
return &ScriptField{fieldName, script, lang, params}
}
func (f *ScriptField) Source() interface{} {
source := make(map[string]interface{})
source["script"] = f.script
if f.lang != "" {
source["lang"] = f.lang
}
if f.params != nil && len(f.params) > 0 {
source["params"] = f.params
}
return source
}
// -- Partial Field --
type PartialField struct {
Name string
includes []string
excludes []string
}
func NewPartialField(name string, includes, excludes []string) *PartialField {
return &PartialField{name, includes, excludes}
}
func (f *PartialField) Source() interface{} {
source := make(map[string]interface{})
if f.includes != nil {
switch len(f.includes) {
case 0:
case 1:
source["include"] = f.includes[0]
default:
source["include"] = f.includes
}
}
if f.excludes != nil {
switch len(f.excludes) {
case 0:
case 1:
source["exclude"] = f.excludes[0]
default:
source["exclude"] = f.excludes
}
}
return source
}