search_aggs_significant_terms.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2012-2015 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. // SignificantSignificantTermsAggregation is an aggregation that returns interesting
  6. // or unusual occurrences of terms in a set.
  7. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html
  8. type SignificantTermsAggregation struct {
  9. field string
  10. subAggregations map[string]Aggregation
  11. requiredSize *int
  12. shardSize *int
  13. minDocCount *int
  14. }
  15. func NewSignificantTermsAggregation() SignificantTermsAggregation {
  16. a := SignificantTermsAggregation{
  17. subAggregations: make(map[string]Aggregation, 0),
  18. }
  19. return a
  20. }
  21. func (a SignificantTermsAggregation) Field(field string) SignificantTermsAggregation {
  22. a.field = field
  23. return a
  24. }
  25. func (a SignificantTermsAggregation) SubAggregation(name string, subAggregation Aggregation) SignificantTermsAggregation {
  26. a.subAggregations[name] = subAggregation
  27. return a
  28. }
  29. func (a SignificantTermsAggregation) RequiredSize(requiredSize int) SignificantTermsAggregation {
  30. a.requiredSize = &requiredSize
  31. return a
  32. }
  33. func (a SignificantTermsAggregation) SharedSize(shardSize int) SignificantTermsAggregation {
  34. a.shardSize = &shardSize
  35. return a
  36. }
  37. func (a SignificantTermsAggregation) MinDocCount(minDocCount int) SignificantTermsAggregation {
  38. a.minDocCount = &minDocCount
  39. return a
  40. }
  41. func (a SignificantTermsAggregation) Source() interface{} {
  42. // Example:
  43. // {
  44. // "query" : {
  45. // "terms" : {"force" : [ "British Transport Police" ]}
  46. // },
  47. // "aggregations" : {
  48. // "significantCrimeTypes" : {
  49. // "significant_terms" : { "field" : "crime_type" }
  50. // }
  51. // }
  52. // }
  53. //
  54. // This method returns only the
  55. // { "significant_terms" : { "field" : "crime_type" }
  56. // part.
  57. source := make(map[string]interface{})
  58. opts := make(map[string]interface{})
  59. source["significant_terms"] = opts
  60. if a.field != "" {
  61. opts["field"] = a.field
  62. }
  63. if a.requiredSize != nil {
  64. opts["size"] = *a.requiredSize // not a typo!
  65. }
  66. if a.shardSize != nil {
  67. opts["shard_size"] = *a.shardSize
  68. }
  69. if a.minDocCount != nil {
  70. // TODO(oe) not sure if minDocCount is a typo in ES and should be min_doc_count!
  71. opts["minDocCount"] = *a.minDocCount
  72. }
  73. // AggregationBuilder (SubAggregations)
  74. if len(a.subAggregations) > 0 {
  75. aggsMap := make(map[string]interface{})
  76. source["aggregations"] = aggsMap
  77. for name, aggregate := range a.subAggregations {
  78. aggsMap[name] = aggregate.Source()
  79. }
  80. }
  81. return source
  82. }