search_aggs_terms.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // TermsAggregation is a multi-bucket value source based aggregation
  6. // where buckets are dynamically built - one per unique value.
  7. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
  8. type TermsAggregation struct {
  9. field string
  10. script string
  11. scriptFile string
  12. lang string
  13. params map[string]interface{}
  14. subAggregations map[string]Aggregation
  15. size *int
  16. shardSize *int
  17. requiredSize *int
  18. minDocCount *int
  19. shardMinDocCount *int
  20. valueType string
  21. order string
  22. orderAsc bool
  23. includePattern string
  24. includeFlags *int
  25. excludePattern string
  26. excludeFlags *int
  27. executionHint string
  28. collectionMode string
  29. showTermDocCountError *bool
  30. includeTerms []string
  31. excludeTerms []string
  32. }
  33. func NewTermsAggregation() TermsAggregation {
  34. a := TermsAggregation{
  35. params: make(map[string]interface{}),
  36. subAggregations: make(map[string]Aggregation, 0),
  37. includeTerms: make([]string, 0),
  38. excludeTerms: make([]string, 0),
  39. }
  40. return a
  41. }
  42. func (a TermsAggregation) Field(field string) TermsAggregation {
  43. a.field = field
  44. return a
  45. }
  46. func (a TermsAggregation) Script(script string) TermsAggregation {
  47. a.script = script
  48. return a
  49. }
  50. func (a TermsAggregation) ScriptFile(scriptFile string) TermsAggregation {
  51. a.scriptFile = scriptFile
  52. return a
  53. }
  54. func (a TermsAggregation) Lang(lang string) TermsAggregation {
  55. a.lang = lang
  56. return a
  57. }
  58. func (a TermsAggregation) Param(name string, value interface{}) TermsAggregation {
  59. a.params[name] = value
  60. return a
  61. }
  62. func (a TermsAggregation) SubAggregation(name string, subAggregation Aggregation) TermsAggregation {
  63. a.subAggregations[name] = subAggregation
  64. return a
  65. }
  66. func (a TermsAggregation) Size(size int) TermsAggregation {
  67. a.size = &size
  68. return a
  69. }
  70. func (a TermsAggregation) RequiredSize(requiredSize int) TermsAggregation {
  71. a.requiredSize = &requiredSize
  72. return a
  73. }
  74. func (a TermsAggregation) ShardSize(shardSize int) TermsAggregation {
  75. a.shardSize = &shardSize
  76. return a
  77. }
  78. func (a TermsAggregation) MinDocCount(minDocCount int) TermsAggregation {
  79. a.minDocCount = &minDocCount
  80. return a
  81. }
  82. func (a TermsAggregation) ShardMinDocCount(shardMinDocCount int) TermsAggregation {
  83. a.shardMinDocCount = &shardMinDocCount
  84. return a
  85. }
  86. func (a TermsAggregation) Include(regexp string) TermsAggregation {
  87. a.includePattern = regexp
  88. return a
  89. }
  90. func (a TermsAggregation) IncludeWithFlags(regexp string, flags int) TermsAggregation {
  91. a.includePattern = regexp
  92. a.includeFlags = &flags
  93. return a
  94. }
  95. func (a TermsAggregation) Exclude(regexp string) TermsAggregation {
  96. a.excludePattern = regexp
  97. return a
  98. }
  99. func (a TermsAggregation) ExcludeWithFlags(regexp string, flags int) TermsAggregation {
  100. a.excludePattern = regexp
  101. a.excludeFlags = &flags
  102. return a
  103. }
  104. // ValueType can be string, long, or double.
  105. func (a TermsAggregation) ValueType(valueType string) TermsAggregation {
  106. a.valueType = valueType
  107. return a
  108. }
  109. func (a TermsAggregation) Order(order string, asc bool) TermsAggregation {
  110. a.order = order
  111. a.orderAsc = asc
  112. return a
  113. }
  114. func (a TermsAggregation) OrderByCount(asc bool) TermsAggregation {
  115. // "order" : { "_count" : "asc" }
  116. a.order = "_count"
  117. a.orderAsc = asc
  118. return a
  119. }
  120. func (a TermsAggregation) OrderByCountAsc() TermsAggregation {
  121. return a.OrderByCount(true)
  122. }
  123. func (a TermsAggregation) OrderByCountDesc() TermsAggregation {
  124. return a.OrderByCount(false)
  125. }
  126. func (a TermsAggregation) OrderByTerm(asc bool) TermsAggregation {
  127. // "order" : { "_term" : "asc" }
  128. a.order = "_term"
  129. a.orderAsc = asc
  130. return a
  131. }
  132. func (a TermsAggregation) OrderByTermAsc() TermsAggregation {
  133. return a.OrderByTerm(true)
  134. }
  135. func (a TermsAggregation) OrderByTermDesc() TermsAggregation {
  136. return a.OrderByTerm(false)
  137. }
  138. // OrderByAggregation creates a bucket ordering strategy which sorts buckets
  139. // based on a single-valued calc get.
  140. func (a TermsAggregation) OrderByAggregation(aggName string, asc bool) TermsAggregation {
  141. // {
  142. // "aggs" : {
  143. // "genders" : {
  144. // "terms" : {
  145. // "field" : "gender",
  146. // "order" : { "avg_height" : "desc" }
  147. // },
  148. // "aggs" : {
  149. // "avg_height" : { "avg" : { "field" : "height" } }
  150. // }
  151. // }
  152. // }
  153. // }
  154. a.order = aggName
  155. a.orderAsc = asc
  156. return a
  157. }
  158. // OrderByAggregationAndMetric creates a bucket ordering strategy which
  159. // sorts buckets based on a multi-valued calc get.
  160. func (a TermsAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) TermsAggregation {
  161. // {
  162. // "aggs" : {
  163. // "genders" : {
  164. // "terms" : {
  165. // "field" : "gender",
  166. // "order" : { "height_stats.avg" : "desc" }
  167. // },
  168. // "aggs" : {
  169. // "height_stats" : { "stats" : { "field" : "height" } }
  170. // }
  171. // }
  172. // }
  173. // }
  174. a.order = aggName + "." + metric
  175. a.orderAsc = asc
  176. return a
  177. }
  178. func (a TermsAggregation) ExecutionHint(hint string) TermsAggregation {
  179. a.executionHint = hint
  180. return a
  181. }
  182. // Collection mode can be depth_first or breadth_first as of 1.4.0.
  183. func (a TermsAggregation) CollectionMode(collectionMode string) TermsAggregation {
  184. a.collectionMode = collectionMode
  185. return a
  186. }
  187. func (a TermsAggregation) ShowTermDocCountError(showTermDocCountError bool) TermsAggregation {
  188. a.showTermDocCountError = &showTermDocCountError
  189. return a
  190. }
  191. func (a TermsAggregation) IncludeTerms(terms ...string) TermsAggregation {
  192. a.includeTerms = append(a.includeTerms, terms...)
  193. return a
  194. }
  195. func (a TermsAggregation) ExcludeTerms(terms ...string) TermsAggregation {
  196. a.excludeTerms = append(a.excludeTerms, terms...)
  197. return a
  198. }
  199. func (a TermsAggregation) Source() interface{} {
  200. // Example:
  201. // {
  202. // "aggs" : {
  203. // "genders" : {
  204. // "terms" : { "field" : "gender" }
  205. // }
  206. // }
  207. // }
  208. // This method returns only the { "terms" : { "field" : "gender" } } part.
  209. source := make(map[string]interface{})
  210. opts := make(map[string]interface{})
  211. source["terms"] = opts
  212. // ValuesSourceAggregationBuilder
  213. if a.field != "" {
  214. opts["field"] = a.field
  215. }
  216. if a.script != "" {
  217. opts["script"] = a.script
  218. }
  219. if a.scriptFile != "" {
  220. opts["script_file"] = a.scriptFile
  221. }
  222. if a.lang != "" {
  223. opts["lang"] = a.lang
  224. }
  225. if len(a.params) > 0 {
  226. opts["params"] = a.params
  227. }
  228. // AggregationBuilder (SubAggregations)
  229. if len(a.subAggregations) > 0 {
  230. aggsMap := make(map[string]interface{})
  231. source["aggregations"] = aggsMap
  232. for name, aggregate := range a.subAggregations {
  233. aggsMap[name] = aggregate.Source()
  234. }
  235. }
  236. // TermsBuilder
  237. if a.size != nil && *a.size >= 0 {
  238. opts["size"] = *a.size
  239. }
  240. if a.shardSize != nil && *a.shardSize >= 0 {
  241. opts["shard_size"] = *a.shardSize
  242. }
  243. if a.requiredSize != nil && *a.requiredSize >= 0 {
  244. opts["required_size"] = *a.requiredSize
  245. }
  246. if a.minDocCount != nil && *a.minDocCount >= 0 {
  247. opts["min_doc_count"] = *a.minDocCount
  248. }
  249. if a.shardMinDocCount != nil && *a.shardMinDocCount >= 0 {
  250. opts["shard_min_doc_count"] = *a.shardMinDocCount
  251. }
  252. if a.showTermDocCountError != nil {
  253. opts["show_term_doc_count_error"] = *a.showTermDocCountError
  254. }
  255. if a.collectionMode != "" {
  256. opts["collect_mode"] = a.collectionMode
  257. }
  258. if a.valueType != "" {
  259. opts["value_type"] = a.valueType
  260. }
  261. if a.order != "" {
  262. o := make(map[string]interface{})
  263. if a.orderAsc {
  264. o[a.order] = "asc"
  265. } else {
  266. o[a.order] = "desc"
  267. }
  268. opts["order"] = o
  269. }
  270. if len(a.includeTerms) > 0 {
  271. opts["include"] = a.includeTerms
  272. }
  273. if a.includePattern != "" {
  274. if a.includeFlags == nil || *a.includeFlags == 0 {
  275. opts["include"] = a.includePattern
  276. } else {
  277. p := make(map[string]interface{})
  278. p["pattern"] = a.includePattern
  279. p["flags"] = *a.includeFlags
  280. opts["include"] = p
  281. }
  282. }
  283. if len(a.excludeTerms) > 0 {
  284. opts["exclude"] = a.excludeTerms
  285. }
  286. if a.excludePattern != "" {
  287. if a.excludeFlags == nil || *a.excludeFlags == 0 {
  288. opts["exclude"] = a.excludePattern
  289. } else {
  290. p := make(map[string]interface{})
  291. p["pattern"] = a.excludePattern
  292. p["flags"] = *a.excludeFlags
  293. opts["exclude"] = p
  294. }
  295. }
  296. if a.executionHint != "" {
  297. opts["execution_hint"] = a.executionHint
  298. }
  299. return source
  300. }