| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
) |
| 10 |
|
| 11 |
// SearchSource enables users to build the search source. |
| 12 |
// It resembles the SearchSourceBuilder in Elasticsearch. |
| 13 |
type SearchSource struct { |
| 14 |
query Query |
| 15 |
postQuery Query |
| 16 |
sliceQuery Query |
| 17 |
from int |
| 18 |
size int |
| 19 |
explain *bool |
| 20 |
version *bool |
| 21 |
sorters []Sorter |
| 22 |
trackScores bool |
| 23 |
searchAfterSortValues []interface{} |
| 24 |
minScore *float64 |
| 25 |
timeout string |
| 26 |
terminateAfter *int |
| 27 |
storedFieldNames []string |
| 28 |
docvalueFields []string |
| 29 |
scriptFields []*ScriptField |
| 30 |
fetchSourceContext *FetchSourceContext |
| 31 |
aggregations map[string]Aggregation |
| 32 |
highlight *Highlight |
| 33 |
globalSuggestText string |
| 34 |
suggesters []Suggester |
| 35 |
rescores []*Rescore |
| 36 |
defaultRescoreWindowSize *int |
| 37 |
indexBoosts map[string]float64 |
| 38 |
stats []string |
| 39 |
innerHits map[string]*InnerHit |
| 40 |
collapse *CollapseBuilder |
| 41 |
profile bool |
| 42 |
} |
| 43 |
|
| 44 |
// NewSearchSource initializes a new SearchSource. |
| 45 |
func NewSearchSource() *SearchSource { |
| 46 |
return &SearchSource{ |
| 47 |
from: -1, |
| 48 |
size: -1, |
| 49 |
trackScores: false, |
| 50 |
aggregations: make(map[string]Aggregation), |
| 51 |
indexBoosts: make(map[string]float64), |
| 52 |
innerHits: make(map[string]*InnerHit), |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Query sets the query to use with this search source. |
| 57 |
func (s *SearchSource) Query(query Query) *SearchSource { |
| 58 |
s.query = query |
| 59 |
return s |
| 60 |
} |
| 61 |
|
| 62 |
// Profile specifies that this search source should activate the |
| 63 |
// Profile API for queries made on it. |
| 64 |
func (s *SearchSource) Profile(profile bool) *SearchSource { |
| 65 |
s.profile = profile |
| 66 |
return s |
| 67 |
} |
| 68 |
|
| 69 |
// PostFilter will be executed after the query has been executed and |
| 70 |
// only affects the search hits, not the aggregations. |
| 71 |
// This filter is always executed as the last filtering mechanism. |
| 72 |
func (s *SearchSource) PostFilter(postFilter Query) *SearchSource { |
| 73 |
s.postQuery = postFilter |
| 74 |
return s |
| 75 |
} |
| 76 |
|
| 77 |
// Slice allows partitioning the documents in multiple slices. |
| 78 |
// It is e.g. used to slice a scroll operation, supported in |
| 79 |
// Elasticsearch 5.0 or later. |
| 80 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-scroll.html#sliced-scroll |
| 81 |
// for details. |
| 82 |
func (s *SearchSource) Slice(sliceQuery Query) *SearchSource { |
| 83 |
s.sliceQuery = sliceQuery |
| 84 |
return s |
| 85 |
} |
| 86 |
|
| 87 |
// From index to start the search from. Defaults to 0. |
| 88 |
func (s *SearchSource) From(from int) *SearchSource { |
| 89 |
s.from = from |
| 90 |
return s |
| 91 |
} |
| 92 |
|
| 93 |
// Size is the number of search hits to return. Defaults to 10. |
| 94 |
func (s *SearchSource) Size(size int) *SearchSource { |
| 95 |
s.size = size |
| 96 |
return s |
| 97 |
} |
| 98 |
|
| 99 |
// MinScore sets the minimum score below which docs will be filtered out. |
| 100 |
func (s *SearchSource) MinScore(minScore float64) *SearchSource { |
| 101 |
s.minScore = &minScore |
| 102 |
return s |
| 103 |
} |
| 104 |
|
| 105 |
// Explain indicates whether each search hit should be returned with |
| 106 |
// an explanation of the hit (ranking). |
| 107 |
func (s *SearchSource) Explain(explain bool) *SearchSource { |
| 108 |
s.explain = &explain |
| 109 |
return s |
| 110 |
} |
| 111 |
|
| 112 |
// Version indicates whether each search hit should be returned with |
| 113 |
// a version associated to it. |
| 114 |
func (s *SearchSource) Version(version bool) *SearchSource { |
| 115 |
s.version = &version |
| 116 |
return s |
| 117 |
} |
| 118 |
|
| 119 |
// Timeout controls how long a search is allowed to take, e.g. "1s" or "500ms". |
| 120 |
func (s *SearchSource) Timeout(timeout string) *SearchSource { |
| 121 |
s.timeout = timeout |
| 122 |
return s |
| 123 |
} |
| 124 |
|
| 125 |
// TimeoutInMillis controls how many milliseconds a search is allowed |
| 126 |
// to take before it is canceled. |
| 127 |
func (s *SearchSource) TimeoutInMillis(timeoutInMillis int) *SearchSource { |
| 128 |
s.timeout = fmt.Sprintf("%dms", timeoutInMillis) |
| 129 |
return s |
| 130 |
} |
| 131 |
|
| 132 |
// TerminateAfter specifies the maximum number of documents to collect for |
| 133 |
// each shard, upon reaching which the query execution will terminate early. |
| 134 |
func (s *SearchSource) TerminateAfter(terminateAfter int) *SearchSource { |
| 135 |
s.terminateAfter = &terminateAfter |
| 136 |
return s |
| 137 |
} |
| 138 |
|
| 139 |
// Sort adds a sort order. |
| 140 |
func (s *SearchSource) Sort(field string, ascending bool) *SearchSource { |
| 141 |
s.sorters = append(s.sorters, SortInfo{Field: field, Ascending: ascending}) |
| 142 |
return s |
| 143 |
} |
| 144 |
|
| 145 |
// SortWithInfo adds a sort order. |
| 146 |
func (s *SearchSource) SortWithInfo(info SortInfo) *SearchSource { |
| 147 |
s.sorters = append(s.sorters, info) |
| 148 |
return s |
| 149 |
} |
| 150 |
|
| 151 |
// SortBy adds a sort order. |
| 152 |
func (s *SearchSource) SortBy(sorter ...Sorter) *SearchSource { |
| 153 |
s.sorters = append(s.sorters, sorter...) |
| 154 |
return s |
| 155 |
} |
| 156 |
|
| 157 |
func (s *SearchSource) hasSort() bool { |
| 158 |
return len(s.sorters) > 0 |
| 159 |
} |
| 160 |
|
| 161 |
// TrackScores is applied when sorting and controls if scores will be |
| 162 |
// tracked as well. Defaults to false. |
| 163 |
func (s *SearchSource) TrackScores(trackScores bool) *SearchSource { |
| 164 |
s.trackScores = trackScores |
| 165 |
return s |
| 166 |
} |
| 167 |
|
| 168 |
// SearchAfter allows a different form of pagination by using a live cursor, |
| 169 |
// using the results of the previous page to help the retrieval of the next. |
| 170 |
// |
| 171 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-search-after.html |
| 172 |
func (s *SearchSource) SearchAfter(sortValues ...interface{}) *SearchSource { |
| 173 |
s.searchAfterSortValues = append(s.searchAfterSortValues, sortValues...) |
| 174 |
return s |
| 175 |
} |
| 176 |
|
| 177 |
// Aggregation adds an aggreation to perform as part of the search. |
| 178 |
func (s *SearchSource) Aggregation(name string, aggregation Aggregation) *SearchSource { |
| 179 |
s.aggregations[name] = aggregation |
| 180 |
return s |
| 181 |
} |
| 182 |
|
| 183 |
// DefaultRescoreWindowSize sets the rescore window size for rescores |
| 184 |
// that don't specify their window. |
| 185 |
func (s *SearchSource) DefaultRescoreWindowSize(defaultRescoreWindowSize int) *SearchSource { |
| 186 |
s.defaultRescoreWindowSize = &defaultRescoreWindowSize |
| 187 |
return s |
| 188 |
} |
| 189 |
|
| 190 |
// Highlight adds highlighting to the search. |
| 191 |
func (s *SearchSource) Highlight(highlight *Highlight) *SearchSource { |
| 192 |
s.highlight = highlight |
| 193 |
return s |
| 194 |
} |
| 195 |
|
| 196 |
// Highlighter returns the highlighter. |
| 197 |
func (s *SearchSource) Highlighter() *Highlight { |
| 198 |
if s.highlight == nil { |
| 199 |
s.highlight = NewHighlight() |
| 200 |
} |
| 201 |
return s.highlight |
| 202 |
} |
| 203 |
|
| 204 |
// GlobalSuggestText defines the global text to use with all suggesters. |
| 205 |
// This avoids repetition. |
| 206 |
func (s *SearchSource) GlobalSuggestText(text string) *SearchSource { |
| 207 |
s.globalSuggestText = text |
| 208 |
return s |
| 209 |
} |
| 210 |
|
| 211 |
// Suggester adds a suggester to the search. |
| 212 |
func (s *SearchSource) Suggester(suggester Suggester) *SearchSource { |
| 213 |
s.suggesters = append(s.suggesters, suggester) |
| 214 |
return s |
| 215 |
} |
| 216 |
|
| 217 |
// Rescorer adds a rescorer to the search. |
| 218 |
func (s *SearchSource) Rescorer(rescore *Rescore) *SearchSource { |
| 219 |
s.rescores = append(s.rescores, rescore) |
| 220 |
return s |
| 221 |
} |
| 222 |
|
| 223 |
// ClearRescorers removes all rescorers from the search. |
| 224 |
func (s *SearchSource) ClearRescorers() *SearchSource { |
| 225 |
s.rescores = make([]*Rescore, 0) |
| 226 |
return s |
| 227 |
} |
| 228 |
|
| 229 |
// FetchSource indicates whether the response should contain the stored |
| 230 |
// _source for every hit. |
| 231 |
func (s *SearchSource) FetchSource(fetchSource bool) *SearchSource { |
| 232 |
if s.fetchSourceContext == nil { |
| 233 |
s.fetchSourceContext = NewFetchSourceContext(fetchSource) |
| 234 |
} else { |
| 235 |
s.fetchSourceContext.SetFetchSource(fetchSource) |
| 236 |
} |
| 237 |
return s |
| 238 |
} |
| 239 |
|
| 240 |
// FetchSourceContext indicates how the _source should be fetched. |
| 241 |
func (s *SearchSource) FetchSourceContext(fetchSourceContext *FetchSourceContext) *SearchSource { |
| 242 |
s.fetchSourceContext = fetchSourceContext |
| 243 |
return s |
| 244 |
} |
| 245 |
|
| 246 |
// NoStoredFields indicates that no fields should be loaded, resulting in only |
| 247 |
// id and type to be returned per field. |
| 248 |
func (s *SearchSource) NoStoredFields() *SearchSource { |
| 249 |
s.storedFieldNames = nil |
| 250 |
return s |
| 251 |
} |
| 252 |
|
| 253 |
// StoredField adds a single field to load and return (note, must be stored) as |
| 254 |
// part of the search request. If none are specified, the source of the |
| 255 |
// document will be returned. |
| 256 |
func (s *SearchSource) StoredField(storedFieldName string) *SearchSource { |
| 257 |
s.storedFieldNames = append(s.storedFieldNames, storedFieldName) |
| 258 |
return s |
| 259 |
} |
| 260 |
|
| 261 |
// StoredFields sets the fields to load and return as part of the search request. |
| 262 |
// If none are specified, the source of the document will be returned. |
| 263 |
func (s *SearchSource) StoredFields(storedFieldNames ...string) *SearchSource { |
| 264 |
s.storedFieldNames = append(s.storedFieldNames, storedFieldNames...) |
| 265 |
return s |
| 266 |
} |
| 267 |
|
| 268 |
// DocvalueField adds a single field to load from the field data cache |
| 269 |
// and return as part of the search request. |
| 270 |
func (s *SearchSource) DocvalueField(fieldDataField string) *SearchSource { |
| 271 |
s.docvalueFields = append(s.docvalueFields, fieldDataField) |
| 272 |
return s |
| 273 |
} |
| 274 |
|
| 275 |
// DocvalueFields adds one or more fields to load from the field data cache |
| 276 |
// and return as part of the search request. |
| 277 |
func (s *SearchSource) DocvalueFields(docvalueFields ...string) *SearchSource { |
| 278 |
s.docvalueFields = append(s.docvalueFields, docvalueFields...) |
| 279 |
return s |
| 280 |
} |
| 281 |
|
| 282 |
// ScriptField adds a single script field with the provided script. |
| 283 |
func (s *SearchSource) ScriptField(scriptField *ScriptField) *SearchSource { |
| 284 |
s.scriptFields = append(s.scriptFields, scriptField) |
| 285 |
return s |
| 286 |
} |
| 287 |
|
| 288 |
// ScriptFields adds one or more script fields with the provided scripts. |
| 289 |
func (s *SearchSource) ScriptFields(scriptFields ...*ScriptField) *SearchSource { |
| 290 |
s.scriptFields = append(s.scriptFields, scriptFields...) |
| 291 |
return s |
| 292 |
} |
| 293 |
|
| 294 |
// IndexBoost sets the boost that a specific index will receive when the |
| 295 |
// query is executed against it. |
| 296 |
func (s *SearchSource) IndexBoost(index string, boost float64) *SearchSource { |
| 297 |
s.indexBoosts[index] = boost |
| 298 |
return s |
| 299 |
} |
| 300 |
|
| 301 |
// Stats group this request will be aggregated under. |
| 302 |
func (s *SearchSource) Stats(statsGroup ...string) *SearchSource { |
| 303 |
s.stats = append(s.stats, statsGroup...) |
| 304 |
return s |
| 305 |
} |
| 306 |
|
| 307 |
// InnerHit adds an inner hit to return with the result. |
| 308 |
func (s *SearchSource) InnerHit(name string, innerHit *InnerHit) *SearchSource { |
| 309 |
s.innerHits[name] = innerHit |
| 310 |
return s |
| 311 |
} |
| 312 |
|
| 313 |
// Collapse adds field collapsing. |
| 314 |
func (s *SearchSource) Collapse(collapse *CollapseBuilder) *SearchSource { |
| 315 |
s.collapse = collapse |
| 316 |
return s |
| 317 |
} |
| 318 |
|
| 319 |
// Source returns the serializable JSON for the source builder. |
| 320 |
func (s *SearchSource) Source() (interface{}, error) { |
| 321 |
source := make(map[string]interface{}) |
| 322 |
|
| 323 |
if s.from != -1 { |
| 324 |
source["from"] = s.from |
| 325 |
} |
| 326 |
if s.size != -1 { |
| 327 |
source["size"] = s.size |
| 328 |
} |
| 329 |
if s.timeout != "" { |
| 330 |
source["timeout"] = s.timeout |
| 331 |
} |
| 332 |
if s.terminateAfter != nil { |
| 333 |
source["terminate_after"] = *s.terminateAfter |
| 334 |
} |
| 335 |
if s.query != nil { |
| 336 |
src, err := s.query.Source() |
| 337 |
if err != nil { |
| 338 |
return nil, err |
| 339 |
} |
| 340 |
source["query"] = src |
| 341 |
} |
| 342 |
if s.postQuery != nil { |
| 343 |
src, err := s.postQuery.Source() |
| 344 |
if err != nil { |
| 345 |
return nil, err |
| 346 |
} |
| 347 |
source["post_filter"] = src |
| 348 |
} |
| 349 |
if s.sliceQuery != nil { |
| 350 |
src, err := s.sliceQuery.Source() |
| 351 |
if err != nil { |
| 352 |
return nil, err |
| 353 |
} |
| 354 |
source["slice"] = src |
| 355 |
} |
| 356 |
if s.minScore != nil { |
| 357 |
source["min_score"] = *s.minScore |
| 358 |
} |
| 359 |
if s.version != nil { |
| 360 |
source["version"] = *s.version |
| 361 |
} |
| 362 |
if s.explain != nil { |
| 363 |
source["explain"] = *s.explain |
| 364 |
} |
| 365 |
if s.profile { |
| 366 |
source["profile"] = s.profile |
| 367 |
} |
| 368 |
if s.collapse != nil { |
| 369 |
src, err := s.collapse.Source() |
| 370 |
if err != nil { |
| 371 |
return nil, err |
| 372 |
} |
| 373 |
source["collapse"] = src |
| 374 |
} |
| 375 |
if s.fetchSourceContext != nil { |
| 376 |
src, err := s.fetchSourceContext.Source() |
| 377 |
if err != nil { |
| 378 |
return nil, err |
| 379 |
} |
| 380 |
source["_source"] = src |
| 381 |
} |
| 382 |
|
| 383 |
if s.storedFieldNames != nil { |
| 384 |
switch len(s.storedFieldNames) { |
| 385 |
case 1: |
| 386 |
source["stored_fields"] = s.storedFieldNames[0] |
| 387 |
default: |
| 388 |
source["stored_fields"] = s.storedFieldNames |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if len(s.docvalueFields) > 0 { |
| 393 |
source["docvalue_fields"] = s.docvalueFields |
| 394 |
} |
| 395 |
|
| 396 |
if len(s.scriptFields) > 0 { |
| 397 |
sfmap := make(map[string]interface{}) |
| 398 |
for _, scriptField := range s.scriptFields { |
| 399 |
src, err := scriptField.Source() |
| 400 |
if err != nil { |
| 401 |
return nil, err |
| 402 |
} |
| 403 |
sfmap[scriptField.FieldName] = src |
| 404 |
} |
| 405 |
source["script_fields"] = sfmap |
| 406 |
} |
| 407 |
|
| 408 |
if len(s.sorters) > 0 { |
| 409 |
var sortarr []interface{} |
| 410 |
for _, sorter := range s.sorters { |
| 411 |
src, err := sorter.Source() |
| 412 |
if err != nil { |
| 413 |
return nil, err |
| 414 |
} |
| 415 |
sortarr = append(sortarr, src) |
| 416 |
} |
| 417 |
source["sort"] = sortarr |
| 418 |
} |
| 419 |
|
| 420 |
if s.trackScores { |
| 421 |
source["track_scores"] = s.trackScores |
| 422 |
} |
| 423 |
|
| 424 |
if len(s.searchAfterSortValues) > 0 { |
| 425 |
source["search_after"] = s.searchAfterSortValues |
| 426 |
} |
| 427 |
|
| 428 |
if len(s.indexBoosts) > 0 { |
| 429 |
source["indices_boost"] = s.indexBoosts |
| 430 |
} |
| 431 |
|
| 432 |
if len(s.aggregations) > 0 { |
| 433 |
aggsMap := make(map[string]interface{}) |
| 434 |
for name, aggregate := range s.aggregations { |
| 435 |
src, err := aggregate.Source() |
| 436 |
if err != nil { |
| 437 |
return nil, err |
| 438 |
} |
| 439 |
aggsMap[name] = src |
| 440 |
} |
| 441 |
source["aggregations"] = aggsMap |
| 442 |
} |
| 443 |
|
| 444 |
if s.highlight != nil { |
| 445 |
src, err := s.highlight.Source() |
| 446 |
if err != nil { |
| 447 |
return nil, err |
| 448 |
} |
| 449 |
source["highlight"] = src |
| 450 |
} |
| 451 |
|
| 452 |
if len(s.suggesters) > 0 { |
| 453 |
suggesters := make(map[string]interface{}) |
| 454 |
for _, s := range s.suggesters { |
| 455 |
src, err := s.Source(false) |
| 456 |
if err != nil { |
| 457 |
return nil, err |
| 458 |
} |
| 459 |
suggesters[s.Name()] = src |
| 460 |
} |
| 461 |
if s.globalSuggestText != "" { |
| 462 |
suggesters["text"] = s.globalSuggestText |
| 463 |
} |
| 464 |
source["suggest"] = suggesters |
| 465 |
} |
| 466 |
|
| 467 |
if len(s.rescores) > 0 { |
| 468 |
// Strip empty rescores from request |
| 469 |
var rescores []*Rescore |
| 470 |
for _, r := range s.rescores { |
| 471 |
if !r.IsEmpty() { |
| 472 |
rescores = append(rescores, r) |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
if len(rescores) == 1 { |
| 477 |
rescores[0].defaultRescoreWindowSize = s.defaultRescoreWindowSize |
| 478 |
src, err := rescores[0].Source() |
| 479 |
if err != nil { |
| 480 |
return nil, err |
| 481 |
} |
| 482 |
source["rescore"] = src |
| 483 |
} else { |
| 484 |
var slice []interface{} |
| 485 |
for _, r := range rescores { |
| 486 |
r.defaultRescoreWindowSize = s.defaultRescoreWindowSize |
| 487 |
src, err := r.Source() |
| 488 |
if err != nil { |
| 489 |
return nil, err |
| 490 |
} |
| 491 |
slice = append(slice, src) |
| 492 |
} |
| 493 |
source["rescore"] = slice |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
if len(s.stats) > 0 { |
| 498 |
source["stats"] = s.stats |
| 499 |
} |
| 500 |
|
| 501 |
if len(s.innerHits) > 0 { |
| 502 |
// Top-level inner hits |
| 503 |
// See http://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-request-inner-hits.html#top-level-inner-hits |
| 504 |
// "inner_hits": { |
| 505 |
// "<inner_hits_name>": { |
| 506 |
// "<path|type>": { |
| 507 |
// "<path-to-nested-object-field|child-or-parent-type>": { |
| 508 |
// <inner_hits_body>, |
| 509 |
// [,"inner_hits" : { [<sub_inner_hits>]+ } ]? |
| 510 |
// } |
| 511 |
// } |
| 512 |
// }, |
| 513 |
// [,"<inner_hits_name_2>" : { ... } ]* |
| 514 |
// } |
| 515 |
m := make(map[string]interface{}) |
| 516 |
for name, hit := range s.innerHits { |
| 517 |
if hit.path != "" { |
| 518 |
src, err := hit.Source() |
| 519 |
if err != nil { |
| 520 |
return nil, err |
| 521 |
} |
| 522 |
path := make(map[string]interface{}) |
| 523 |
path[hit.path] = src |
| 524 |
m[name] = map[string]interface{}{ |
| 525 |
"path": path, |
| 526 |
} |
| 527 |
} else if hit.typ != "" { |
| 528 |
src, err := hit.Source() |
| 529 |
if err != nil { |
| 530 |
return nil, err |
| 531 |
} |
| 532 |
typ := make(map[string]interface{}) |
| 533 |
typ[hit.typ] = src |
| 534 |
m[name] = map[string]interface{}{ |
| 535 |
"type": typ, |
| 536 |
} |
| 537 |
} else { |
| 538 |
// TODO the Java client throws here, because either path or typ must be specified |
| 539 |
_ = m |
| 540 |
} |
| 541 |
} |
| 542 |
source["inner_hits"] = m |
| 543 |
} |
| 544 |
|
| 545 |
return source, nil |
| 546 |
} |