search_queries_slice.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. package elastic
  5. // SliceQuery allows to partition the documents into several slices.
  6. // It is used e.g. to slice scroll operations in Elasticsearch 5.0 or later.
  7. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-scroll.html#sliced-scroll
  8. // for details.
  9. type SliceQuery struct {
  10. field string
  11. id *int
  12. max *int
  13. }
  14. // NewSliceQuery creates a new SliceQuery.
  15. func NewSliceQuery() *SliceQuery {
  16. return &SliceQuery{}
  17. }
  18. // Field is the name of the field to slice against (_uid by default).
  19. func (s *SliceQuery) Field(field string) *SliceQuery {
  20. s.field = field
  21. return s
  22. }
  23. // Id is the id of the slice.
  24. func (s *SliceQuery) Id(id int) *SliceQuery {
  25. s.id = &id
  26. return s
  27. }
  28. // Max is the maximum number of slices.
  29. func (s *SliceQuery) Max(max int) *SliceQuery {
  30. s.max = &max
  31. return s
  32. }
  33. // Source returns the JSON body.
  34. func (s *SliceQuery) Source() (interface{}, error) {
  35. m := make(map[string]interface{})
  36. if s.field != "" {
  37. m["field"] = s.field
  38. }
  39. if s.id != nil {
  40. m["id"] = *s.id
  41. }
  42. if s.max != nil {
  43. m["max"] = *s.max
  44. }
  45. return m, nil
  46. }