Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_collapse_builder.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // CollapseBuilder enables field collapsing on a search request.
  6. // See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-collapse.html
  7. // for details.
  8. type CollapseBuilder struct {
  9. field string
  10. innerHit *InnerHit
  11. maxConcurrentGroupRequests *int
  12. }
  13. // NewCollapseBuilder creates a new CollapseBuilder.
  14. func NewCollapseBuilder(field string) *CollapseBuilder {
  15. return &CollapseBuilder{field: field}
  16. }
  17. // Field to collapse.
  18. func (b *CollapseBuilder) Field(field string) *CollapseBuilder {
  19. b.field = field
  20. return b
  21. }
  22. // InnerHit option to expand the collapsed results.
  23. func (b *CollapseBuilder) InnerHit(innerHit *InnerHit) *CollapseBuilder {
  24. b.innerHit = innerHit
  25. return b
  26. }
  27. // MaxConcurrentGroupRequests is the maximum number of group requests that are
  28. // allowed to be ran concurrently in the inner_hits phase.
  29. func (b *CollapseBuilder) MaxConcurrentGroupRequests(max int) *CollapseBuilder {
  30. b.maxConcurrentGroupRequests = &max
  31. return b
  32. }
  33. // Source generates the JSON serializable fragment for the CollapseBuilder.
  34. func (b *CollapseBuilder) Source() (interface{}, error) {
  35. // {
  36. // "field": "user",
  37. // "inner_hits": {
  38. // "name": "last_tweets",
  39. // "size": 5,
  40. // "sort": [{ "date": "asc" }]
  41. // },
  42. // "max_concurrent_group_searches": 4
  43. // }
  44. src := map[string]interface{}{
  45. "field": b.field,
  46. }
  47. if b.innerHit != nil {
  48. hits, err := b.innerHit.Source()
  49. if err != nil {
  50. return nil, err
  51. }
  52. src["inner_hits"] = hits
  53. }
  54. if b.maxConcurrentGroupRequests != nil {
  55. src["max_concurrent_group_searches"] = *b.maxConcurrentGroupRequests
  56. }
  57. return src, nil
  58. }