search_aggs_bucket_diversified_sampler.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // DiversifiedSamplerAggregation Like the ‘sampler` aggregation this is a filtering aggregation used to limit any
  6. // sub aggregations’ processing to a sample of the top-scoring documents. The diversified_sampler aggregation adds
  7. // the ability to limit the number of matches that share a common value such as an "author".
  8. //
  9. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-diversified-sampler-aggregation.html
  10. type DiversifiedSamplerAggregation struct {
  11. subAggregations map[string]Aggregation
  12. meta map[string]interface{}
  13. field string
  14. script *Script
  15. shardSize int
  16. maxDocsPerValue int
  17. executionHint string
  18. }
  19. func NewDiversifiedSamplerAggregation() *DiversifiedSamplerAggregation {
  20. return &DiversifiedSamplerAggregation{
  21. shardSize: -1,
  22. maxDocsPerValue: -1,
  23. subAggregations: make(map[string]Aggregation),
  24. }
  25. }
  26. func (a *DiversifiedSamplerAggregation) SubAggregation(name string, subAggregation Aggregation) *DiversifiedSamplerAggregation {
  27. a.subAggregations[name] = subAggregation
  28. return a
  29. }
  30. // Meta sets the meta data to be included in the aggregation response.
  31. func (a *DiversifiedSamplerAggregation) Meta(metaData map[string]interface{}) *DiversifiedSamplerAggregation {
  32. a.meta = metaData
  33. return a
  34. }
  35. // Field on which the aggregation is processed.
  36. func (a *DiversifiedSamplerAggregation) Field(field string) *DiversifiedSamplerAggregation {
  37. a.field = field
  38. return a
  39. }
  40. func (a *DiversifiedSamplerAggregation) Script(script *Script) *DiversifiedSamplerAggregation {
  41. a.script = script
  42. return a
  43. }
  44. // ShardSize sets the maximum number of docs returned from each shard.
  45. func (a *DiversifiedSamplerAggregation) ShardSize(shardSize int) *DiversifiedSamplerAggregation {
  46. a.shardSize = shardSize
  47. return a
  48. }
  49. func (a *DiversifiedSamplerAggregation) MaxDocsPerValue(maxDocsPerValue int) *DiversifiedSamplerAggregation {
  50. a.maxDocsPerValue = maxDocsPerValue
  51. return a
  52. }
  53. func (a *DiversifiedSamplerAggregation) ExecutionHint(hint string) *DiversifiedSamplerAggregation {
  54. a.executionHint = hint
  55. return a
  56. }
  57. func (a *DiversifiedSamplerAggregation) Source() (interface{}, error) {
  58. // Example:
  59. // {
  60. // "aggs": {
  61. // "my_unbiased_sample": {
  62. // "diversified_sampler": {
  63. // "shard_size": 200,
  64. // "field" : "author"
  65. // }
  66. // }
  67. // }
  68. // }
  69. //
  70. // This method returns only the { "diversified_sampler" : { ... } } part.
  71. source := make(map[string]interface{})
  72. opts := make(map[string]interface{})
  73. source["diversified_sampler"] = opts
  74. if a.field != "" {
  75. opts["field"] = a.field
  76. }
  77. if a.script != nil {
  78. src, err := a.script.Source()
  79. if err != nil {
  80. return nil, err
  81. }
  82. opts["script"] = src
  83. }
  84. if a.shardSize >= 0 {
  85. opts["shard_size"] = a.shardSize
  86. }
  87. if a.maxDocsPerValue >= 0 {
  88. opts["max_docs_per_value"] = a.maxDocsPerValue
  89. }
  90. if a.executionHint != "" {
  91. opts["execution_hint"] = a.executionHint
  92. }
  93. // AggregationBuilder (SubAggregations)
  94. if len(a.subAggregations) > 0 {
  95. aggsMap := make(map[string]interface{})
  96. source["aggregations"] = aggsMap
  97. for name, aggregate := range a.subAggregations {
  98. src, err := aggregate.Source()
  99. if err != nil {
  100. return nil, err
  101. }
  102. aggsMap[name] = src
  103. }
  104. }
  105. // Add Meta data if available
  106. if len(a.meta) > 0 {
  107. source["meta"] = a.meta
  108. }
  109. return source, nil
  110. }