|
| 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 | + |
| 5 | +package elastic |
| 6 | + |
| 7 | +// SignificantTextAggregation returns interesting or unusual occurrences |
| 8 | +// of free-text terms in a set. |
| 9 | +// See: https://www.elastic.co/guide/en/elasticsearch/reference/6.0/search-aggregations-bucket-significanttext-aggregation.html |
| 10 | +type SignificantTextAggregation struct { |
| 11 | + field string |
| 12 | + subAggregations map[string]Aggregation |
| 13 | + meta map[string]interface{} |
| 14 | + |
| 15 | + sourceFieldNames []string |
| 16 | + filterDuplicateText *bool |
| 17 | + includeExclude *TermsAggregationIncludeExclude |
| 18 | + filter Query |
| 19 | + bucketCountThresholds *BucketCountThresholds |
| 20 | + significanceHeuristic SignificanceHeuristic |
| 21 | +} |
| 22 | + |
| 23 | +func NewSignificantTextAggregation() *SignificantTextAggregation { |
| 24 | + return &SignificantTextAggregation{ |
| 25 | + subAggregations: make(map[string]Aggregation, 0), |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (a *SignificantTextAggregation) Field(field string) *SignificantTextAggregation { |
| 30 | + a.field = field |
| 31 | + return a |
| 32 | +} |
| 33 | + |
| 34 | +func (a *SignificantTextAggregation) SubAggregation(name string, subAggregation Aggregation) *SignificantTextAggregation { |
| 35 | + a.subAggregations[name] = subAggregation |
| 36 | + return a |
| 37 | +} |
| 38 | + |
| 39 | +// Meta sets the meta data to be included in the aggregation response. |
| 40 | +func (a *SignificantTextAggregation) Meta(metaData map[string]interface{}) *SignificantTextAggregation { |
| 41 | + a.meta = metaData |
| 42 | + return a |
| 43 | +} |
| 44 | + |
| 45 | +func (a *SignificantTextAggregation) SourceFieldNames(names ...string) *SignificantTextAggregation { |
| 46 | + a.sourceFieldNames = names |
| 47 | + return a |
| 48 | +} |
| 49 | + |
| 50 | +func (a *SignificantTextAggregation) FilterDuplicateText(filter bool) *SignificantTextAggregation { |
| 51 | + a.filterDuplicateText = &filter |
| 52 | + return a |
| 53 | +} |
| 54 | + |
| 55 | +func (a *SignificantTextAggregation) MinDocCount(minDocCount int64) *SignificantTextAggregation { |
| 56 | + if a.bucketCountThresholds == nil { |
| 57 | + a.bucketCountThresholds = &BucketCountThresholds{} |
| 58 | + } |
| 59 | + a.bucketCountThresholds.MinDocCount = &minDocCount |
| 60 | + return a |
| 61 | +} |
| 62 | + |
| 63 | +func (a *SignificantTextAggregation) ShardMinDocCount(shardMinDocCount int64) *SignificantTextAggregation { |
| 64 | + if a.bucketCountThresholds == nil { |
| 65 | + a.bucketCountThresholds = &BucketCountThresholds{} |
| 66 | + } |
| 67 | + a.bucketCountThresholds.ShardMinDocCount = &shardMinDocCount |
| 68 | + return a |
| 69 | +} |
| 70 | + |
| 71 | +func (a *SignificantTextAggregation) Size(size int) *SignificantTextAggregation { |
| 72 | + if a.bucketCountThresholds == nil { |
| 73 | + a.bucketCountThresholds = &BucketCountThresholds{} |
| 74 | + } |
| 75 | + a.bucketCountThresholds.RequiredSize = &size |
| 76 | + return a |
| 77 | +} |
| 78 | + |
| 79 | +func (a *SignificantTextAggregation) ShardSize(shardSize int) *SignificantTextAggregation { |
| 80 | + if a.bucketCountThresholds == nil { |
| 81 | + a.bucketCountThresholds = &BucketCountThresholds{} |
| 82 | + } |
| 83 | + a.bucketCountThresholds.ShardSize = &shardSize |
| 84 | + return a |
| 85 | +} |
| 86 | + |
| 87 | +func (a *SignificantTextAggregation) BackgroundFilter(filter Query) *SignificantTextAggregation { |
| 88 | + a.filter = filter |
| 89 | + return a |
| 90 | +} |
| 91 | + |
| 92 | +func (a *SignificantTextAggregation) SignificanceHeuristic(heuristic SignificanceHeuristic) *SignificantTextAggregation { |
| 93 | + a.significanceHeuristic = heuristic |
| 94 | + return a |
| 95 | +} |
| 96 | + |
| 97 | +func (a *SignificantTextAggregation) Include(regexp string) *SignificantTextAggregation { |
| 98 | + if a.includeExclude == nil { |
| 99 | + a.includeExclude = &TermsAggregationIncludeExclude{} |
| 100 | + } |
| 101 | + a.includeExclude.Include = regexp |
| 102 | + return a |
| 103 | +} |
| 104 | + |
| 105 | +func (a *SignificantTextAggregation) IncludeValues(values ...interface{}) *SignificantTextAggregation { |
| 106 | + if a.includeExclude == nil { |
| 107 | + a.includeExclude = &TermsAggregationIncludeExclude{} |
| 108 | + } |
| 109 | + a.includeExclude.IncludeValues = append(a.includeExclude.IncludeValues, values...) |
| 110 | + return a |
| 111 | +} |
| 112 | + |
| 113 | +func (a *SignificantTextAggregation) Exclude(regexp string) *SignificantTextAggregation { |
| 114 | + if a.includeExclude == nil { |
| 115 | + a.includeExclude = &TermsAggregationIncludeExclude{} |
| 116 | + } |
| 117 | + a.includeExclude.Exclude = regexp |
| 118 | + return a |
| 119 | +} |
| 120 | + |
| 121 | +func (a *SignificantTextAggregation) ExcludeValues(values ...interface{}) *SignificantTextAggregation { |
| 122 | + if a.includeExclude == nil { |
| 123 | + a.includeExclude = &TermsAggregationIncludeExclude{} |
| 124 | + } |
| 125 | + a.includeExclude.ExcludeValues = append(a.includeExclude.ExcludeValues, values...) |
| 126 | + return a |
| 127 | +} |
| 128 | + |
| 129 | +func (a *SignificantTextAggregation) Partition(p int) *SignificantTextAggregation { |
| 130 | + if a.includeExclude == nil { |
| 131 | + a.includeExclude = &TermsAggregationIncludeExclude{} |
| 132 | + } |
| 133 | + a.includeExclude.Partition = p |
| 134 | + return a |
| 135 | +} |
| 136 | + |
| 137 | +func (a *SignificantTextAggregation) NumPartitions(n int) *SignificantTextAggregation { |
| 138 | + if a.includeExclude == nil { |
| 139 | + a.includeExclude = &TermsAggregationIncludeExclude{} |
| 140 | + } |
| 141 | + a.includeExclude.NumPartitions = n |
| 142 | + return a |
| 143 | +} |
| 144 | + |
| 145 | +func (a *SignificantTextAggregation) Source() (interface{}, error) { |
| 146 | + // Example: |
| 147 | + // { |
| 148 | + // "query" : { |
| 149 | + // "match" : {"content" : "Bird flu"} |
| 150 | + // }, |
| 151 | + // "aggregations" : { |
| 152 | + // "my_sample" : { |
| 153 | + // "sampler": { |
| 154 | + // "shard_size" : 100 |
| 155 | + // }, |
| 156 | + // "aggregations": { |
| 157 | + // "keywords" : { |
| 158 | + // "significant_text" : { "field" : "content" } |
| 159 | + // } |
| 160 | + // } |
| 161 | + // } |
| 162 | + // } |
| 163 | + // } |
| 164 | + // |
| 165 | + // This method returns only the |
| 166 | + // { "significant_text" : { "field" : "content" } |
| 167 | + // part. |
| 168 | + |
| 169 | + source := make(map[string]interface{}) |
| 170 | + opts := make(map[string]interface{}) |
| 171 | + source["significant_text"] = opts |
| 172 | + |
| 173 | + if a.field != "" { |
| 174 | + opts["field"] = a.field |
| 175 | + } |
| 176 | + if a.bucketCountThresholds != nil { |
| 177 | + if a.bucketCountThresholds.RequiredSize != nil { |
| 178 | + opts["size"] = (*a.bucketCountThresholds).RequiredSize |
| 179 | + } |
| 180 | + if a.bucketCountThresholds.ShardSize != nil { |
| 181 | + opts["shard_size"] = (*a.bucketCountThresholds).ShardSize |
| 182 | + } |
| 183 | + if a.bucketCountThresholds.MinDocCount != nil { |
| 184 | + opts["min_doc_count"] = (*a.bucketCountThresholds).MinDocCount |
| 185 | + } |
| 186 | + if a.bucketCountThresholds.ShardMinDocCount != nil { |
| 187 | + opts["shard_min_doc_count"] = (*a.bucketCountThresholds).ShardMinDocCount |
| 188 | + } |
| 189 | + } |
| 190 | + if a.filter != nil { |
| 191 | + src, err := a.filter.Source() |
| 192 | + if err != nil { |
| 193 | + return nil, err |
| 194 | + } |
| 195 | + opts["background_filter"] = src |
| 196 | + } |
| 197 | + if a.significanceHeuristic != nil { |
| 198 | + name := a.significanceHeuristic.Name() |
| 199 | + src, err := a.significanceHeuristic.Source() |
| 200 | + if err != nil { |
| 201 | + return nil, err |
| 202 | + } |
| 203 | + opts[name] = src |
| 204 | + } |
| 205 | + // Include/Exclude |
| 206 | + if ie := a.includeExclude; ie != nil { |
| 207 | + // Include |
| 208 | + if ie.Include != "" { |
| 209 | + opts["include"] = ie.Include |
| 210 | + } else if len(ie.IncludeValues) > 0 { |
| 211 | + opts["include"] = ie.IncludeValues |
| 212 | + } else if ie.NumPartitions > 0 { |
| 213 | + inc := make(map[string]interface{}) |
| 214 | + inc["partition"] = ie.Partition |
| 215 | + inc["num_partitions"] = ie.NumPartitions |
| 216 | + opts["include"] = inc |
| 217 | + } |
| 218 | + // Exclude |
| 219 | + if ie.Exclude != "" { |
| 220 | + opts["exclude"] = ie.Exclude |
| 221 | + } else if len(ie.ExcludeValues) > 0 { |
| 222 | + opts["exclude"] = ie.ExcludeValues |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + // AggregationBuilder (SubAggregations) |
| 227 | + if len(a.subAggregations) > 0 { |
| 228 | + aggsMap := make(map[string]interface{}) |
| 229 | + source["aggregations"] = aggsMap |
| 230 | + for name, aggregate := range a.subAggregations { |
| 231 | + src, err := aggregate.Source() |
| 232 | + if err != nil { |
| 233 | + return nil, err |
| 234 | + } |
| 235 | + aggsMap[name] = src |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + // Add Meta data if available |
| 240 | + if len(a.meta) > 0 { |
| 241 | + source["meta"] = a.meta |
| 242 | + } |
| 243 | + |
| 244 | + return source, nil |
| 245 | +} |
0 commit comments