search_aggs_bucket_sampler.go 2.9 KB

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