search_aggs.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-datehistogram-aggregation.html
  429. func (a Aggregations) DateHistogram(name string) (*AggregationBucketHistogramItems, bool) {
  430. if raw, found := a[name]; found {
  431. agg := new(AggregationBucketHistogramItems)
  432. if raw == nil {
  433. return agg, true
  434. }
  435. if err := json.Unmarshal(*raw, agg); err == nil {
  436. return agg, true
  437. }
  438. }
  439. return nil, false
  440. }
  441. // GeoBounds returns geo-bounds aggregation results.
  442. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-geobounds-aggregation.html
  443. func (a Aggregations) GeoBounds(name string) (*AggregationGeoBoundsMetric, bool) {
  444. if raw, found := a[name]; found {
  445. agg := new(AggregationGeoBoundsMetric)
  446. if raw == nil {
  447. return agg, true
  448. }
  449. if err := json.Unmarshal(*raw, agg); err == nil {
  450. return agg, true
  451. }
  452. }
  453. return nil, false
  454. }
  455. // GeoHash returns geo-hash aggregation results.
  456. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-geohashgrid-aggregation.html
  457. func (a Aggregations) GeoHash(name string) (*AggregationBucketKeyItems, bool) {
  458. if raw, found := a[name]; found {
  459. agg := new(AggregationBucketKeyItems)
  460. if raw == nil {
  461. return agg, true
  462. }
  463. if err := json.Unmarshal(*raw, agg); err == nil {
  464. return agg, true
  465. }
  466. }
  467. return nil, false
  468. }
  469. // GeoCentroid returns geo-centroid aggregation results.
  470. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-metrics-geocentroid-aggregation.html
  471. func (a Aggregations) GeoCentroid(name string) (*AggregationGeoCentroidMetric, bool) {
  472. if raw, found := a[name]; found {
  473. agg := new(AggregationGeoCentroidMetric)
  474. if raw == nil {
  475. return agg, true
  476. }
  477. if err := json.Unmarshal(*raw, agg); err == nil {
  478. return agg, true
  479. }
  480. }
  481. return nil, false
  482. }
  483. // GeoDistance returns geo distance aggregation results.
  484. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-geodistance-aggregation.html
  485. func (a Aggregations) GeoDistance(name string) (*AggregationBucketRangeItems, bool) {
  486. if raw, found := a[name]; found {
  487. agg := new(AggregationBucketRangeItems)
  488. if raw == nil {
  489. return agg, true
  490. }
  491. if err := json.Unmarshal(*raw, agg); err == nil {
  492. return agg, true
  493. }
  494. }
  495. return nil, false
  496. }
  497. // AvgBucket returns average bucket pipeline aggregation results.
  498. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-avg-bucket-aggregation.html
  499. func (a Aggregations) AvgBucket(name string) (*AggregationPipelineSimpleValue, bool) {
  500. if raw, found := a[name]; found {
  501. agg := new(AggregationPipelineSimpleValue)
  502. if raw == nil {
  503. return agg, true
  504. }
  505. if err := json.Unmarshal(*raw, agg); err == nil {
  506. return agg, true
  507. }
  508. }
  509. return nil, false
  510. }
  511. // SumBucket returns sum bucket pipeline aggregation results.
  512. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-sum-bucket-aggregation.html
  513. func (a Aggregations) SumBucket(name string) (*AggregationPipelineSimpleValue, bool) {
  514. if raw, found := a[name]; found {
  515. agg := new(AggregationPipelineSimpleValue)
  516. if raw == nil {
  517. return agg, true
  518. }
  519. if err := json.Unmarshal(*raw, agg); err == nil {
  520. return agg, true
  521. }
  522. }
  523. return nil, false
  524. }
  525. // StatsBucket returns stats bucket pipeline aggregation results.
  526. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-stats-bucket-aggregation.html
  527. func (a Aggregations) StatsBucket(name string) (*AggregationPipelineStatsMetric, bool) {
  528. if raw, found := a[name]; found {
  529. agg := new(AggregationPipelineStatsMetric)
  530. if raw == nil {
  531. return agg, true
  532. }
  533. if err := json.Unmarshal(*raw, agg); err == nil {
  534. return agg, true
  535. }
  536. }
  537. return nil, false
  538. }
  539. // PercentilesBucket returns stats bucket pipeline aggregation results.
  540. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-percentiles-bucket-aggregation.html
  541. func (a Aggregations) PercentilesBucket(name string) (*AggregationPipelinePercentilesMetric, bool) {
  542. if raw, found := a[name]; found {
  543. agg := new(AggregationPipelinePercentilesMetric)
  544. if raw == nil {
  545. return agg, true
  546. }
  547. if err := json.Unmarshal(*raw, agg); err == nil {
  548. return agg, true
  549. }
  550. }
  551. return nil, false
  552. }
  553. // MaxBucket returns maximum bucket pipeline aggregation results.
  554. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-max-bucket-aggregation.html
  555. func (a Aggregations) MaxBucket(name string) (*AggregationPipelineBucketMetricValue, bool) {
  556. if raw, found := a[name]; found {
  557. agg := new(AggregationPipelineBucketMetricValue)
  558. if raw == nil {
  559. return agg, true
  560. }
  561. if err := json.Unmarshal(*raw, agg); err == nil {
  562. return agg, true
  563. }
  564. }
  565. return nil, false
  566. }
  567. // MinBucket returns minimum bucket pipeline aggregation results.
  568. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-min-bucket-aggregation.html
  569. func (a Aggregations) MinBucket(name string) (*AggregationPipelineBucketMetricValue, bool) {
  570. if raw, found := a[name]; found {
  571. agg := new(AggregationPipelineBucketMetricValue)
  572. if raw == nil {
  573. return agg, true
  574. }
  575. if err := json.Unmarshal(*raw, agg); err == nil {
  576. return agg, true
  577. }
  578. }
  579. return nil, false
  580. }
  581. // MovAvg returns moving average pipeline aggregation results.
  582. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html
  583. func (a Aggregations) MovAvg(name string) (*AggregationPipelineSimpleValue, bool) {
  584. if raw, found := a[name]; found {
  585. agg := new(AggregationPipelineSimpleValue)
  586. if raw == nil {
  587. return agg, true
  588. }
  589. if err := json.Unmarshal(*raw, agg); err == nil {
  590. return agg, true
  591. }
  592. }
  593. return nil, false
  594. }
  595. // Derivative returns derivative pipeline aggregation results.
  596. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-derivative-aggregation.html
  597. func (a Aggregations) Derivative(name string) (*AggregationPipelineDerivative, bool) {
  598. if raw, found := a[name]; found {
  599. agg := new(AggregationPipelineDerivative)
  600. if raw == nil {
  601. return agg, true
  602. }
  603. if err := json.Unmarshal(*raw, agg); err == nil {
  604. return agg, true
  605. }
  606. }
  607. return nil, false
  608. }
  609. // CumulativeSum returns a cumulative sum pipeline aggregation results.
  610. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-cumulative-sum-aggregation.html
  611. func (a Aggregations) CumulativeSum(name string) (*AggregationPipelineSimpleValue, bool) {
  612. if raw, found := a[name]; found {
  613. agg := new(AggregationPipelineSimpleValue)
  614. if raw == nil {
  615. return agg, true
  616. }
  617. if err := json.Unmarshal(*raw, agg); err == nil {
  618. return agg, true
  619. }
  620. }
  621. return nil, false
  622. }
  623. // BucketScript returns bucket script pipeline aggregation results.
  624. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-bucket-script-aggregation.html
  625. func (a Aggregations) BucketScript(name string) (*AggregationPipelineSimpleValue, bool) {
  626. if raw, found := a[name]; found {
  627. agg := new(AggregationPipelineSimpleValue)
  628. if raw == nil {
  629. return agg, true
  630. }
  631. if err := json.Unmarshal(*raw, agg); err == nil {
  632. return agg, true
  633. }
  634. }
  635. return nil, false
  636. }
  637. // SerialDiff returns serial differencing pipeline aggregation results.
  638. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-serialdiff-aggregation.html
  639. func (a Aggregations) SerialDiff(name string) (*AggregationPipelineSimpleValue, bool) {
  640. if raw, found := a[name]; found {
  641. agg := new(AggregationPipelineSimpleValue)
  642. if raw == nil {
  643. return agg, true
  644. }
  645. if err := json.Unmarshal(*raw, agg); err == nil {
  646. return agg, true
  647. }
  648. }
  649. return nil, false
  650. }
  651. // -- Single value metric --
  652. // AggregationValueMetric is a single-value metric, returned e.g. by a
  653. // Min or Max aggregation.
  654. type AggregationValueMetric struct {
  655. Aggregations
  656. Value *float64 //`json:"value"`
  657. Meta map[string]interface{} // `json:"meta,omitempty"`
  658. }
  659. // UnmarshalJSON decodes JSON data and initializes an AggregationValueMetric structure.
  660. func (a *AggregationValueMetric) UnmarshalJSON(data []byte) error {
  661. var aggs map[string]*json.RawMessage
  662. if err := json.Unmarshal(data, &aggs); err != nil {
  663. return err
  664. }
  665. if v, ok := aggs["value"]; ok && v != nil {
  666. json.Unmarshal(*v, &a.Value)
  667. }
  668. if v, ok := aggs["meta"]; ok && v != nil {
  669. json.Unmarshal(*v, &a.Meta)
  670. }
  671. a.Aggregations = aggs
  672. return nil
  673. }
  674. // -- Stats metric --
  675. // AggregationStatsMetric is a multi-value metric, returned by a Stats aggregation.
  676. type AggregationStatsMetric struct {
  677. Aggregations
  678. Count int64 // `json:"count"`
  679. Min *float64 //`json:"min,omitempty"`
  680. Max *float64 //`json:"max,omitempty"`
  681. Avg *float64 //`json:"avg,omitempty"`
  682. Sum *float64 //`json:"sum,omitempty"`
  683. Meta map[string]interface{} // `json:"meta,omitempty"`
  684. }
  685. // UnmarshalJSON decodes JSON data and initializes an AggregationStatsMetric structure.
  686. func (a *AggregationStatsMetric) UnmarshalJSON(data []byte) error {
  687. var aggs map[string]*json.RawMessage
  688. if err := json.Unmarshal(data, &aggs); err != nil {
  689. return err
  690. }
  691. if v, ok := aggs["count"]; ok && v != nil {
  692. json.Unmarshal(*v, &a.Count)
  693. }
  694. if v, ok := aggs["min"]; ok && v != nil {
  695. json.Unmarshal(*v, &a.Min)
  696. }
  697. if v, ok := aggs["max"]; ok && v != nil {
  698. json.Unmarshal(*v, &a.Max)
  699. }
  700. if v, ok := aggs["avg"]; ok && v != nil {
  701. json.Unmarshal(*v, &a.Avg)
  702. }
  703. if v, ok := aggs["sum"]; ok && v != nil {
  704. json.Unmarshal(*v, &a.Sum)
  705. }
  706. if v, ok := aggs["meta"]; ok && v != nil {
  707. json.Unmarshal(*v, &a.Meta)
  708. }
  709. a.Aggregations = aggs
  710. return nil
  711. }
  712. // -- Extended stats metric --
  713. // AggregationExtendedStatsMetric is a multi-value metric, returned by an ExtendedStats aggregation.
  714. type AggregationExtendedStatsMetric struct {
  715. Aggregations
  716. Count int64 // `json:"count"`
  717. Min *float64 //`json:"min,omitempty"`
  718. Max *float64 //`json:"max,omitempty"`
  719. Avg *float64 //`json:"avg,omitempty"`
  720. Sum *float64 //`json:"sum,omitempty"`
  721. SumOfSquares *float64 //`json:"sum_of_squares,omitempty"`
  722. Variance *float64 //`json:"variance,omitempty"`
  723. StdDeviation *float64 //`json:"std_deviation,omitempty"`
  724. Meta map[string]interface{} // `json:"meta,omitempty"`
  725. }
  726. // UnmarshalJSON decodes JSON data and initializes an AggregationExtendedStatsMetric structure.
  727. func (a *AggregationExtendedStatsMetric) UnmarshalJSON(data []byte) error {
  728. var aggs map[string]*json.RawMessage
  729. if err := json.Unmarshal(data, &aggs); err != nil {
  730. return err
  731. }
  732. if v, ok := aggs["count"]; ok && v != nil {
  733. json.Unmarshal(*v, &a.Count)
  734. }
  735. if v, ok := aggs["min"]; ok && v != nil {
  736. json.Unmarshal(*v, &a.Min)
  737. }
  738. if v, ok := aggs["max"]; ok && v != nil {
  739. json.Unmarshal(*v, &a.Max)
  740. }
  741. if v, ok := aggs["avg"]; ok && v != nil {
  742. json.Unmarshal(*v, &a.Avg)
  743. }
  744. if v, ok := aggs["sum"]; ok && v != nil {
  745. json.Unmarshal(*v, &a.Sum)
  746. }
  747. if v, ok := aggs["sum_of_squares"]; ok && v != nil {
  748. json.Unmarshal(*v, &a.SumOfSquares)
  749. }
  750. if v, ok := aggs["variance"]; ok && v != nil {
  751. json.Unmarshal(*v, &a.Variance)
  752. }
  753. if v, ok := aggs["std_deviation"]; ok && v != nil {
  754. json.Unmarshal(*v, &a.StdDeviation)
  755. }
  756. if v, ok := aggs["meta"]; ok && v != nil {
  757. json.Unmarshal(*v, &a.Meta)
  758. }
  759. a.Aggregations = aggs
  760. return nil
  761. }
  762. // -- Matrix Stats --
  763. // AggregationMatrixStats is returned by a MatrixStats aggregation.
  764. type AggregationMatrixStats struct {
  765. Aggregations
  766. Fields []*AggregationMatrixStatsField // `json:"field,omitempty"`
  767. Meta map[string]interface{} // `json:"meta,omitempty"`
  768. }
  769. // AggregationMatrixStatsField represents running stats of a single field
  770. // returned from MatrixStats aggregation.
  771. type AggregationMatrixStatsField struct {
  772. Name string `json:"name"`
  773. Count int64 `json:"count"`
  774. Mean float64 `json:"mean,omitempty"`
  775. Variance float64 `json:"variance,omitempty"`
  776. Skewness float64 `json:"skewness,omitempty"`
  777. Kurtosis float64 `json:"kurtosis,omitempty"`
  778. Covariance map[string]float64 `json:"covariance,omitempty"`
  779. Correlation map[string]float64 `json:"correlation,omitempty"`
  780. }
  781. // UnmarshalJSON decodes JSON data and initializes an AggregationMatrixStats structure.
  782. func (a *AggregationMatrixStats) UnmarshalJSON(data []byte) error {
  783. var aggs map[string]*json.RawMessage
  784. if err := json.Unmarshal(data, &aggs); err != nil {
  785. return err
  786. }
  787. if v, ok := aggs["fields"]; ok && v != nil {
  788. // RunningStats for every field
  789. json.Unmarshal(*v, &a.Fields)
  790. }
  791. if v, ok := aggs["meta"]; ok && v != nil {
  792. json.Unmarshal(*v, &a.Meta)
  793. }
  794. a.Aggregations = aggs
  795. return nil
  796. }
  797. // -- Percentiles metric --
  798. // AggregationPercentilesMetric is a multi-value metric, returned by a Percentiles aggregation.
  799. type AggregationPercentilesMetric struct {
  800. Aggregations
  801. Values map[string]float64 // `json:"values"`
  802. Meta map[string]interface{} // `json:"meta,omitempty"`
  803. }
  804. // UnmarshalJSON decodes JSON data and initializes an AggregationPercentilesMetric structure.
  805. func (a *AggregationPercentilesMetric) UnmarshalJSON(data []byte) error {
  806. var aggs map[string]*json.RawMessage
  807. if err := json.Unmarshal(data, &aggs); err != nil {
  808. return err
  809. }
  810. if v, ok := aggs["values"]; ok && v != nil {
  811. json.Unmarshal(*v, &a.Values)
  812. }
  813. if v, ok := aggs["meta"]; ok && v != nil {
  814. json.Unmarshal(*v, &a.Meta)
  815. }
  816. a.Aggregations = aggs
  817. return nil
  818. }
  819. // -- Top-hits metric --
  820. // AggregationTopHitsMetric is a metric returned by a TopHits aggregation.
  821. type AggregationTopHitsMetric struct {
  822. Aggregations
  823. Hits *SearchHits //`json:"hits"`
  824. Meta map[string]interface{} // `json:"meta,omitempty"`
  825. }
  826. // UnmarshalJSON decodes JSON data and initializes an AggregationTopHitsMetric structure.
  827. func (a *AggregationTopHitsMetric) UnmarshalJSON(data []byte) error {
  828. var aggs map[string]*json.RawMessage
  829. if err := json.Unmarshal(data, &aggs); err != nil {
  830. return err
  831. }
  832. a.Aggregations = aggs
  833. a.Hits = new(SearchHits)
  834. if v, ok := aggs["hits"]; ok && v != nil {
  835. json.Unmarshal(*v, &a.Hits)
  836. }
  837. if v, ok := aggs["meta"]; ok && v != nil {
  838. json.Unmarshal(*v, &a.Meta)
  839. }
  840. return nil
  841. }
  842. // -- Geo-bounds metric --
  843. // AggregationGeoBoundsMetric is a metric as returned by a GeoBounds aggregation.
  844. type AggregationGeoBoundsMetric struct {
  845. Aggregations
  846. Bounds struct {
  847. TopLeft struct {
  848. Latitude float64 `json:"lat"`
  849. Longitude float64 `json:"lon"`
  850. } `json:"top_left"`
  851. BottomRight struct {
  852. Latitude float64 `json:"lat"`
  853. Longitude float64 `json:"lon"`
  854. } `json:"bottom_right"`
  855. } `json:"bounds"`
  856. Meta map[string]interface{} // `json:"meta,omitempty"`
  857. }
  858. // UnmarshalJSON decodes JSON data and initializes an AggregationGeoBoundsMetric structure.
  859. func (a *AggregationGeoBoundsMetric) UnmarshalJSON(data []byte) error {
  860. var aggs map[string]*json.RawMessage
  861. if err := json.Unmarshal(data, &aggs); err != nil {
  862. return err
  863. }
  864. if v, ok := aggs["bounds"]; ok && v != nil {
  865. json.Unmarshal(*v, &a.Bounds)
  866. }
  867. if v, ok := aggs["meta"]; ok && v != nil {
  868. json.Unmarshal(*v, &a.Meta)
  869. }
  870. a.Aggregations = aggs
  871. return nil
  872. }
  873. // -- Geo Centroid --
  874. // AggregationGeocentroidMetric is a metric as returned by a GeoCentroid aggregation.
  875. type AggregationGeoCentroidMetric struct {
  876. Aggregations
  877. Location struct {
  878. Latitude float64 `json:"lat"`
  879. Longitude float64 `json:"lon"`
  880. } `json:"location"`
  881. Count int // `json:"count,omitempty"`
  882. Meta map[string]interface{} // `json:"meta,omitempty"`
  883. }
  884. // UnmarshalJSON decodes JSON data and initializes an AggregationGeoCentroidMetric structure.
  885. func (a *AggregationGeoCentroidMetric) UnmarshalJSON(data []byte) error {
  886. var aggs map[string]*json.RawMessage
  887. if err := json.Unmarshal(data, &aggs); err != nil {
  888. return err
  889. }
  890. if v, ok := aggs["location"]; ok && v != nil {
  891. json.Unmarshal(*v, &a.Location)
  892. }
  893. if v, ok := aggs["meta"]; ok && v != nil {
  894. json.Unmarshal(*v, &a.Meta)
  895. }
  896. if v, ok := aggs["count"]; ok && v != nil {
  897. json.Unmarshal(*v, &a.Count)
  898. }
  899. a.Aggregations = aggs
  900. return nil
  901. }
  902. // -- Single bucket --
  903. // AggregationSingleBucket is a single bucket, returned e.g. via an aggregation of type Global.
  904. type AggregationSingleBucket struct {
  905. Aggregations
  906. DocCount int64 // `json:"doc_count"`
  907. Meta map[string]interface{} // `json:"meta,omitempty"`
  908. }
  909. // UnmarshalJSON decodes JSON data and initializes an AggregationSingleBucket structure.
  910. func (a *AggregationSingleBucket) UnmarshalJSON(data []byte) error {
  911. var aggs map[string]*json.RawMessage
  912. if err := json.Unmarshal(data, &aggs); err != nil {
  913. return err
  914. }
  915. if v, ok := aggs["doc_count"]; ok && v != nil {
  916. json.Unmarshal(*v, &a.DocCount)
  917. }
  918. if v, ok := aggs["meta"]; ok && v != nil {
  919. json.Unmarshal(*v, &a.Meta)
  920. }
  921. a.Aggregations = aggs
  922. return nil
  923. }
  924. // -- Bucket range items --
  925. // AggregationBucketRangeItems is a bucket aggregation that is e.g. returned
  926. // with a range aggregation.
  927. type AggregationBucketRangeItems struct {
  928. Aggregations
  929. DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"`
  930. SumOfOtherDocCount int64 //`json:"sum_other_doc_count"`
  931. Buckets []*AggregationBucketRangeItem //`json:"buckets"`
  932. Meta map[string]interface{} // `json:"meta,omitempty"`
  933. }
  934. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItems structure.
  935. func (a *AggregationBucketRangeItems) UnmarshalJSON(data []byte) error {
  936. var aggs map[string]*json.RawMessage
  937. if err := json.Unmarshal(data, &aggs); err != nil {
  938. return err
  939. }
  940. if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil {
  941. json.Unmarshal(*v, &a.DocCountErrorUpperBound)
  942. }
  943. if v, ok := aggs["sum_other_doc_count"]; ok && v != nil {
  944. json.Unmarshal(*v, &a.SumOfOtherDocCount)
  945. }
  946. if v, ok := aggs["buckets"]; ok && v != nil {
  947. json.Unmarshal(*v, &a.Buckets)
  948. }
  949. if v, ok := aggs["meta"]; ok && v != nil {
  950. json.Unmarshal(*v, &a.Meta)
  951. }
  952. a.Aggregations = aggs
  953. return nil
  954. }
  955. // AggregationBucketKeyedRangeItems is a bucket aggregation that is e.g. returned
  956. // with a keyed range aggregation.
  957. type AggregationBucketKeyedRangeItems struct {
  958. Aggregations
  959. DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"`
  960. SumOfOtherDocCount int64 //`json:"sum_other_doc_count"`
  961. Buckets map[string]*AggregationBucketRangeItem //`json:"buckets"`
  962. Meta map[string]interface{} // `json:"meta,omitempty"`
  963. }
  964. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItems structure.
  965. func (a *AggregationBucketKeyedRangeItems) UnmarshalJSON(data []byte) error {
  966. var aggs map[string]*json.RawMessage
  967. if err := json.Unmarshal(data, &aggs); err != nil {
  968. return err
  969. }
  970. if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil {
  971. json.Unmarshal(*v, &a.DocCountErrorUpperBound)
  972. }
  973. if v, ok := aggs["sum_other_doc_count"]; ok && v != nil {
  974. json.Unmarshal(*v, &a.SumOfOtherDocCount)
  975. }
  976. if v, ok := aggs["buckets"]; ok && v != nil {
  977. json.Unmarshal(*v, &a.Buckets)
  978. }
  979. if v, ok := aggs["meta"]; ok && v != nil {
  980. json.Unmarshal(*v, &a.Meta)
  981. }
  982. a.Aggregations = aggs
  983. return nil
  984. }
  985. // AggregationBucketRangeItem is a single bucket of an AggregationBucketRangeItems structure.
  986. type AggregationBucketRangeItem struct {
  987. Aggregations
  988. Key string //`json:"key"`
  989. DocCount int64 //`json:"doc_count"`
  990. From *float64 //`json:"from"`
  991. FromAsString string //`json:"from_as_string"`
  992. To *float64 //`json:"to"`
  993. ToAsString string //`json:"to_as_string"`
  994. }
  995. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItem structure.
  996. func (a *AggregationBucketRangeItem) UnmarshalJSON(data []byte) error {
  997. var aggs map[string]*json.RawMessage
  998. if err := json.Unmarshal(data, &aggs); err != nil {
  999. return err
  1000. }
  1001. if v, ok := aggs["key"]; ok && v != nil {
  1002. json.Unmarshal(*v, &a.Key)
  1003. }
  1004. if v, ok := aggs["doc_count"]; ok && v != nil {
  1005. json.Unmarshal(*v, &a.DocCount)
  1006. }
  1007. if v, ok := aggs["from"]; ok && v != nil {
  1008. json.Unmarshal(*v, &a.From)
  1009. }
  1010. if v, ok := aggs["from_as_string"]; ok && v != nil {
  1011. json.Unmarshal(*v, &a.FromAsString)
  1012. }
  1013. if v, ok := aggs["to"]; ok && v != nil {
  1014. json.Unmarshal(*v, &a.To)
  1015. }
  1016. if v, ok := aggs["to_as_string"]; ok && v != nil {
  1017. json.Unmarshal(*v, &a.ToAsString)
  1018. }
  1019. a.Aggregations = aggs
  1020. return nil
  1021. }
  1022. // -- Bucket key items --
  1023. // AggregationBucketKeyItems is a bucket aggregation that is e.g. returned
  1024. // with a terms aggregation.
  1025. type AggregationBucketKeyItems struct {
  1026. Aggregations
  1027. DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"`
  1028. SumOfOtherDocCount int64 //`json:"sum_other_doc_count"`
  1029. Buckets []*AggregationBucketKeyItem //`json:"buckets"`
  1030. Meta map[string]interface{} // `json:"meta,omitempty"`
  1031. }
  1032. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyItems structure.
  1033. func (a *AggregationBucketKeyItems) UnmarshalJSON(data []byte) error {
  1034. var aggs map[string]*json.RawMessage
  1035. if err := json.Unmarshal(data, &aggs); err != nil {
  1036. return err
  1037. }
  1038. if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil {
  1039. json.Unmarshal(*v, &a.DocCountErrorUpperBound)
  1040. }
  1041. if v, ok := aggs["sum_other_doc_count"]; ok && v != nil {
  1042. json.Unmarshal(*v, &a.SumOfOtherDocCount)
  1043. }
  1044. if v, ok := aggs["buckets"]; ok && v != nil {
  1045. json.Unmarshal(*v, &a.Buckets)
  1046. }
  1047. if v, ok := aggs["meta"]; ok && v != nil {
  1048. json.Unmarshal(*v, &a.Meta)
  1049. }
  1050. a.Aggregations = aggs
  1051. return nil
  1052. }
  1053. // AggregationBucketKeyItem is a single bucket of an AggregationBucketKeyItems structure.
  1054. type AggregationBucketKeyItem struct {
  1055. Aggregations
  1056. Key interface{} //`json:"key"`
  1057. KeyAsString *string //`json:"key_as_string"`
  1058. KeyNumber json.Number
  1059. DocCount int64 //`json:"doc_count"`
  1060. }
  1061. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyItem structure.
  1062. func (a *AggregationBucketKeyItem) UnmarshalJSON(data []byte) error {
  1063. var aggs map[string]*json.RawMessage
  1064. dec := json.NewDecoder(bytes.NewReader(data))
  1065. dec.UseNumber()
  1066. if err := dec.Decode(&aggs); err != nil {
  1067. return err
  1068. }
  1069. if v, ok := aggs["key"]; ok && v != nil {
  1070. json.Unmarshal(*v, &a.Key)
  1071. json.Unmarshal(*v, &a.KeyNumber)
  1072. }
  1073. if v, ok := aggs["key_as_string"]; ok && v != nil {
  1074. json.Unmarshal(*v, &a.KeyAsString)
  1075. }
  1076. if v, ok := aggs["doc_count"]; ok && v != nil {
  1077. json.Unmarshal(*v, &a.DocCount)
  1078. }
  1079. a.Aggregations = aggs
  1080. return nil
  1081. }
  1082. // -- Bucket types for significant terms --
  1083. // AggregationBucketSignificantTerms is a bucket aggregation returned
  1084. // with a significant terms aggregation.
  1085. type AggregationBucketSignificantTerms struct {
  1086. Aggregations
  1087. DocCount int64 //`json:"doc_count"`
  1088. Buckets []*AggregationBucketSignificantTerm //`json:"buckets"`
  1089. Meta map[string]interface{} // `json:"meta,omitempty"`
  1090. }
  1091. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketSignificantTerms structure.
  1092. func (a *AggregationBucketSignificantTerms) UnmarshalJSON(data []byte) error {
  1093. var aggs map[string]*json.RawMessage
  1094. if err := json.Unmarshal(data, &aggs); err != nil {
  1095. return err
  1096. }
  1097. if v, ok := aggs["doc_count"]; ok && v != nil {
  1098. json.Unmarshal(*v, &a.DocCount)
  1099. }
  1100. if v, ok := aggs["buckets"]; ok && v != nil {
  1101. json.Unmarshal(*v, &a.Buckets)
  1102. }
  1103. if v, ok := aggs["meta"]; ok && v != nil {
  1104. json.Unmarshal(*v, &a.Meta)
  1105. }
  1106. a.Aggregations = aggs
  1107. return nil
  1108. }
  1109. // AggregationBucketSignificantTerm is a single bucket of an AggregationBucketSignificantTerms structure.
  1110. type AggregationBucketSignificantTerm struct {
  1111. Aggregations
  1112. Key string //`json:"key"`
  1113. DocCount int64 //`json:"doc_count"`
  1114. BgCount int64 //`json:"bg_count"`
  1115. Score float64 //`json:"score"`
  1116. }
  1117. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketSignificantTerm structure.
  1118. func (a *AggregationBucketSignificantTerm) UnmarshalJSON(data []byte) error {
  1119. var aggs map[string]*json.RawMessage
  1120. if err := json.Unmarshal(data, &aggs); err != nil {
  1121. return err
  1122. }
  1123. if v, ok := aggs["key"]; ok && v != nil {
  1124. json.Unmarshal(*v, &a.Key)
  1125. }
  1126. if v, ok := aggs["doc_count"]; ok && v != nil {
  1127. json.Unmarshal(*v, &a.DocCount)
  1128. }
  1129. if v, ok := aggs["bg_count"]; ok && v != nil {
  1130. json.Unmarshal(*v, &a.BgCount)
  1131. }
  1132. if v, ok := aggs["score"]; ok && v != nil {
  1133. json.Unmarshal(*v, &a.Score)
  1134. }
  1135. a.Aggregations = aggs
  1136. return nil
  1137. }
  1138. // -- Bucket filters --
  1139. // AggregationBucketFilters is a multi-bucket aggregation that is returned
  1140. // with a filters aggregation.
  1141. type AggregationBucketFilters struct {
  1142. Aggregations
  1143. Buckets []*AggregationBucketKeyItem //`json:"buckets"`
  1144. NamedBuckets map[string]*AggregationBucketKeyItem //`json:"buckets"`
  1145. Meta map[string]interface{} // `json:"meta,omitempty"`
  1146. }
  1147. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketFilters structure.
  1148. func (a *AggregationBucketFilters) UnmarshalJSON(data []byte) error {
  1149. var aggs map[string]*json.RawMessage
  1150. if err := json.Unmarshal(data, &aggs); err != nil {
  1151. return err
  1152. }
  1153. if v, ok := aggs["buckets"]; ok && v != nil {
  1154. json.Unmarshal(*v, &a.Buckets)
  1155. json.Unmarshal(*v, &a.NamedBuckets)
  1156. }
  1157. if v, ok := aggs["meta"]; ok && v != nil {
  1158. json.Unmarshal(*v, &a.Meta)
  1159. }
  1160. a.Aggregations = aggs
  1161. return nil
  1162. }
  1163. // -- Bucket AdjacencyMatrix --
  1164. // AggregationBucketAdjacencyMatrix is a multi-bucket aggregation that is returned
  1165. // with a AdjacencyMatrix aggregation.
  1166. type AggregationBucketAdjacencyMatrix struct {
  1167. Aggregations
  1168. Buckets []*AggregationBucketKeyItem //`json:"buckets"`
  1169. Meta map[string]interface{} // `json:"meta,omitempty"`
  1170. }
  1171. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketAdjacencyMatrix structure.
  1172. func (a *AggregationBucketAdjacencyMatrix) UnmarshalJSON(data []byte) error {
  1173. var aggs map[string]*json.RawMessage
  1174. if err := json.Unmarshal(data, &aggs); err != nil {
  1175. return err
  1176. }
  1177. if v, ok := aggs["buckets"]; ok && v != nil {
  1178. json.Unmarshal(*v, &a.Buckets)
  1179. }
  1180. if v, ok := aggs["meta"]; ok && v != nil {
  1181. json.Unmarshal(*v, &a.Meta)
  1182. }
  1183. a.Aggregations = aggs
  1184. return nil
  1185. }
  1186. // -- Bucket histogram items --
  1187. // AggregationBucketHistogramItems is a bucket aggregation that is returned
  1188. // with a date histogram aggregation.
  1189. type AggregationBucketHistogramItems struct {
  1190. Aggregations
  1191. Buckets []*AggregationBucketHistogramItem //`json:"buckets"`
  1192. Meta map[string]interface{} // `json:"meta,omitempty"`
  1193. }
  1194. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketHistogramItems structure.
  1195. func (a *AggregationBucketHistogramItems) UnmarshalJSON(data []byte) error {
  1196. var aggs map[string]*json.RawMessage
  1197. if err := json.Unmarshal(data, &aggs); err != nil {
  1198. return err
  1199. }
  1200. if v, ok := aggs["buckets"]; ok && v != nil {
  1201. json.Unmarshal(*v, &a.Buckets)
  1202. }
  1203. if v, ok := aggs["meta"]; ok && v != nil {
  1204. json.Unmarshal(*v, &a.Meta)
  1205. }
  1206. a.Aggregations = aggs
  1207. return nil
  1208. }
  1209. // AggregationBucketHistogramItem is a single bucket of an AggregationBucketHistogramItems structure.
  1210. type AggregationBucketHistogramItem struct {
  1211. Aggregations
  1212. Key float64 //`json:"key"`
  1213. KeyAsString *string //`json:"key_as_string"`
  1214. DocCount int64 //`json:"doc_count"`
  1215. }
  1216. // UnmarshalJSON decodes JSON data and initializes an AggregationBucketHistogramItem structure.
  1217. func (a *AggregationBucketHistogramItem) UnmarshalJSON(data []byte) error {
  1218. var aggs map[string]*json.RawMessage
  1219. if err := json.Unmarshal(data, &aggs); err != nil {
  1220. return err
  1221. }
  1222. if v, ok := aggs["key"]; ok && v != nil {
  1223. json.Unmarshal(*v, &a.Key)
  1224. }
  1225. if v, ok := aggs["key_as_string"]; ok && v != nil {
  1226. json.Unmarshal(*v, &a.KeyAsString)
  1227. }
  1228. if v, ok := aggs["doc_count"]; ok && v != nil {
  1229. json.Unmarshal(*v, &a.DocCount)
  1230. }
  1231. a.Aggregations = aggs
  1232. return nil
  1233. }
  1234. // -- Pipeline simple value --
  1235. // AggregationPipelineSimpleValue is a simple value, returned e.g. by a
  1236. // MovAvg aggregation.
  1237. type AggregationPipelineSimpleValue struct {
  1238. Aggregations
  1239. Value *float64 // `json:"value"`
  1240. ValueAsString string // `json:"value_as_string"`
  1241. Meta map[string]interface{} // `json:"meta,omitempty"`
  1242. }
  1243. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineSimpleValue structure.
  1244. func (a *AggregationPipelineSimpleValue) UnmarshalJSON(data []byte) error {
  1245. var aggs map[string]*json.RawMessage
  1246. if err := json.Unmarshal(data, &aggs); err != nil {
  1247. return err
  1248. }
  1249. if v, ok := aggs["value"]; ok && v != nil {
  1250. json.Unmarshal(*v, &a.Value)
  1251. }
  1252. if v, ok := aggs["value_as_string"]; ok && v != nil {
  1253. json.Unmarshal(*v, &a.ValueAsString)
  1254. }
  1255. if v, ok := aggs["meta"]; ok && v != nil {
  1256. json.Unmarshal(*v, &a.Meta)
  1257. }
  1258. a.Aggregations = aggs
  1259. return nil
  1260. }
  1261. // -- Pipeline simple value --
  1262. // AggregationPipelineBucketMetricValue is a value returned e.g. by a
  1263. // MaxBucket aggregation.
  1264. type AggregationPipelineBucketMetricValue struct {
  1265. Aggregations
  1266. Keys []interface{} // `json:"keys"`
  1267. Value *float64 // `json:"value"`
  1268. ValueAsString string // `json:"value_as_string"`
  1269. Meta map[string]interface{} // `json:"meta,omitempty"`
  1270. }
  1271. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineBucketMetricValue structure.
  1272. func (a *AggregationPipelineBucketMetricValue) UnmarshalJSON(data []byte) error {
  1273. var aggs map[string]*json.RawMessage
  1274. if err := json.Unmarshal(data, &aggs); err != nil {
  1275. return err
  1276. }
  1277. if v, ok := aggs["keys"]; ok && v != nil {
  1278. json.Unmarshal(*v, &a.Keys)
  1279. }
  1280. if v, ok := aggs["value"]; ok && v != nil {
  1281. json.Unmarshal(*v, &a.Value)
  1282. }
  1283. if v, ok := aggs["value_as_string"]; ok && v != nil {
  1284. json.Unmarshal(*v, &a.ValueAsString)
  1285. }
  1286. if v, ok := aggs["meta"]; ok && v != nil {
  1287. json.Unmarshal(*v, &a.Meta)
  1288. }
  1289. a.Aggregations = aggs
  1290. return nil
  1291. }
  1292. // -- Pipeline derivative --
  1293. // AggregationPipelineDerivative is the value returned by a
  1294. // Derivative aggregation.
  1295. type AggregationPipelineDerivative struct {
  1296. Aggregations
  1297. Value *float64 // `json:"value"`
  1298. ValueAsString string // `json:"value_as_string"`
  1299. NormalizedValue *float64 // `json:"normalized_value"`
  1300. NormalizedValueAsString string // `json:"normalized_value_as_string"`
  1301. Meta map[string]interface{} // `json:"meta,omitempty"`
  1302. }
  1303. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineDerivative structure.
  1304. func (a *AggregationPipelineDerivative) UnmarshalJSON(data []byte) error {
  1305. var aggs map[string]*json.RawMessage
  1306. if err := json.Unmarshal(data, &aggs); err != nil {
  1307. return err
  1308. }
  1309. if v, ok := aggs["value"]; ok && v != nil {
  1310. json.Unmarshal(*v, &a.Value)
  1311. }
  1312. if v, ok := aggs["value_as_string"]; ok && v != nil {
  1313. json.Unmarshal(*v, &a.ValueAsString)
  1314. }
  1315. if v, ok := aggs["normalized_value"]; ok && v != nil {
  1316. json.Unmarshal(*v, &a.NormalizedValue)
  1317. }
  1318. if v, ok := aggs["normalized_value_as_string"]; ok && v != nil {
  1319. json.Unmarshal(*v, &a.NormalizedValueAsString)
  1320. }
  1321. if v, ok := aggs["meta"]; ok && v != nil {
  1322. json.Unmarshal(*v, &a.Meta)
  1323. }
  1324. a.Aggregations = aggs
  1325. return nil
  1326. }
  1327. // -- Pipeline stats metric --
  1328. // AggregationPipelineStatsMetric is a simple value, returned e.g. by a
  1329. // MovAvg aggregation.
  1330. type AggregationPipelineStatsMetric struct {
  1331. Aggregations
  1332. Count int64 // `json:"count"`
  1333. CountAsString string // `json:"count_as_string"`
  1334. Min *float64 // `json:"min"`
  1335. MinAsString string // `json:"min_as_string"`
  1336. Max *float64 // `json:"max"`
  1337. MaxAsString string // `json:"max_as_string"`
  1338. Avg *float64 // `json:"avg"`
  1339. AvgAsString string // `json:"avg_as_string"`
  1340. Sum *float64 // `json:"sum"`
  1341. SumAsString string // `json:"sum_as_string"`
  1342. Meta map[string]interface{} // `json:"meta,omitempty"`
  1343. }
  1344. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelineStatsMetric structure.
  1345. func (a *AggregationPipelineStatsMetric) UnmarshalJSON(data []byte) error {
  1346. var aggs map[string]*json.RawMessage
  1347. if err := json.Unmarshal(data, &aggs); err != nil {
  1348. return err
  1349. }
  1350. if v, ok := aggs["count"]; ok && v != nil {
  1351. json.Unmarshal(*v, &a.Count)
  1352. }
  1353. if v, ok := aggs["count_as_string"]; ok && v != nil {
  1354. json.Unmarshal(*v, &a.CountAsString)
  1355. }
  1356. if v, ok := aggs["min"]; ok && v != nil {
  1357. json.Unmarshal(*v, &a.Min)
  1358. }
  1359. if v, ok := aggs["min_as_string"]; ok && v != nil {
  1360. json.Unmarshal(*v, &a.MinAsString)
  1361. }
  1362. if v, ok := aggs["max"]; ok && v != nil {
  1363. json.Unmarshal(*v, &a.Max)
  1364. }
  1365. if v, ok := aggs["max_as_string"]; ok && v != nil {
  1366. json.Unmarshal(*v, &a.MaxAsString)
  1367. }
  1368. if v, ok := aggs["avg"]; ok && v != nil {
  1369. json.Unmarshal(*v, &a.Avg)
  1370. }
  1371. if v, ok := aggs["avg_as_string"]; ok && v != nil {
  1372. json.Unmarshal(*v, &a.AvgAsString)
  1373. }
  1374. if v, ok := aggs["sum"]; ok && v != nil {
  1375. json.Unmarshal(*v, &a.Sum)
  1376. }
  1377. if v, ok := aggs["sum_as_string"]; ok && v != nil {
  1378. json.Unmarshal(*v, &a.SumAsString)
  1379. }
  1380. if v, ok := aggs["meta"]; ok && v != nil {
  1381. json.Unmarshal(*v, &a.Meta)
  1382. }
  1383. a.Aggregations = aggs
  1384. return nil
  1385. }
  1386. // -- Pipeline percentiles
  1387. // AggregationPipelinePercentilesMetric is the value returned by a pipeline
  1388. // percentiles Metric aggregation
  1389. type AggregationPipelinePercentilesMetric struct {
  1390. Aggregations
  1391. Values map[string]float64 // `json:"values"`
  1392. Meta map[string]interface{} // `json:"meta,omitempty"`
  1393. }
  1394. // UnmarshalJSON decodes JSON data and initializes an AggregationPipelinePercentilesMetric structure.
  1395. func (a *AggregationPipelinePercentilesMetric) UnmarshalJSON(data []byte) error {
  1396. var aggs map[string]*json.RawMessage
  1397. if err := json.Unmarshal(data, &aggs); err != nil {
  1398. return err
  1399. }
  1400. if v, ok := aggs["values"]; ok && v != nil {
  1401. json.Unmarshal(*v, &a.Values)
  1402. }
  1403. if v, ok := aggs["meta"]; ok && v != nil {
  1404. json.Unmarshal(*v, &a.Meta)
  1405. }
  1406. a.Aggregations = aggs
  1407. return nil
  1408. }