search_aggs.go 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. import (
  6. "bytes"
  7. "encoding/json"
  8. )
  9. // Aggregations can be seen as a unit-of-work that build
  10. // analytic information over a set of documents. It is
  11. // (in many senses) the follow-up of facets in Elasticsearch.
  12. // For more details about aggregations, visit:
  13. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations.html
  14. type Aggregation interface {
  15. // Source returns a JSON-serializable aggregation that is a fragment
  16. // of the request sent to Elasticsearch.
  17. Source() (interface{}, error)
  18. }
  19. // Aggregations is a list of aggregations that are part of a search result.
  20. type Aggregations map[string]*json.RawMessage
  21. // Min returns min aggregation results.
  22. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-min-aggregation.html
  23. func (a Aggregations) Min(name string) (*AggregationValueMetric, bool) {
  24. if raw, found := a[name]; found {
  25. agg := new(AggregationValueMetric)
  26. if raw == nil {
  27. return agg, true
  28. }
  29. if err := json.Unmarshal(*raw, agg); err == nil {
  30. return agg, true
  31. }
  32. }
  33. return nil, false
  34. }
  35. // Max returns max aggregation results.
  36. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-max-aggregation.html
  37. func (a Aggregations) Max(name string) (*AggregationValueMetric, bool) {
  38. if raw, found := a[name]; found {
  39. agg := new(AggregationValueMetric)
  40. if raw == nil {
  41. return agg, true
  42. }
  43. if err := json.Unmarshal(*raw, agg); err == nil {
  44. return agg, true
  45. }
  46. }
  47. return nil, false
  48. }
  49. // Sum returns sum aggregation results.
  50. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-sum-aggregation.html
  51. func (a Aggregations) Sum(name string) (*AggregationValueMetric, bool) {
  52. if raw, found := a[name]; found {
  53. agg := new(AggregationValueMetric)
  54. if raw == nil {
  55. return agg, true
  56. }
  57. if err := json.Unmarshal(*raw, agg); err == nil {
  58. return agg, true
  59. }
  60. }
  61. return nil, false
  62. }
  63. // Avg returns average aggregation results.
  64. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-avg-aggregation.html
  65. func (a Aggregations) Avg(name string) (*AggregationValueMetric, bool) {
  66. if raw, found := a[name]; found {
  67. agg := new(AggregationValueMetric)
  68. if raw == nil {
  69. return agg, true
  70. }
  71. if err := json.Unmarshal(*raw, agg); err == nil {
  72. return agg, true
  73. }
  74. }
  75. return nil, false
  76. }
  77. // ValueCount returns value-count aggregation results.
  78. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-valuecount-aggregation.html
  79. func (a Aggregations) ValueCount(name string) (*AggregationValueMetric, bool) {
  80. if raw, found := a[name]; found {
  81. agg := new(AggregationValueMetric)
  82. if raw == nil {
  83. return agg, true
  84. }
  85. if err := json.Unmarshal(*raw, agg); err == nil {
  86. return agg, true
  87. }
  88. }
  89. return nil, false
  90. }
  91. // Cardinality returns cardinality aggregation results.
  92. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-cardinality-aggregation.html
  93. func (a Aggregations) Cardinality(name string) (*AggregationValueMetric, bool) {
  94. if raw, found := a[name]; found {
  95. agg := new(AggregationValueMetric)
  96. if raw == nil {
  97. return agg, true
  98. }
  99. if err := json.Unmarshal(*raw, agg); err == nil {
  100. return agg, true
  101. }
  102. }
  103. return nil, false
  104. }
  105. // Stats returns stats aggregation results.
  106. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-stats-aggregation.html
  107. func (a Aggregations) Stats(name string) (*AggregationStatsMetric, bool) {
  108. if raw, found := a[name]; found {
  109. agg := new(AggregationStatsMetric)
  110. if raw == nil {
  111. return agg, true
  112. }
  113. if err := json.Unmarshal(*raw, agg); err == nil {
  114. return agg, true
  115. }
  116. }
  117. return nil, false
  118. }
  119. // ExtendedStats returns extended stats aggregation results.
  120. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-extendedstats-aggregation.html
  121. func (a Aggregations) ExtendedStats(name string) (*AggregationExtendedStatsMetric, bool) {
  122. if raw, found := a[name]; found {
  123. agg := new(AggregationExtendedStatsMetric)
  124. if raw == nil {
  125. return agg, true
  126. }
  127. if err := json.Unmarshal(*raw, agg); err == nil {
  128. return agg, true
  129. }
  130. }
  131. return nil, false
  132. }
  133. // MatrixStats returns matrix stats aggregation results.
  134. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-matrix-stats-aggregation.html
  135. func (a Aggregations) MatrixStats(name string) (*AggregationMatrixStats, bool) {
  136. if raw, found := a[name]; found {
  137. agg := new(AggregationMatrixStats)
  138. if raw == nil {
  139. return agg, true
  140. }
  141. if err := json.Unmarshal(*raw, agg); err == nil {
  142. return agg, true
  143. }
  144. }
  145. return nil, false
  146. }
  147. // Percentiles returns percentiles results.
  148. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-percentile-aggregation.html
  149. func (a Aggregations) Percentiles(name string) (*AggregationPercentilesMetric, bool) {
  150. if raw, found := a[name]; found {
  151. agg := new(AggregationPercentilesMetric)
  152. if raw == nil {
  153. return agg, true
  154. }
  155. if err := json.Unmarshal(*raw, agg); err == nil {
  156. return agg, true
  157. }
  158. }
  159. return nil, false
  160. }
  161. // PercentileRanks returns percentile ranks results.
  162. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-percentile-rank-aggregation.html
  163. func (a Aggregations) PercentileRanks(name string) (*AggregationPercentilesMetric, bool) {
  164. if raw, found := a[name]; found {
  165. agg := new(AggregationPercentilesMetric)
  166. if raw == nil {
  167. return agg, true
  168. }
  169. if err := json.Unmarshal(*raw, agg); err == nil {
  170. return agg, true
  171. }
  172. }
  173. return nil, false
  174. }
  175. // TopHits returns top-hits aggregation results.
  176. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-top-hits-aggregation.html
  177. func (a Aggregations) TopHits(name string) (*AggregationTopHitsMetric, bool) {
  178. if raw, found := a[name]; found {
  179. agg := new(AggregationTopHitsMetric)
  180. if raw == nil {
  181. return agg, true
  182. }
  183. if err := json.Unmarshal(*raw, agg); err == nil {
  184. return agg, true
  185. }
  186. }
  187. return nil, false
  188. }
  189. // Global returns global results.
  190. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-global-aggregation.html
  191. func (a Aggregations) Global(name string) (*AggregationSingleBucket, bool) {
  192. if raw, found := a[name]; found {
  193. agg := new(AggregationSingleBucket)
  194. if raw == nil {
  195. return agg, true
  196. }
  197. if err := json.Unmarshal(*raw, agg); err == nil {
  198. return agg, true
  199. }
  200. }
  201. return nil, false
  202. }
  203. // Filter returns filter results.
  204. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-filter-aggregation.html
  205. func (a Aggregations) Filter(name string) (*AggregationSingleBucket, bool) {
  206. if raw, found := a[name]; found {
  207. agg := new(AggregationSingleBucket)
  208. if raw == nil {
  209. return agg, true
  210. }
  211. if err := json.Unmarshal(*raw, agg); err == nil {
  212. return agg, true
  213. }
  214. }
  215. return nil, false
  216. }
  217. // Filters returns filters results.
  218. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-filters-aggregation.html
  219. func (a Aggregations) Filters(name string) (*AggregationBucketFilters, bool) {
  220. if raw, found := a[name]; found {
  221. agg := new(AggregationBucketFilters)
  222. if raw == nil {
  223. return agg, true
  224. }
  225. if err := json.Unmarshal(*raw, agg); err == nil {
  226. return agg, true
  227. }
  228. }
  229. return nil, false
  230. }
  231. // AdjacencyMatrix returning a form of adjacency matrix.
  232. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-adjacency-matrix-aggregation.html
  233. func (a Aggregations) AdjacencyMatrix(name string) (*AggregationBucketAdjacencyMatrix, bool) {
  234. if raw, found := a[name]; found {
  235. agg := new(AggregationBucketAdjacencyMatrix)
  236. if raw == nil {
  237. return agg, true
  238. }
  239. if err := json.Unmarshal(*raw, agg); err == nil {
  240. return agg, true
  241. }
  242. }
  243. return nil, false
  244. }
  245. // Missing returns missing results.
  246. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-missing-aggregation.html
  247. func (a Aggregations) Missing(name string) (*AggregationSingleBucket, bool) {
  248. if raw, found := a[name]; found {
  249. agg := new(AggregationSingleBucket)
  250. if raw == nil {
  251. return agg, true
  252. }
  253. if err := json.Unmarshal(*raw, agg); err == nil {
  254. return agg, true
  255. }
  256. }
  257. return nil, false
  258. }
  259. // Nested returns nested results.
  260. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-nested-aggregation.html
  261. func (a Aggregations) Nested(name string) (*AggregationSingleBucket, bool) {
  262. if raw, found := a[name]; found {
  263. agg := new(AggregationSingleBucket)
  264. if raw == nil {
  265. return agg, true
  266. }
  267. if err := json.Unmarshal(*raw, agg); err == nil {
  268. return agg, true
  269. }
  270. }
  271. return nil, false
  272. }
  273. // ReverseNested returns reverse-nested results.
  274. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-reverse-nested-aggregation.html
  275. func (a Aggregations) ReverseNested(name string) (*AggregationSingleBucket, bool) {
  276. if raw, found := a[name]; found {
  277. agg := new(AggregationSingleBucket)
  278. if raw == nil {
  279. return agg, true
  280. }
  281. if err := json.Unmarshal(*raw, agg); err == nil {
  282. return agg, true
  283. }
  284. }
  285. return nil, false
  286. }
  287. // Children returns children results.
  288. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-children-aggregation.html
  289. func (a Aggregations) Children(name string) (*AggregationSingleBucket, bool) {
  290. if raw, found := a[name]; found {
  291. agg := new(AggregationSingleBucket)
  292. if raw == nil {
  293. return agg, true
  294. }
  295. if err := json.Unmarshal(*raw, agg); err == nil {
  296. return agg, true
  297. }
  298. }
  299. return nil, false
  300. }
  301. // Terms returns terms aggregation results.
  302. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-terms-aggregation.html
  303. func (a Aggregations) Terms(name string) (*AggregationBucketKeyItems, bool) {
  304. if raw, found := a[name]; found {
  305. agg := new(AggregationBucketKeyItems)
  306. if raw == nil {
  307. return agg, true
  308. }
  309. if err := json.Unmarshal(*raw, agg); err == nil {
  310. return agg, true
  311. }
  312. }
  313. return nil, false
  314. }
  315. // SignificantTerms returns significant terms aggregation results.
  316. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-significantterms-aggregation.html
  317. func (a Aggregations) SignificantTerms(name string) (*AggregationBucketSignificantTerms, bool) {
  318. if raw, found := a[name]; found {
  319. agg := new(AggregationBucketSignificantTerms)
  320. if raw == nil {
  321. return agg, true
  322. }
  323. if err := json.Unmarshal(*raw, agg); err == nil {
  324. return agg, true
  325. }
  326. }
  327. return nil, false
  328. }
  329. // Sampler returns sampler aggregation results.
  330. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-sampler-aggregation.html
  331. func (a Aggregations) Sampler(name string) (*AggregationSingleBucket, bool) {
  332. if raw, found := a[name]; found {
  333. agg := new(AggregationSingleBucket)
  334. if raw == nil {
  335. return agg, true
  336. }
  337. if err := json.Unmarshal(*raw, agg); err == nil {
  338. return agg, true
  339. }
  340. }
  341. return nil, false
  342. }
  343. // DiversifiedSampler returns diversified_sampler aggregation results.
  344. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-diversified-sampler-aggregation.html
  345. func (a Aggregations) DiversifiedSampler(name string) (*AggregationSingleBucket, bool) {
  346. if raw, found := a[name]; found {
  347. agg := new(AggregationSingleBucket)
  348. if raw == nil {
  349. return agg, true
  350. }
  351. if err := json.Unmarshal(*raw, agg); err == nil {
  352. return agg, true
  353. }
  354. }
  355. return nil, false
  356. }
  357. // Range returns range aggregation results.
  358. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-range-aggregation.html
  359. func (a Aggregations) Range(name string) (*AggregationBucketRangeItems, bool) {
  360. if raw, found := a[name]; found {
  361. agg := new(AggregationBucketRangeItems)
  362. if raw == nil {
  363. return agg, true
  364. }
  365. if err := json.Unmarshal(*raw, agg); err == nil {
  366. return agg, true
  367. }
  368. }
  369. return nil, false
  370. }
  371. // KeyedRange returns keyed range aggregation results.
  372. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-range-aggregation.html.
  373. func (a Aggregations) KeyedRange(name string) (*AggregationBucketKeyedRangeItems, bool) {
  374. if raw, found := a[name]; found {
  375. agg := new(AggregationBucketKeyedRangeItems)
  376. if raw == nil {
  377. return agg, true
  378. }
  379. if err := json.Unmarshal(*raw, agg); err == nil {
  380. return agg, true
  381. }
  382. }
  383. return nil, false
  384. }
  385. // DateRange returns date range aggregation results.
  386. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-daterange-aggregation.html
  387. func (a Aggregations) DateRange(name string) (*AggregationBucketRangeItems, bool) {
  388. if raw, found := a[name]; found {
  389. agg := new(AggregationBucketRangeItems)
  390. if raw == nil {
  391. return agg, true
  392. }
  393. if err := json.Unmarshal(*raw, agg); err == nil {
  394. return agg, true
  395. }
  396. }
  397. return nil, false
  398. }
  399. // IPRange returns IP range aggregation results.
  400. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-iprange-aggregation.html
  401. func (a Aggregations) IPRange(name string) (*AggregationBucketRangeItems, bool) {
  402. if raw, found := a[name]; found {
  403. agg := new(AggregationBucketRangeItems)
  404. if raw == nil {
  405. return agg, true
  406. }
  407. if err := json.Unmarshal(*raw, agg); err == nil {
  408. return agg, true
  409. }
  410. }
  411. return nil, false
  412. }
  413. // Histogram returns histogram aggregation results.
  414. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-histogram-aggregation.html
  415. func (a Aggregations) Histogram(name string) (*AggregationBucketHistogramItems, bool) {
  416. if raw, found := a[name]; found {
  417. agg := new(AggregationBucketHistogramItems)
  418. if raw == nil {
  419. return agg, true
  420. }
  421. if err := json.Unmarshal(*raw, agg); err == nil {
  422. return agg, true
  423. }
  424. }
  425. return nil, false
  426. }
  427. // DateHistogram returns date histogram aggregation results.
  428. //
  429. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-datehistogram-aggregation.html
  430. func (a Aggregations) DateHistogram(name string) (*AggregationBucketHistogramItems, bool) {
  431. if raw, found := a[name]; found {
  432. agg := new(AggregationBucketHistogramItems)
  433. if raw == nil {
  434. return agg, true
  435. }
  436. if err := json.Unmarshal(*raw, agg); err == nil {
  437. return agg, true
  438. }
  439. }
  440. return nil, false
  441. }
  442. // KeyedDateHistogram returns date histogram aggregation results for keyed responses.
  443. //
  444. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-datehistogram-aggregation.html#_keyed_response_3
  445. func (a Aggregations) KeyedDateHistogram(name string) (*AggregationBucketKeyedHistogramItems, bool) {
  446. if raw, found := a[name]; found {
  447. agg := new(AggregationBucketKeyedHistogramItems)
  448. if raw == nil {
  449. return agg, true
  450. }
  451. if err := json.Unmarshal(*raw, agg); err == nil {
  452. return agg, true
  453. }
  454. }
  455. return nil, false
  456. }
  457. // GeoBounds returns geo-bounds aggregation results.
  458. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-geobounds-aggregation.html
  459. func (a Aggregations) GeoBounds(name string) (*AggregationGeoBoundsMetric, bool) {
  460. if raw, found := a[name]; found {
  461. agg := new(AggregationGeoBoundsMetric)
  462. if raw == nil {
  463. return agg, true
  464. }
  465. if err := json.Unmarshal(*raw, agg); err == nil {
  466. return agg, true
  467. }
  468. }
  469. return nil, false
  470. }
  471. // GeoHash returns geo-hash aggregation results.
  472. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-geohashgrid-aggregation.html
  473. func (a Aggregations) GeoHash(name string) (*AggregationBucketKeyItems, bool) {
  474. if raw, found := a[name]; found {
  475. agg := new(AggregationBucketKeyItems)
  476. if raw == nil {
  477. return agg, true
  478. }
  479. if err := json.Unmarshal(*raw, agg); err == nil {
  480. return agg, true
  481. }
  482. }
  483. return nil, false
  484. }
  485. // GeoCentroid returns geo-centroid aggregation results.
  486. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-metrics-geocentroid-aggregation.html
  487. func (a Aggregations) GeoCentroid(name string) (*AggregationGeoCentroidMetric, bool) {
  488. if raw, found := a[name]; found {
  489. agg := new(AggregationGeoCentroidMetric)
  490. if raw == nil {
  491. return agg, true
  492. }
  493. if err := json.Unmarshal(*raw, agg); err == nil {
  494. return agg, true
  495. }
  496. }
  497. return nil, false
  498. }
  499. // GeoDistance returns geo distance aggregation results.
  500. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-geodistance-aggregation.html
  501. func (a Aggregations) GeoDistance(name string) (*AggregationBucketRangeItems, bool) {
  502. if raw, found := a[name]; found {
  503. agg := new(AggregationBucketRangeItems)
  504. if raw == nil {
  505. return agg, true
  506. }
  507. if err := json.Unmarshal(*raw, agg); err == nil {
  508. return agg, true
  509. }
  510. }
  511. return nil, false
  512. }
  513. // AvgBucket returns average bucket pipeline aggregation results.
  514. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-avg-bucket-aggregation.html
  515. func (a Aggregations) AvgBucket(name string) (*AggregationPipelineSimpleValue, bool) {
  516. if raw, found := a[name]; found {
  517. agg := new(AggregationPipelineSimpleValue)
  518. if raw == nil {
  519. return agg, true
  520. }
  521. if err := json.Unmarshal(*raw, agg); err == nil {
  522. return agg, true
  523. }
  524. }
  525. return nil, false
  526. }
  527. // SumBucket returns sum bucket pipeline aggregation results.
  528. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-sum-bucket-aggregation.html
  529. func (a Aggregations) SumBucket(name string) (*AggregationPipelineSimpleValue, bool) {
  530. if raw, found := a[name]; found {
  531. agg := new(AggregationPipelineSimpleValue)
  532. if raw == nil {
  533. return agg, true
  534. }
  535. if err := json.Unmarshal(*raw, agg); err == nil {
  536. return agg, true
  537. }
  538. }
  539. return nil, false
  540. }
  541. // StatsBucket returns stats bucket pipeline aggregation results.
  542. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-stats-bucket-aggregation.html
  543. func (a Aggregations) StatsBucket(name string) (*AggregationPipelineStatsMetric, bool) {
  544. if raw, found := a[name]; found {
  545. agg := new(AggregationPipelineStatsMetric)
  546. if raw == nil {
  547. return agg, true
  548. }
  549. if err := json.Unmarshal(*raw, agg); err == nil {
  550. return agg, true
  551. }
  552. }
  553. return nil, false
  554. }
  555. // PercentilesBucket returns stats bucket pipeline aggregation results.
  556. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-percentiles-bucket-aggregation.html
  557. func (a Aggregations) PercentilesBucket(name string) (*AggregationPipelinePercentilesMetric, bool) {
  558. if raw, found := a[name]; found {
  559. agg := new(AggregationPipelinePercentilesMetric)
  560. if raw == nil {
  561. return agg, true
  562. }
  563. if err := json.Unmarshal(*raw, agg); err == nil {
  564. return agg, true
  565. }
  566. }
  567. return nil, false
  568. }
  569. // MaxBucket returns maximum bucket pipeline aggregation results.
  570. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-max-bucket-aggregation.html
  571. func (a Aggregations) MaxBucket(name string) (*AggregationPipelineBucketMetricValue, bool) {
  572. if raw, found := a[name]; found {
  573. agg := new(AggregationPipelineBucketMetricValue)
  574. if raw == nil {
  575. return agg, true
  576. }
  577. if err := json.Unmarshal(*raw, agg); err == nil {
  578. return agg, true
  579. }
  580. }
  581. return nil, false
  582. }
  583. // MinBucket returns minimum bucket pipeline aggregation results.
  584. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-min-bucket-aggregation.html
  585. func (a Aggregations) MinBucket(name string) (*AggregationPipelineBucketMetricValue, bool) {
  586. if raw, found := a[name]; found {
  587. agg := new(AggregationPipelineBucketMetricValue)
  588. if raw == nil {
  589. return agg, true
  590. }
  591. if err := json.Unmarshal(*raw, agg); err == nil {
  592. return agg, true
  593. }
  594. }
  595. return nil, false
  596. }
  597. // MovAvg returns moving average pipeline aggregation results.
  598. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html
  599. func (a Aggregations) MovAvg(name string) (*AggregationPipelineSimpleValue, bool) {
  600. if raw, found := a[name]; found {
  601. agg := new(AggregationPipelineSimpleValue)
  602. if raw == nil {
  603. return agg, true
  604. }
  605. if err := json.Unmarshal(*raw, agg); err == nil {
  606. return agg, true
  607. }
  608. }
  609. return nil, false
  610. }
  611. // Derivative returns derivative pipeline aggregation results.
  612. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-derivative-aggregation.html
  613. func (a Aggregations) Derivative(name string) (*AggregationPipelineDerivative, bool) {
  614. if raw, found := a[name]; found {
  615. agg := new(AggregationPipelineDerivative)
  616. if raw == nil {
  617. return agg, true
  618. }
  619. if err := json.Unmarshal(*raw, agg); err == nil {
  620. return agg, true
  621. }
  622. }
  623. return nil, false
  624. }
  625. // CumulativeSum returns a cumulative sum pipeline aggregation results.
  626. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-cumulative-sum-aggregation.html
  627. func (a Aggregations) CumulativeSum(name string) (*AggregationPipelineSimpleValue, bool) {
  628. if raw, found := a[name]; found {
  629. agg := new(AggregationPipelineSimpleValue)
  630. if raw == nil {
  631. return agg, true
  632. }
  633. if err := json.Unmarshal(*raw, agg); err == nil {
  634. return agg, true
  635. }
  636. }
  637. return nil, false
  638. }
  639. // BucketScript returns bucket script pipeline aggregation results.
  640. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-bucket-script-aggregation.html
  641. func (a Aggregations) BucketScript(name string) (*AggregationPipelineSimpleValue, bool) {
  642. if raw, found := a[name]; found {
  643. agg := new(AggregationPipelineSimpleValue)
  644. if raw == nil {
  645. return agg, true
  646. }
  647. if err := json.Unmarshal(*raw, agg); err == nil {
  648. return agg, true
  649. }
  650. }
  651. return nil, false
  652. }
  653. // SerialDiff returns serial differencing pipeline aggregation results.
  654. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-serialdiff-aggregation.html
  655. func (a Aggregations) SerialDiff(name string) (*AggregationPipelineSimpleValue, bool) {
  656. if raw, found := a[name]; found {
  657. agg := new(AggregationPipelineSimpleValue)
  658. if raw == nil {
  659. return agg, true
  660. }
  661. if err := json.Unmarshal(*raw, agg); err == nil {
  662. return agg, true
  663. }
  664. }
  665. return nil, false
  666. }
  667. // -- Single value metric --
  668. // AggregationValueMetric is a single-value metric, returned e.g. by a
  669. // Min or Max aggregation.
  670. type AggregationValueMetric struct {
  671. Aggregations
  672. Value *float64 //`json:"value"`
  673. Meta map[string]interface{} // `json:"meta,omitempty"`
  674. }
  675. // UnmarshalJSON decodes JSON data and initializes an AggregationValueMetric structure.
  676. func (a *AggregationValueMetric) UnmarshalJSON(data []byte) error {
  677. var aggs map[string]*json.RawMessage
  678. if err := json.Unmarshal(data, &aggs); err != nil {
  679. return err
  680. }
  681. if v, ok := aggs["value"]; ok && v != nil {
  682. json.Unmarshal(*v, &a.Value)
  683. }
  684. if v, ok := aggs["meta"]; ok && v != nil {
  685. json.Unmarshal(*v, &a.Meta)
  686. }
  687. a.Aggregations = aggs
  688. return nil
  689. }
  690. // -- Stats metric --
  691. // AggregationStatsMetric is a multi-value metric, returned by a Stats aggregation.
  692. type AggregationStatsMetric struct {
  693. Aggregations
  694. Count int64 // `json:"count"`
  695. Min *float64 //`json:"min,omitempty"`
  696. Max *float64 //`json:"max,omitempty"`
  697. Avg *float64 //`json:"avg,omitempty"`
  698. Sum *float64 //`json:"sum,omitempty"`
  699. Meta map[string]interface{} // `json:"meta,omitempty"`
  700. }
  701. // UnmarshalJSON decodes JSON data and initializes an AggregationStatsMetric structure.
  702. func (a *AggregationStatsMetric) UnmarshalJSON(data []byte) error {
  703. var aggs map[string]*json.RawMessage
  704. if err := json.Unmarshal(data, &aggs); err != nil {
  705. return err
  706. }
  707. if v, ok := aggs["count"]; ok && v != nil {
  708. json.Unmarshal(*v, &a.Count)
  709. }
  710. if v, ok := aggs["min"]; ok && v != nil {
  711. json.Unmarshal(*v, &a.Min)
  712. }
  713. if v, ok := aggs["max"]; ok && v != nil {
  714. json.Unmarshal(*v, &a.Max)
  715. }
  716. if v, ok := aggs["avg"]; ok && v != nil {
  717. json.Unmarshal(*v, &a.Avg)
  718. }
  719. if v, ok := aggs["sum"]; ok && v != nil {
  720. json.Unmarshal(*v, &a.Sum)
  721. }
  722. if v, ok := aggs["meta"]; ok && v != nil {
  723. json.Unmarshal(*v, &a.Meta)
  724. }
  725. a.Aggregations = aggs
  726. return nil
  727. }
  728. // -- Extended stats metric --
  729. // AggregationExtendedStatsMetric is a multi-value metric, returned by an ExtendedStats aggregation.
  730. type AggregationExtendedStatsMetric struct {
  731. Aggregations
  732. Count int64 // `json:"count"`
  733. Min *float64 //`json:"min,omitempty"`
  734. Max *float64 //`json:"max,omitempty"`
  735. Avg *float64 //`json:"avg,omitempty"`
  736. Sum *float64 //`json:"sum,omitempty"`
  737. SumOfSquares *float64 //`json:"sum_of_squares,omitempty"`
  738. Variance *float64 //`json:"variance,omitempty"`
  739. StdDeviation *float64 //`json:"std_deviation,omitempty"`
  740. Meta map[string]interface{} // `json:"meta,omitempty"`
  741. }
  742. // UnmarshalJSON decodes JSON data and initializes an AggregationExtendedStatsMetric structure.
  743. func (a *AggregationExtendedStatsMetric) UnmarshalJSON(data []byte) error {
  744. var aggs map[string]*json.RawMessage
  745. if err := json.Unmarshal(data, &aggs); err != nil {
  746. return err
  747. }
  748. if v, ok := aggs["count"]; ok && v != nil {
  749. json.Unmarshal(*v, &a.Count)
  750. }
  751. if v, ok := aggs["min"]; ok && v != nil {
  752. json.Unmarshal(*v, &a.Min)
  753. }
  754. if v, ok := aggs["max"]; ok && v != nil {
  755. json.Unmarshal(*v, &a.Max)
  756. }
  757. if v, ok := aggs["avg"]; ok && v != nil {
  758. json.Unmarshal(*v, &a.Avg)
  759. }
  760. if v, ok := aggs["sum"]; ok && v != nil {
  761. json.Unmarshal(*v, &a.Sum)
  762. }
  763. if v, ok := aggs["sum_of_squares"]; ok && v != nil {
  764. json.Unmarshal(*v, &a.SumOfSquares)
  765. }
  766. if v, ok := aggs["variance"]; ok && v != nil {
  767. json.Unmarshal(*v, &a.Variance)
  768. }
  769. if v, ok := aggs["std_deviation"]; ok && v != nil {
  770. json.Unmarshal(*v, &a.StdDeviation)
  771. }
  772. if v, ok := aggs["meta"]; ok && v != nil {
  773. json.Unmarshal(*v, &a.Meta)
  774. }
  775. a.Aggregations = aggs
  776. return nil
  777. }
  778. // -- Matrix Stats --
  779. // AggregationMatrixStats is returned by a MatrixStats aggregation.
  780. type AggregationMatrixStats struct {
  781. Aggregations
  782. Fields []*AggregationMatrixStatsField // `json:"field,omitempty"`
  783. Meta map[string]interface{} // `json:"meta,omitempty"`
  784. }
  785. // AggregationMatrixStatsField represents running stats of a single field
  786. // returned from MatrixStats aggregation.
  787. type AggregationMatrixStatsField struct {
  788. Name string `json:"name"`
  789. Count int64 `json:"count"`
  790. Mean float64 `json:"mean,omitempty"`
  791. Variance float64 `json:"variance,omitempty"`
  792. Skewness float64 `json:"skewness,omitempty"`
  793. Kurtosis float64 `json:"kurtosis,omitempty"`
  794. Covariance map[string]float64 `json:"covariance,omitempty"`
  795. Correlation map[string]float64 `json:"correlation,omitempty"`
  796. }
  797. // UnmarshalJSON decodes JSON data and initializes an AggregationMatrixStats structure.
  798. func (a *AggregationMatrixStats) UnmarshalJSON(data []byte) error {
  799. var aggs map[string]*json.RawMessage
  800. if err := json.Unmarshal(data, &aggs); err != nil {
  801. return err
  802. }
  803. if v, ok := aggs["fields"]; ok && v != nil {
  804. // RunningStats for every field
  805. json.Unmarshal(*v, &a.Fields)
  806. }
  807. if v, ok := aggs["meta"]; ok && v != nil {
  808. json.Unmarshal(*v, &a.Meta)
  809. }
  810. a.Aggregations = aggs
  811. return nil
  812. }
  813. // -- Percentiles metric --
  814. // AggregationPercentilesMetric is a multi-value metric, returned by a Percentiles aggregation.
  815. type AggregationPercentilesMetric struct {
  816. Aggregations
  817. Values map[string]float64 // `json:"values"`
  818. Meta map[string]interface{} // `json:"meta,omitempty"`
  819. }
  820. // UnmarshalJSON decodes JSON data and initializes an AggregationPercentilesMetric structure.
  821. func (a *AggregationPercentilesMetric) UnmarshalJSON(data []byte) error {
  822. var aggs map[string]*json.RawMessage
  823. if err := json.Unmarshal(data, &aggs); err != nil {
  824. return err
  825. }
  826. if v, ok := aggs["values"]; ok && v != nil {
  827. json.Unmarshal(*v, &a.Values)
  828. }
  829. if v, ok := aggs["meta"]; ok && v != nil {
  830. json.Unmarshal(*v, &a.Meta)
  831. }
  832. a.Aggregations = aggs
  833. return nil
  834. }
  835. // -- Top-hits metric --
  836. // AggregationTopHitsMetric is a metric returned by a TopHits aggregation.
  837. type AggregationTopHitsMetric struct {
  838. Aggregations
  839. Hits *SearchHits //`json:"hits"`
  840. Meta map[string]interface{} // `json:"meta,omitempty"`
  841. }
  842. // UnmarshalJSON decodes JSON data and initializes an AggregationTopHitsMetric structure.
  843. func (a *AggregationTopHitsMetric) UnmarshalJSON(data []byte) error {
  844. var aggs map[string]*json.RawMessage
  845. if err := json.Unmarshal(data, &aggs); err != nil {
  846. return err
  847. }
  848. a.Aggregations = aggs
  849. a.Hits = new(SearchHits)
  850. if v, ok := aggs["hits"]; ok && v != nil {
  851. json.Unmarshal(*v, &a.Hits)
  852. }
  853. if v, ok := aggs["meta"]; ok && v != nil {
  854. json.Unmarshal(*v, &a.Meta)
  855. }
  856. return nil
  857. }
  858. // -- Geo-bounds metric --
  859. // AggregationGeoBoundsMetric is a metric as returned by a GeoBounds aggregation.
  860. type AggregationGeoBoundsMetric struct {
  861. Aggregations
  862. Bounds struct {
  863. TopLeft struct {
  864. Latitude float64 `json:"lat"`
  865. Longitude float64 `json:"lon"`
  866. } `json:"top_left"`
  867. BottomRight struct {
  868. Latitude float64 `json:"lat"`
  869. Longitude float64 `json:"lon"`
  870. } `json:"bottom_right"`
  871. } `json:"bounds"`
  872. Meta map[string]interface{} // `json:"meta,omitempty"`
  873. }
  874. // UnmarshalJSON decodes JSON data and initializes an AggregationGeoBoundsMetric structure.
  875. func (a *AggregationGeoBoundsMetric) UnmarshalJSON(data []byte) error {
  876. var aggs map[string]*json.RawMessage
  877. if err := json.Unmarshal(data, &aggs); err != nil {
  878. return err
  879. }
  880. if v, ok := aggs["bounds"]; ok && v != nil {
  881. json.Unmarshal(*v, &a.Bounds)
  882. }
  883. if v, ok := aggs["meta"]; ok && v != nil {
  884. json.Unmarshal(*v, &a.Meta)
  885. }
  886. a.Aggregations = aggs
  887. return nil
  888. }
  889. // -- Geo Centroid --
  890. // AggregationGeocentroidMetric is a metric as returned by a GeoCentroid aggregation.
  891. type AggregationGeoCentroidMetric struct {
  892. Aggregations
  893. Location struct {
  894. Latitude float64 `json:"lat"`
  895. Longitude float64 `json:"lon"`
  896. } `json:"location"`
  897. Count int // `json:"count,omitempty"`
  898. Meta map[string]interface{} // `json:"meta,omitempty"`
  899. }
  900. // UnmarshalJSON decodes JSON data and initializes an AggregationGeoCentroidMetric structure.
  901. func (a *AggregationGeoCentroidMetric) UnmarshalJSON(data []byte) error {
  902. var aggs map[string]*json.RawMessage
  903. if err := json.Unmarshal(data, &aggs); err != nil {
  904. return err
  905. }
  906. if v, ok := aggs["location"]; ok && v != nil {
  907. json.Unmarshal(*v, &a.Location)
  908. }
  909. if v, ok := aggs["meta"]; ok && v != nil {
  910. json.Unmarshal(*v, &a.Meta)
  911. }
  912. if v, ok := aggs["count"]; ok && v != nil {
  913. json.Unmarshal(*v, &a.Count)
  914. }
  915. a.Aggregations = aggs
  916. return nil
  917. }
  918. // -- Single bucket --
  919. // AggregationSingleBucket is a single bucket, returned e.g. via an aggregation of type Global.
  920. type AggregationSingleBucket struct {
  921. Aggregations
  922. DocCount int64 // `json:"doc_count"`
  923. Meta map[string]interface{} // `json:"meta,omitempty"`
  924. }
  925. // UnmarshalJSON decodes JSON data and initializes an AggregationSingleBucket structure.
  926. func (a *AggregationSingleBucket) UnmarshalJSON(data []byte) error {
  927. var aggs map[string]*json.RawMessage
  928. if err := json.Unmarshal(data, &aggs); err != nil {
  929. return err
  930. }
  931. if v, ok := aggs["doc_count"]; ok && v != nil {
  932. json.Unmarshal(*v, &a.DocCount)
  933. }
  934. if v, ok := aggs["meta"]; ok && v != nil {
  935. json.Unmarshal(*v, &a.Meta)
  936. }
  937. a.Aggregations = aggs
  938. return nil
  939. }
  940. // -- Bucket range items --
  941. // AggregationBucketRangeItems is a bucket aggregation that is e.g. returned
  942. // with a range aggregation.
  943. type AggregationBucketRangeItems struct {
  944. Aggregations
  945. DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"`
  946. SumOfOtherDocCount int64 //`json:"sum_other_doc_count"`
  947. Buckets []*AggregationBucketRangeItem //`json:"buckets"`
  948. Meta map[string]interface{} // `json:"meta,omitempty"`
  949. }
  950. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItems structure.
  951. func (a *AggregationBucketRangeItems) UnmarshalJSON(data []byte) error {
  952. var aggs map[string]*json.RawMessage
  953. if err := json.Unmarshal(data, &aggs); err != nil {
  954. return err
  955. }
  956. if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil {
  957. json.Unmarshal(*v, &a.DocCountErrorUpperBound)
  958. }
  959. if v, ok := aggs["sum_other_doc_count"]; ok && v != nil {
  960. json.Unmarshal(*v, &a.SumOfOtherDocCount)
  961. }
  962. if v, ok := aggs["buckets"]; ok && v != nil {
  963. json.Unmarshal(*v, &a.Buckets)
  964. }
  965. if v, ok := aggs["meta"]; ok && v != nil {
  966. json.Unmarshal(*v, &a.Meta)
  967. }
  968. a.Aggregations = aggs
  969. return nil
  970. }
  971. // AggregationBucketKeyedRangeItems is a bucket aggregation that is e.g. returned
  972. // with a keyed range aggregation.
  973. type AggregationBucketKeyedRangeItems struct {
  974. Aggregations
  975. DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"`
  976. SumOfOtherDocCount int64 //`json:"sum_other_doc_count"`
  977. Buckets map[string]*AggregationBucketRangeItem //`json:"buckets"`
  978. Meta map[string]interface{} // `json:"meta,omitempty"`
  979. }
  980. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItems structure.
  981. func (a *AggregationBucketKeyedRangeItems) UnmarshalJSON(data []byte) error {
  982. var aggs map[string]*json.RawMessage
  983. if err := json.Unmarshal(data, &aggs); err != nil {
  984. return err
  985. }
  986. if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil {
  987. json.Unmarshal(*v, &a.DocCountErrorUpperBound)
  988. }
  989. if v, ok := aggs["sum_other_doc_count"]; ok && v != nil {
  990. json.Unmarshal(*v, &a.SumOfOtherDocCount)
  991. }
  992. if v, ok := aggs["buckets"]; ok && v != nil {
  993. json.Unmarshal(*v, &a.Buckets)
  994. }
  995. if v, ok := aggs["meta"]; ok && v != nil {
  996. json.Unmarshal(*v, &a.Meta)
  997. }
  998. a.Aggregations = aggs
  999. return nil
  1000. }
  1001. // AggregationBucketRangeItem is a single bucket of an AggregationBucketRangeItems structure.
  1002. type AggregationBucketRangeItem struct {
  1003. Aggregations
  1004. Key string //`json:"key"`
  1005. DocCount int64 //`json:"doc_count"`
  1006. From *float64 //`json:"from"`
  1007. FromAsString string //`json:"from_as_string"`
  1008. To *float64 //`json:"to"`
  1009. ToAsString string //`json:"to_as_string"`
  1010. }
  1011. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItem structure.
  1012. func (a *AggregationBucketRangeItem) UnmarshalJSON(data []byte) error {
  1013. var aggs map[string]*json.RawMessage
  1014. if err := json.Unmarshal(data, &aggs); err != nil {
  1015. return err
  1016. }
  1017. if v, ok := aggs["key"]; ok && v != nil {
  1018. json.Unmarshal(*v, &a.Key)
  1019. }
  1020. if v, ok := aggs["doc_count"]; ok && v != nil {
  1021. json.Unmarshal(*v, &a.DocCount)
  1022. }
  1023. if v, ok := aggs["from"]; ok && v != nil {
  1024. json.Unmarshal(*v, &a.From)
  1025. }
  1026. if v, ok := aggs["from_as_string"]; ok && v != nil {
  1027. json.Unmarshal(*v, &a.FromAsString)
  1028. }
  1029. if v, ok := aggs["to"]; ok && v != nil {
  1030. json.Unmarshal(*v, &a.To)
  1031. }
  1032. if v, ok := aggs["to_as_string"]; ok && v != nil {
  1033. json.Unmarshal(*v, &a.ToAsString)
  1034. }
  1035. a.Aggregations = aggs
  1036. return nil
  1037. }
  1038. // -- Bucket key items --
  1039. // AggregationBucketKeyItems is a bucket aggregation that is e.g. returned
  1040. // with a terms aggregation.
  1041. type AggregationBucketKeyItems struct {
  1042. Aggregations
  1043. DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"`
  1044. SumOfOtherDocCount int64 //`json:"sum_other_doc_count"`
  1045. Buckets []*AggregationBucketKeyItem //`json:"buckets"`
  1046. Meta map[string]interface{} // `json:"meta,omitempty"`
  1047. }
  1048. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyItems structure.
  1049. func (a *AggregationBucketKeyItems) UnmarshalJSON(data []byte) error {
  1050. var aggs map[string]*json.RawMessage
  1051. if err := json.Unmarshal(data, &aggs); err != nil {
  1052. return err
  1053. }
  1054. if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil {
  1055. json.Unmarshal(*v, &a.DocCountErrorUpperBound)
  1056. }
  1057. if v, ok := aggs["sum_other_doc_count"]; ok && v != nil {
  1058. json.Unmarshal(*v, &a.SumOfOtherDocCount)
  1059. }
  1060. if v, ok := aggs["buckets"]; ok && v != nil {
  1061. json.Unmarshal(*v, &a.Buckets)
  1062. }
  1063. if v, ok := aggs["meta"]; ok && v != nil {
  1064. json.Unmarshal(*v, &a.Meta)
  1065. }
  1066. a.Aggregations = aggs
  1067. return nil
  1068. }
  1069. // AggregationBucketKeyItem is a single bucket of an AggregationBucketKeyItems structure.
  1070. type AggregationBucketKeyItem struct {
  1071. Aggregations
  1072. Key interface{} //`json:"key"`
  1073. KeyAsString *string //`json:"key_as_string"`
  1074. KeyNumber json.Number
  1075. DocCount int64 //`json:"doc_count"`
  1076. }
  1077. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyItem structure.
  1078. func (a *AggregationBucketKeyItem) UnmarshalJSON(data []byte) error {
  1079. var aggs map[string]*json.RawMessage
  1080. dec := json.NewDecoder(bytes.NewReader(data))
  1081. dec.UseNumber()
  1082. if err := dec.Decode(&aggs); err != nil {
  1083. return err
  1084. }
  1085. if v, ok := aggs["key"]; ok && v != nil {
  1086. json.Unmarshal(*v, &a.Key)
  1087. json.Unmarshal(*v, &a.KeyNumber)
  1088. }
  1089. if v, ok := aggs["key_as_string"]; ok && v != nil {
  1090. json.Unmarshal(*v, &a.KeyAsString)
  1091. }
  1092. if v, ok := aggs["doc_count"]; ok && v != nil {
  1093. json.Unmarshal(*v, &a.DocCount)
  1094. }
  1095. a.Aggregations = aggs
  1096. return nil
  1097. }
  1098. // -- Bucket types for significant terms --
  1099. // AggregationBucketSignificantTerms is a bucket aggregation returned
  1100. // with a significant terms aggregation.
  1101. type AggregationBucketSignificantTerms struct {
  1102. Aggregations
  1103. DocCount int64 //`json:"doc_count"`
  1104. Buckets []*AggregationBucketSignificantTerm //`json:"buckets"`
  1105. Meta map[string]interface{} // `json:"meta,omitempty"`
  1106. }
  1107. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketSignificantTerms structure.
  1108. func (a *AggregationBucketSignificantTerms) UnmarshalJSON(data []byte) error {
  1109. var aggs map[string]*json.RawMessage
  1110. if err := json.Unmarshal(data, &aggs); err != nil {
  1111. return err
  1112. }
  1113. if v, ok := aggs["doc_count"]; ok && v != nil {
  1114. json.Unmarshal(*v, &a.DocCount)
  1115. }
  1116. if v, ok := aggs["buckets"]; ok && v != nil {
  1117. json.Unmarshal(*v, &a.Buckets)
  1118. }
  1119. if v, ok := aggs["meta"]; ok && v != nil {
  1120. json.Unmarshal(*v, &a.Meta)
  1121. }
  1122. a.Aggregations = aggs
  1123. return nil
  1124. }
  1125. // AggregationBucketSignificantTerm is a single bucket of an AggregationBucketSignificantTerms structure.
  1126. type AggregationBucketSignificantTerm struct {
  1127. Aggregations
  1128. Key string //`json:"key"`
  1129. DocCount int64 //`json:"doc_count"`
  1130. BgCount int64 //`json:"bg_count"`
  1131. Score float64 //`json:"score"`
  1132. }
  1133. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketSignificantTerm structure.
  1134. func (a *AggregationBucketSignificantTerm) UnmarshalJSON(data []byte) error {
  1135. var aggs map[string]*json.RawMessage
  1136. if err := json.Unmarshal(data, &aggs); err != nil {
  1137. return err
  1138. }
  1139. if v, ok := aggs["key"]; ok && v != nil {
  1140. json.Unmarshal(*v, &a.Key)
  1141. }
  1142. if v, ok := aggs["doc_count"]; ok && v != nil {
  1143. json.Unmarshal(*v, &a.DocCount)
  1144. }
  1145. if v, ok := aggs["bg_count"]; ok && v != nil {
  1146. json.Unmarshal(*v, &a.BgCount)
  1147. }
  1148. if v, ok := aggs["score"]; ok && v != nil {
  1149. json.Unmarshal(*v, &a.Score)
  1150. }
  1151. a.Aggregations = aggs
  1152. return nil
  1153. }
  1154. // -- Bucket filters --
  1155. // AggregationBucketFilters is a multi-bucket aggregation that is returned
  1156. // with a filters aggregation.
  1157. type AggregationBucketFilters struct {
  1158. Aggregations
  1159. Buckets []*AggregationBucketKeyItem //`json:"buckets"`
  1160. NamedBuckets map[string]*AggregationBucketKeyItem //`json:"buckets"`
  1161. Meta map[string]interface{} // `json:"meta,omitempty"`
  1162. }
  1163. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketFilters structure.
  1164. func (a *AggregationBucketFilters) UnmarshalJSON(data []byte) error {
  1165. var aggs map[string]*json.RawMessage
  1166. if err := json.Unmarshal(data, &aggs); err != nil {
  1167. return err
  1168. }
  1169. if v, ok := aggs["buckets"]; ok && v != nil {
  1170. json.Unmarshal(*v, &a.Buckets)
  1171. json.Unmarshal(*v, &a.NamedBuckets)
  1172. }
  1173. if v, ok := aggs["meta"]; ok && v != nil {
  1174. json.Unmarshal(*v, &a.Meta)
  1175. }
  1176. a.Aggregations = aggs
  1177. return nil
  1178. }
  1179. // -- Bucket AdjacencyMatrix --
  1180. // AggregationBucketAdjacencyMatrix is a multi-bucket aggregation that is returned
  1181. // with a AdjacencyMatrix aggregation.
  1182. type AggregationBucketAdjacencyMatrix struct {
  1183. Aggregations
  1184. Buckets []*AggregationBucketKeyItem //`json:"buckets"`
  1185. Meta map[string]interface{} // `json:"meta,omitempty"`
  1186. }
  1187. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketAdjacencyMatrix structure.
  1188. func (a *AggregationBucketAdjacencyMatrix) UnmarshalJSON(data []byte) error {
  1189. var aggs map[string]*json.RawMessage
  1190. if err := json.Unmarshal(data, &aggs); err != nil {
  1191. return err
  1192. }
  1193. if v, ok := aggs["buckets"]; ok && v != nil {
  1194. json.Unmarshal(*v, &a.Buckets)
  1195. }
  1196. if v, ok := aggs["meta"]; ok && v != nil {
  1197. json.Unmarshal(*v, &a.Meta)
  1198. }
  1199. a.Aggregations = aggs
  1200. return nil
  1201. }
  1202. // -- Bucket histogram items --
  1203. // AggregationBucketHistogramItems is a bucket aggregation that is returned
  1204. // with a date histogram aggregation.
  1205. type AggregationBucketHistogramItems struct {
  1206. Aggregations
  1207. Buckets []*AggregationBucketHistogramItem //`json:"buckets"`
  1208. Meta map[string]interface{} // `json:"meta,omitempty"`
  1209. }
  1210. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketHistogramItems structure.
  1211. func (a *AggregationBucketHistogramItems) UnmarshalJSON(data []byte) error {
  1212. var aggs map[string]*json.RawMessage
  1213. if err := json.Unmarshal(data, &aggs); err != nil {
  1214. return err
  1215. }
  1216. if v, ok := aggs["buckets"]; ok && v != nil {
  1217. json.Unmarshal(*v, &a.Buckets)
  1218. }
  1219. if v, ok := aggs["meta"]; ok && v != nil {
  1220. json.Unmarshal(*v, &a.Meta)
  1221. }
  1222. a.Aggregations = aggs
  1223. return nil
  1224. }
  1225. // AggregationBucketKeyedHistogramItems is a bucket aggregation that is returned
  1226. // with a (keyed) date histogram aggregation.
  1227. type AggregationBucketKeyedHistogramItems struct {
  1228. Aggregations
  1229. Buckets map[string]*AggregationBucketHistogramItem //`json:"buckets"`
  1230. Meta map[string]interface{} // `json:"meta,omitempty"`
  1231. }
  1232. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyedHistogramItems structure.
  1233. func (a *AggregationBucketKeyedHistogramItems) UnmarshalJSON(data []byte) error {
  1234. var aggs map[string]*json.RawMessage
  1235. if err := json.Unmarshal(data, &aggs); err != nil {
  1236. return err
  1237. }
  1238. if v, ok := aggs["buckets"]; ok && v != nil {
  1239. json.Unmarshal(*v, &a.Buckets)
  1240. }
  1241. if v, ok := aggs["meta"]; ok && v != nil {
  1242. json.Unmarshal(*v, &a.Meta)
  1243. }
  1244. a.Aggregations = aggs
  1245. return nil
  1246. }
  1247. // AggregationBucketHistogramItem is a single bucket of an AggregationBucketHistogramItems structure.
  1248. type AggregationBucketHistogramItem struct {
  1249. Aggregations
  1250. Key float64 //`json:"key"`
  1251. KeyAsString *string //`json:"key_as_string"`
  1252. DocCount int64 //`json:"doc_count"`
  1253. }
  1254. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketHistogramItem structure.
  1255. func (a *AggregationBucketHistogramItem) UnmarshalJSON(data []byte) error {
  1256. var aggs map[string]*json.RawMessage
  1257. if err := json.Unmarshal(data, &aggs); err != nil {
  1258. return err
  1259. }
  1260. if v, ok := aggs["key"]; ok && v != nil {
  1261. json.Unmarshal(*v, &a.Key)
  1262. }
  1263. if v, ok := aggs["key_as_string"]; ok && v != nil {
  1264. json.Unmarshal(*v, &a.KeyAsString)
  1265. }
  1266. if v, ok := aggs["doc_count"]; ok && v != nil {
  1267. json.Unmarshal(*v, &a.DocCount)
  1268. }
  1269. a.Aggregations = aggs
  1270. return nil
  1271. }
  1272. // -- Pipeline simple value --
  1273. // AggregationPipelineSimpleValue is a simple value, returned e.g. by a
  1274. // MovAvg aggregation.
  1275. type AggregationPipelineSimpleValue struct {
  1276. Aggregations
  1277. Value *float64 // `json:"value"`
  1278. ValueAsString string // `json:"value_as_string"`
  1279. Meta map[string]interface{} // `json:"meta,omitempty"`
  1280. }
  1281. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineSimpleValue structure.
  1282. func (a *AggregationPipelineSimpleValue) UnmarshalJSON(data []byte) error {
  1283. var aggs map[string]*json.RawMessage
  1284. if err := json.Unmarshal(data, &aggs); err != nil {
  1285. return err
  1286. }
  1287. if v, ok := aggs["value"]; ok && v != nil {
  1288. json.Unmarshal(*v, &a.Value)
  1289. }
  1290. if v, ok := aggs["value_as_string"]; ok && v != nil {
  1291. json.Unmarshal(*v, &a.ValueAsString)
  1292. }
  1293. if v, ok := aggs["meta"]; ok && v != nil {
  1294. json.Unmarshal(*v, &a.Meta)
  1295. }
  1296. a.Aggregations = aggs
  1297. return nil
  1298. }
  1299. // -- Pipeline simple value --
  1300. // AggregationPipelineBucketMetricValue is a value returned e.g. by a
  1301. // MaxBucket aggregation.
  1302. type AggregationPipelineBucketMetricValue struct {
  1303. Aggregations
  1304. Keys []interface{} // `json:"keys"`
  1305. Value *float64 // `json:"value"`
  1306. ValueAsString string // `json:"value_as_string"`
  1307. Meta map[string]interface{} // `json:"meta,omitempty"`
  1308. }
  1309. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineBucketMetricValue structure.
  1310. func (a *AggregationPipelineBucketMetricValue) UnmarshalJSON(data []byte) error {
  1311. var aggs map[string]*json.RawMessage
  1312. if err := json.Unmarshal(data, &aggs); err != nil {
  1313. return err
  1314. }
  1315. if v, ok := aggs["keys"]; ok && v != nil {
  1316. json.Unmarshal(*v, &a.Keys)
  1317. }
  1318. if v, ok := aggs["value"]; ok && v != nil {
  1319. json.Unmarshal(*v, &a.Value)
  1320. }
  1321. if v, ok := aggs["value_as_string"]; ok && v != nil {
  1322. json.Unmarshal(*v, &a.ValueAsString)
  1323. }
  1324. if v, ok := aggs["meta"]; ok && v != nil {
  1325. json.Unmarshal(*v, &a.Meta)
  1326. }
  1327. a.Aggregations = aggs
  1328. return nil
  1329. }
  1330. // -- Pipeline derivative --
  1331. // AggregationPipelineDerivative is the value returned by a
  1332. // Derivative aggregation.
  1333. type AggregationPipelineDerivative struct {
  1334. Aggregations
  1335. Value *float64 // `json:"value"`
  1336. ValueAsString string // `json:"value_as_string"`
  1337. NormalizedValue *float64 // `json:"normalized_value"`
  1338. NormalizedValueAsString string // `json:"normalized_value_as_string"`
  1339. Meta map[string]interface{} // `json:"meta,omitempty"`
  1340. }
  1341. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineDerivative structure.
  1342. func (a *AggregationPipelineDerivative) UnmarshalJSON(data []byte) error {
  1343. var aggs map[string]*json.RawMessage
  1344. if err := json.Unmarshal(data, &aggs); err != nil {
  1345. return err
  1346. }
  1347. if v, ok := aggs["value"]; ok && v != nil {
  1348. json.Unmarshal(*v, &a.Value)
  1349. }
  1350. if v, ok := aggs["value_as_string"]; ok && v != nil {
  1351. json.Unmarshal(*v, &a.ValueAsString)
  1352. }
  1353. if v, ok := aggs["normalized_value"]; ok && v != nil {
  1354. json.Unmarshal(*v, &a.NormalizedValue)
  1355. }
  1356. if v, ok := aggs["normalized_value_as_string"]; ok && v != nil {
  1357. json.Unmarshal(*v, &a.NormalizedValueAsString)
  1358. }
  1359. if v, ok := aggs["meta"]; ok && v != nil {
  1360. json.Unmarshal(*v, &a.Meta)
  1361. }
  1362. a.Aggregations = aggs
  1363. return nil
  1364. }
  1365. // -- Pipeline stats metric --
  1366. // AggregationPipelineStatsMetric is a simple value, returned e.g. by a
  1367. // MovAvg aggregation.
  1368. type AggregationPipelineStatsMetric struct {
  1369. Aggregations
  1370. Count int64 // `json:"count"`
  1371. CountAsString string // `json:"count_as_string"`
  1372. Min *float64 // `json:"min"`
  1373. MinAsString string // `json:"min_as_string"`
  1374. Max *float64 // `json:"max"`
  1375. MaxAsString string // `json:"max_as_string"`
  1376. Avg *float64 // `json:"avg"`
  1377. AvgAsString string // `json:"avg_as_string"`
  1378. Sum *float64 // `json:"sum"`
  1379. SumAsString string // `json:"sum_as_string"`
  1380. Meta map[string]interface{} // `json:"meta,omitempty"`
  1381. }
  1382. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineStatsMetric structure.
  1383. func (a *AggregationPipelineStatsMetric) UnmarshalJSON(data []byte) error {
  1384. var aggs map[string]*json.RawMessage
  1385. if err := json.Unmarshal(data, &aggs); err != nil {
  1386. return err
  1387. }
  1388. if v, ok := aggs["count"]; ok && v != nil {
  1389. json.Unmarshal(*v, &a.Count)
  1390. }
  1391. if v, ok := aggs["count_as_string"]; ok && v != nil {
  1392. json.Unmarshal(*v, &a.CountAsString)
  1393. }
  1394. if v, ok := aggs["min"]; ok && v != nil {
  1395. json.Unmarshal(*v, &a.Min)
  1396. }
  1397. if v, ok := aggs["min_as_string"]; ok && v != nil {
  1398. json.Unmarshal(*v, &a.MinAsString)
  1399. }
  1400. if v, ok := aggs["max"]; ok && v != nil {
  1401. json.Unmarshal(*v, &a.Max)
  1402. }
  1403. if v, ok := aggs["max_as_string"]; ok && v != nil {
  1404. json.Unmarshal(*v, &a.MaxAsString)
  1405. }
  1406. if v, ok := aggs["avg"]; ok && v != nil {
  1407. json.Unmarshal(*v, &a.Avg)
  1408. }
  1409. if v, ok := aggs["avg_as_string"]; ok && v != nil {
  1410. json.Unmarshal(*v, &a.AvgAsString)
  1411. }
  1412. if v, ok := aggs["sum"]; ok && v != nil {
  1413. json.Unmarshal(*v, &a.Sum)
  1414. }
  1415. if v, ok := aggs["sum_as_string"]; ok && v != nil {
  1416. json.Unmarshal(*v, &a.SumAsString)
  1417. }
  1418. if v, ok := aggs["meta"]; ok && v != nil {
  1419. json.Unmarshal(*v, &a.Meta)
  1420. }
  1421. a.Aggregations = aggs
  1422. return nil
  1423. }
  1424. // -- Pipeline percentiles
  1425. // AggregationPipelinePercentilesMetric is the value returned by a pipeline
  1426. // percentiles Metric aggregation
  1427. type AggregationPipelinePercentilesMetric struct {
  1428. Aggregations
  1429. Values map[string]float64 // `json:"values"`
  1430. Meta map[string]interface{} // `json:"meta,omitempty"`
  1431. }
  1432. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelinePercentilesMetric structure.
  1433. func (a *AggregationPipelinePercentilesMetric) UnmarshalJSON(data []byte) error {
  1434. var aggs map[string]*json.RawMessage
  1435. if err := json.Unmarshal(data, &aggs); err != nil {
  1436. return err
  1437. }
  1438. if v, ok := aggs["values"]; ok && v != nil {
  1439. json.Unmarshal(*v, &a.Values)
  1440. }
  1441. if v, ok := aggs["meta"]; ok && v != nil {
  1442. json.Unmarshal(*v, &a.Meta)
  1443. }
  1444. a.Aggregations = aggs
  1445. return nil
  1446. }