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 5package elastic 6 7// SignificantSignificantTermsAggregation is an aggregation that returns interesting 8// or unusual occurrences of terms in a set. 9// See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html 10type SignificantTermsAggregation struct { 11 field string 12 subAggregations map[string]Aggregation 13 14 minDocCount *int 15 shardMinDocCount *int 16 requiredSize *int 17 shardSize *int 18 filter Filter 19 executionHint string 20} 21 22func NewSignificantTermsAggregation() SignificantTermsAggregation { 23 a := SignificantTermsAggregation{ 24 subAggregations: make(map[string]Aggregation, 0), 25 } 26 return a 27} 28 29func (a SignificantTermsAggregation) Field(field string) SignificantTermsAggregation { 30 a.field = field 31 return a 32} 33 34func (a SignificantTermsAggregation) SubAggregation(name string, subAggregation Aggregation) SignificantTermsAggregation { 35 a.subAggregations[name] = subAggregation 36 return a 37} 38 39func (a SignificantTermsAggregation) MinDocCount(minDocCount int) SignificantTermsAggregation { 40 a.minDocCount = &minDocCount 41 return a 42} 43 44func (a SignificantTermsAggregation) ShardMinDocCount(shardMinDocCount int) SignificantTermsAggregation { 45 a.shardMinDocCount = &shardMinDocCount 46 return a 47} 48 49func (a SignificantTermsAggregation) RequiredSize(requiredSize int) SignificantTermsAggregation { 50 a.requiredSize = &requiredSize 51 return a 52} 53 54func (a SignificantTermsAggregation) ShardSize(shardSize int) SignificantTermsAggregation { 55 a.shardSize = &shardSize 56 return a 57} 58 59func (a SignificantTermsAggregation) BackgroundFilter(filter Filter) SignificantTermsAggregation { 60 a.filter = filter 61 return a 62} 63 64func (a SignificantTermsAggregation) ExecutionHint(hint string) SignificantTermsAggregation { 65 a.executionHint = hint 66 return a 67} 68 69func (a SignificantTermsAggregation) Source() interface{} { 70 // Example: 71 // { 72 // "query" : { 73 // "terms" : {"force" : [ "British Transport Police" ]} 74 // }, 75 // "aggregations" : { 76 // "significantCrimeTypes" : { 77 // "significant_terms" : { "field" : "crime_type" } 78 // } 79 // } 80 // } 81 // 82 // This method returns only the 83 // { "significant_terms" : { "field" : "crime_type" } 84 // part. 85 86 source := make(map[string]interface{}) 87 opts := make(map[string]interface{}) 88 source["significant_terms"] = opts 89 90 if a.field != "" { 91 opts["field"] = a.field 92 } 93 if a.requiredSize != nil { 94 opts["size"] = *a.requiredSize // not a typo! 95 } 96 if a.shardSize != nil { 97 opts["shard_size"] = *a.shardSize 98 } 99 if a.minDocCount != nil { 100 opts["min_doc_count"] = *a.minDocCount 101 } 102 if a.shardMinDocCount != nil { 103 opts["shard_min_doc_count"] = *a.shardMinDocCount 104 } 105 if a.filter != nil { 106 opts["background_filter"] = a.filter.Source() 107 } 108 if a.executionHint != "" { 109 opts["execution_hint"] = a.executionHint 110 } 111 112 // AggregationBuilder (SubAggregations) 113 if len(a.subAggregations) > 0 { 114 aggsMap := make(map[string]interface{}) 115 source["aggregations"] = aggsMap 116 for name, aggregate := range a.subAggregations { 117 aggsMap[name] = aggregate.Source() 118 } 119 } 120 121 return source 122} 123