search_aggs_pipeline_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. "context"
  7. "testing"
  8. )
  9. func TestAggsIntegrationAvgBucket(t *testing.T) {
  10. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  11. client := setupTestClientAndCreateIndexAndAddDocs(t)
  12. esversion, err := client.ElasticsearchVersion(DefaultURL)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if esversion < "2.0" {
  17. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  18. return
  19. }
  20. // Match all should return all documents
  21. builder := client.Search().
  22. Index(testIndexName).
  23. Type("order").
  24. Query(NewMatchAllQuery()).
  25. Pretty(true)
  26. h := NewDateHistogramAggregation().Field("time").Interval("month")
  27. h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
  28. builder = builder.Aggregation("sales_per_month", h)
  29. builder = builder.Aggregation("avg_monthly_sales", NewAvgBucketAggregation().BucketsPath("sales_per_month>sales"))
  30. res, err := builder.Do(context.TODO())
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. if res.Hits == nil {
  35. t.Errorf("expected Hits != nil; got: nil")
  36. }
  37. aggs := res.Aggregations
  38. if aggs == nil {
  39. t.Fatal("expected aggregations != nil; got: nil")
  40. }
  41. agg, found := aggs.AvgBucket("avg_monthly_sales")
  42. if !found {
  43. t.Fatal("expected avg_monthly_sales aggregation")
  44. }
  45. if agg == nil {
  46. t.Fatal("expected avg_monthly_sales aggregation")
  47. }
  48. if agg.Value == nil {
  49. t.Fatal("expected avg_monthly_sales.value != nil")
  50. }
  51. if got, want := *agg.Value, float64(939.2); got != want {
  52. t.Fatalf("expected avg_monthly_sales.value=%v; got: %v", want, got)
  53. }
  54. }
  55. func TestAggsIntegrationDerivative(t *testing.T) {
  56. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  57. client := setupTestClientAndCreateIndexAndAddDocs(t)
  58. esversion, err := client.ElasticsearchVersion(DefaultURL)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if esversion < "2.0" {
  63. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  64. return
  65. }
  66. // Match all should return all documents
  67. builder := client.Search().
  68. Index(testIndexName).
  69. Type("order").
  70. Query(NewMatchAllQuery()).
  71. Pretty(true)
  72. h := NewDateHistogramAggregation().Field("time").Interval("month")
  73. h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
  74. h = h.SubAggregation("sales_deriv", NewDerivativeAggregation().BucketsPath("sales"))
  75. builder = builder.Aggregation("sales_per_month", h)
  76. res, err := builder.Do(context.TODO())
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if res.Hits == nil {
  81. t.Errorf("expected Hits != nil; got: nil")
  82. }
  83. aggs := res.Aggregations
  84. if aggs == nil {
  85. t.Fatal("expected aggregations != nil; got: nil")
  86. }
  87. agg, found := aggs.DateHistogram("sales_per_month")
  88. if !found {
  89. t.Fatal("expected sales_per_month aggregation")
  90. }
  91. if agg == nil {
  92. t.Fatal("expected sales_per_month aggregation")
  93. }
  94. if got, want := len(agg.Buckets), 6; got != want {
  95. t.Fatalf("expected %d buckets; got: %d", want, got)
  96. }
  97. if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
  98. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  99. }
  100. if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
  101. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  102. }
  103. if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
  104. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  105. }
  106. if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
  107. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  108. }
  109. if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
  110. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  111. }
  112. if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
  113. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  114. }
  115. d, found := agg.Buckets[0].Derivative("sales_deriv")
  116. if found {
  117. t.Fatal("expected no sales_deriv aggregation")
  118. }
  119. if d != nil {
  120. t.Fatal("expected no sales_deriv aggregation")
  121. }
  122. d, found = agg.Buckets[1].Derivative("sales_deriv")
  123. if !found {
  124. t.Fatal("expected sales_deriv aggregation")
  125. }
  126. if d == nil {
  127. t.Fatal("expected sales_deriv aggregation")
  128. }
  129. if d.Value != nil {
  130. t.Fatal("expected sales_deriv value == nil")
  131. }
  132. d, found = agg.Buckets[2].Derivative("sales_deriv")
  133. if !found {
  134. t.Fatal("expected sales_deriv aggregation")
  135. }
  136. if d == nil {
  137. t.Fatal("expected sales_deriv aggregation")
  138. }
  139. if d.Value != nil {
  140. t.Fatal("expected sales_deriv value == nil")
  141. }
  142. d, found = agg.Buckets[3].Derivative("sales_deriv")
  143. if !found {
  144. t.Fatal("expected sales_deriv aggregation")
  145. }
  146. if d == nil {
  147. t.Fatal("expected sales_deriv aggregation")
  148. }
  149. if d.Value == nil {
  150. t.Fatal("expected sales_deriv value != nil")
  151. }
  152. if got, want := *d.Value, float64(2348.0); got != want {
  153. t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)
  154. }
  155. d, found = agg.Buckets[4].Derivative("sales_deriv")
  156. if !found {
  157. t.Fatal("expected sales_deriv aggregation")
  158. }
  159. if d == nil {
  160. t.Fatal("expected sales_deriv aggregation")
  161. }
  162. if d.Value == nil {
  163. t.Fatal("expected sales_deriv value != nil")
  164. }
  165. if got, want := *d.Value, float64(-1658.0); got != want {
  166. t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)
  167. }
  168. d, found = agg.Buckets[5].Derivative("sales_deriv")
  169. if !found {
  170. t.Fatal("expected sales_deriv aggregation")
  171. }
  172. if d == nil {
  173. t.Fatal("expected sales_deriv aggregation")
  174. }
  175. if d.Value == nil {
  176. t.Fatal("expected sales_deriv value != nil")
  177. }
  178. if got, want := *d.Value, float64(-722.0); got != want {
  179. t.Fatalf("expected sales_deriv.value=%v; got: %v", want, got)
  180. }
  181. }
  182. func TestAggsIntegrationMaxBucket(t *testing.T) {
  183. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  184. client := setupTestClientAndCreateIndexAndAddDocs(t)
  185. esversion, err := client.ElasticsearchVersion(DefaultURL)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. if esversion < "2.0" {
  190. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  191. return
  192. }
  193. // Match all should return all documents
  194. builder := client.Search().
  195. Index(testIndexName).
  196. Type("order").
  197. Query(NewMatchAllQuery()).
  198. Pretty(true)
  199. h := NewDateHistogramAggregation().Field("time").Interval("month")
  200. h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
  201. builder = builder.Aggregation("sales_per_month", h)
  202. builder = builder.Aggregation("max_monthly_sales", NewMaxBucketAggregation().BucketsPath("sales_per_month>sales"))
  203. res, err := builder.Do(context.TODO())
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. if res.Hits == nil {
  208. t.Errorf("expected Hits != nil; got: nil")
  209. }
  210. aggs := res.Aggregations
  211. if aggs == nil {
  212. t.Fatal("expected aggregations != nil; got: nil")
  213. }
  214. agg, found := aggs.MaxBucket("max_monthly_sales")
  215. if !found {
  216. t.Fatal("expected max_monthly_sales aggregation")
  217. }
  218. if agg == nil {
  219. t.Fatal("expected max_monthly_sales aggregation")
  220. }
  221. if got, want := len(agg.Keys), 1; got != want {
  222. t.Fatalf("expected len(max_monthly_sales.keys)=%d; got: %d", want, got)
  223. }
  224. if got, want := agg.Keys[0], "2015-04-01"; got != want {
  225. t.Fatalf("expected max_monthly_sales.keys[0]=%v; got: %v", want, got)
  226. }
  227. if agg.Value == nil {
  228. t.Fatal("expected max_monthly_sales.value != nil")
  229. }
  230. if got, want := *agg.Value, float64(2448); got != want {
  231. t.Fatalf("expected max_monthly_sales.value=%v; got: %v", want, got)
  232. }
  233. }
  234. func TestAggsIntegrationMinBucket(t *testing.T) {
  235. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  236. client := setupTestClientAndCreateIndexAndAddDocs(t)
  237. esversion, err := client.ElasticsearchVersion(DefaultURL)
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. if esversion < "2.0" {
  242. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  243. return
  244. }
  245. // Match all should return all documents
  246. builder := client.Search().
  247. Index(testIndexName).
  248. Type("order").
  249. Query(NewMatchAllQuery()).
  250. Pretty(true)
  251. h := NewDateHistogramAggregation().Field("time").Interval("month")
  252. h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
  253. builder = builder.Aggregation("sales_per_month", h)
  254. builder = builder.Aggregation("min_monthly_sales", NewMinBucketAggregation().BucketsPath("sales_per_month>sales"))
  255. res, err := builder.Do(context.TODO())
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. if res.Hits == nil {
  260. t.Errorf("expected Hits != nil; got: nil")
  261. }
  262. aggs := res.Aggregations
  263. if aggs == nil {
  264. t.Fatal("expected aggregations != nil; got: nil")
  265. }
  266. agg, found := aggs.MinBucket("min_monthly_sales")
  267. if !found {
  268. t.Fatal("expected min_monthly_sales aggregation")
  269. }
  270. if agg == nil {
  271. t.Fatal("expected min_monthly_sales aggregation")
  272. }
  273. if got, want := len(agg.Keys), 1; got != want {
  274. t.Fatalf("expected len(min_monthly_sales.keys)=%d; got: %d", want, got)
  275. }
  276. if got, want := agg.Keys[0], "2015-06-01"; got != want {
  277. t.Fatalf("expected min_monthly_sales.keys[0]=%v; got: %v", want, got)
  278. }
  279. if agg.Value == nil {
  280. t.Fatal("expected min_monthly_sales.value != nil")
  281. }
  282. if got, want := *agg.Value, float64(68); got != want {
  283. t.Fatalf("expected min_monthly_sales.value=%v; got: %v", want, got)
  284. }
  285. }
  286. func TestAggsIntegrationSumBucket(t *testing.T) {
  287. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  288. client := setupTestClientAndCreateIndexAndAddDocs(t)
  289. esversion, err := client.ElasticsearchVersion(DefaultURL)
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. if esversion < "2.0" {
  294. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  295. return
  296. }
  297. // Match all should return all documents
  298. builder := client.Search().
  299. Index(testIndexName).
  300. Type("order").
  301. Query(NewMatchAllQuery()).
  302. Pretty(true)
  303. h := NewDateHistogramAggregation().Field("time").Interval("month")
  304. h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
  305. builder = builder.Aggregation("sales_per_month", h)
  306. builder = builder.Aggregation("sum_monthly_sales", NewSumBucketAggregation().BucketsPath("sales_per_month>sales"))
  307. res, err := builder.Do(context.TODO())
  308. if err != nil {
  309. t.Fatal(err)
  310. }
  311. if res.Hits == nil {
  312. t.Errorf("expected Hits != nil; got: nil")
  313. }
  314. aggs := res.Aggregations
  315. if aggs == nil {
  316. t.Fatal("expected aggregations != nil; got: nil")
  317. }
  318. agg, found := aggs.SumBucket("sum_monthly_sales")
  319. if !found {
  320. t.Fatal("expected sum_monthly_sales aggregation")
  321. }
  322. if agg == nil {
  323. t.Fatal("expected sum_monthly_sales aggregation")
  324. }
  325. if agg.Value == nil {
  326. t.Fatal("expected sum_monthly_sales.value != nil")
  327. }
  328. if got, want := *agg.Value, float64(4696.0); got != want {
  329. t.Fatalf("expected sum_monthly_sales.value=%v; got: %v", want, got)
  330. }
  331. }
  332. func TestAggsIntegrationMovAvg(t *testing.T) {
  333. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  334. client := setupTestClientAndCreateIndexAndAddDocs(t)
  335. esversion, err := client.ElasticsearchVersion(DefaultURL)
  336. if err != nil {
  337. t.Fatal(err)
  338. }
  339. if esversion < "2.0" {
  340. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  341. return
  342. }
  343. // Match all should return all documents
  344. builder := client.Search().
  345. Index(testIndexName).
  346. Type("order").
  347. Query(NewMatchAllQuery()).
  348. Pretty(true)
  349. h := NewDateHistogramAggregation().Field("time").Interval("month")
  350. h = h.SubAggregation("the_sum", NewSumAggregation().Field("price"))
  351. h = h.SubAggregation("the_movavg", NewMovAvgAggregation().BucketsPath("the_sum"))
  352. builder = builder.Aggregation("my_date_histo", h)
  353. res, err := builder.Do(context.TODO())
  354. if err != nil {
  355. t.Fatal(err)
  356. }
  357. if res.Hits == nil {
  358. t.Errorf("expected Hits != nil; got: nil")
  359. }
  360. aggs := res.Aggregations
  361. if aggs == nil {
  362. t.Fatal("expected aggregations != nil; got: nil")
  363. }
  364. agg, found := aggs.DateHistogram("my_date_histo")
  365. if !found {
  366. t.Fatal("expected sum_monthly_sales aggregation")
  367. }
  368. if agg == nil {
  369. t.Fatal("expected sum_monthly_sales aggregation")
  370. }
  371. if got, want := len(agg.Buckets), 6; got != want {
  372. t.Fatalf("expected %d buckets; got: %d", want, got)
  373. }
  374. d, found := agg.Buckets[0].MovAvg("the_movavg")
  375. if found {
  376. t.Fatal("expected no the_movavg aggregation")
  377. }
  378. if d != nil {
  379. t.Fatal("expected no the_movavg aggregation")
  380. }
  381. d, found = agg.Buckets[1].MovAvg("the_movavg")
  382. if found {
  383. t.Fatal("expected no the_movavg aggregation")
  384. }
  385. if d != nil {
  386. t.Fatal("expected no the_movavg aggregation")
  387. }
  388. d, found = agg.Buckets[2].MovAvg("the_movavg")
  389. if !found {
  390. t.Fatal("expected the_movavg aggregation")
  391. }
  392. if d == nil {
  393. t.Fatal("expected the_movavg aggregation")
  394. }
  395. if d.Value == nil {
  396. t.Fatal("expected the_movavg value")
  397. }
  398. if got, want := *d.Value, float64(1290.0); got != want {
  399. t.Fatalf("expected %v buckets; got: %v", want, got)
  400. }
  401. d, found = agg.Buckets[3].MovAvg("the_movavg")
  402. if !found {
  403. t.Fatal("expected the_movavg aggregation")
  404. }
  405. if d == nil {
  406. t.Fatal("expected the_movavg aggregation")
  407. }
  408. if d.Value == nil {
  409. t.Fatal("expected the_movavg value")
  410. }
  411. if got, want := *d.Value, float64(695.0); got != want {
  412. t.Fatalf("expected %v buckets; got: %v", want, got)
  413. }
  414. d, found = agg.Buckets[4].MovAvg("the_movavg")
  415. if !found {
  416. t.Fatal("expected the_movavg aggregation")
  417. }
  418. if d == nil {
  419. t.Fatal("expected the_movavg aggregation")
  420. }
  421. if d.Value == nil {
  422. t.Fatal("expected the_movavg value")
  423. }
  424. if got, want := *d.Value, float64(1279.3333333333333); got != want {
  425. t.Fatalf("expected %v buckets; got: %v", want, got)
  426. }
  427. d, found = agg.Buckets[5].MovAvg("the_movavg")
  428. if !found {
  429. t.Fatal("expected the_movavg aggregation")
  430. }
  431. if d == nil {
  432. t.Fatal("expected the_movavg aggregation")
  433. }
  434. if d.Value == nil {
  435. t.Fatal("expected the_movavg value")
  436. }
  437. if got, want := *d.Value, float64(1157.0); got != want {
  438. t.Fatalf("expected %v buckets; got: %v", want, got)
  439. }
  440. }
  441. func TestAggsIntegrationCumulativeSum(t *testing.T) {
  442. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  443. client := setupTestClientAndCreateIndexAndAddDocs(t)
  444. esversion, err := client.ElasticsearchVersion(DefaultURL)
  445. if err != nil {
  446. t.Fatal(err)
  447. }
  448. if esversion < "2.0" {
  449. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  450. return
  451. }
  452. // Match all should return all documents
  453. builder := client.Search().
  454. Index(testIndexName).
  455. Type("order").
  456. Query(NewMatchAllQuery()).
  457. Pretty(true)
  458. h := NewDateHistogramAggregation().Field("time").Interval("month")
  459. h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
  460. h = h.SubAggregation("cumulative_sales", NewCumulativeSumAggregation().BucketsPath("sales"))
  461. builder = builder.Aggregation("sales_per_month", h)
  462. res, err := builder.Do(context.TODO())
  463. if err != nil {
  464. t.Fatal(err)
  465. }
  466. if res.Hits == nil {
  467. t.Errorf("expected Hits != nil; got: nil")
  468. }
  469. aggs := res.Aggregations
  470. if aggs == nil {
  471. t.Fatal("expected aggregations != nil; got: nil")
  472. }
  473. agg, found := aggs.DateHistogram("sales_per_month")
  474. if !found {
  475. t.Fatal("expected sales_per_month aggregation")
  476. }
  477. if agg == nil {
  478. t.Fatal("expected sales_per_month aggregation")
  479. }
  480. if got, want := len(agg.Buckets), 6; got != want {
  481. t.Fatalf("expected %d buckets; got: %d", want, got)
  482. }
  483. if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
  484. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  485. }
  486. if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
  487. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  488. }
  489. if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
  490. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  491. }
  492. if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
  493. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  494. }
  495. if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
  496. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  497. }
  498. if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
  499. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  500. }
  501. d, found := agg.Buckets[0].CumulativeSum("cumulative_sales")
  502. if !found {
  503. t.Fatal("expected cumulative_sales aggregation")
  504. }
  505. if d == nil {
  506. t.Fatal("expected cumulative_sales aggregation")
  507. }
  508. if d.Value == nil {
  509. t.Fatal("expected cumulative_sales value != nil")
  510. }
  511. if got, want := *d.Value, float64(1290.0); got != want {
  512. t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
  513. }
  514. d, found = agg.Buckets[1].CumulativeSum("cumulative_sales")
  515. if !found {
  516. t.Fatal("expected cumulative_sales aggregation")
  517. }
  518. if d == nil {
  519. t.Fatal("expected cumulative_sales aggregation")
  520. }
  521. if d.Value == nil {
  522. t.Fatal("expected cumulative_sales value != nil")
  523. }
  524. if got, want := *d.Value, float64(1290.0); got != want {
  525. t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
  526. }
  527. d, found = agg.Buckets[2].CumulativeSum("cumulative_sales")
  528. if !found {
  529. t.Fatal("expected cumulative_sales aggregation")
  530. }
  531. if d == nil {
  532. t.Fatal("expected cumulative_sales aggregation")
  533. }
  534. if d.Value == nil {
  535. t.Fatal("expected cumulative_sales value != nil")
  536. }
  537. if got, want := *d.Value, float64(1390.0); got != want {
  538. t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
  539. }
  540. d, found = agg.Buckets[3].CumulativeSum("cumulative_sales")
  541. if !found {
  542. t.Fatal("expected cumulative_sales aggregation")
  543. }
  544. if d == nil {
  545. t.Fatal("expected cumulative_sales aggregation")
  546. }
  547. if d.Value == nil {
  548. t.Fatal("expected cumulative_sales value != nil")
  549. }
  550. if got, want := *d.Value, float64(3838.0); got != want {
  551. t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
  552. }
  553. d, found = agg.Buckets[4].CumulativeSum("cumulative_sales")
  554. if !found {
  555. t.Fatal("expected cumulative_sales aggregation")
  556. }
  557. if d == nil {
  558. t.Fatal("expected cumulative_sales aggregation")
  559. }
  560. if d.Value == nil {
  561. t.Fatal("expected cumulative_sales value != nil")
  562. }
  563. if got, want := *d.Value, float64(4628.0); got != want {
  564. t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
  565. }
  566. d, found = agg.Buckets[5].CumulativeSum("cumulative_sales")
  567. if !found {
  568. t.Fatal("expected cumulative_sales aggregation")
  569. }
  570. if d == nil {
  571. t.Fatal("expected cumulative_sales aggregation")
  572. }
  573. if d.Value == nil {
  574. t.Fatal("expected cumulative_sales value != nil")
  575. }
  576. if got, want := *d.Value, float64(4696.0); got != want {
  577. t.Fatalf("expected cumulative_sales.value=%v; got: %v", want, got)
  578. }
  579. }
  580. func TestAggsIntegrationBucketScript(t *testing.T) {
  581. client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  582. esversion, err := client.ElasticsearchVersion(DefaultURL)
  583. if err != nil {
  584. t.Fatal(err)
  585. }
  586. if esversion < "2.0" {
  587. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  588. return
  589. }
  590. // Match all should return all documents
  591. builder := client.Search().
  592. Index(testIndexName).
  593. Type("order").
  594. Query(NewMatchAllQuery()).
  595. Pretty(true)
  596. h := NewDateHistogramAggregation().Field("time").Interval("month")
  597. h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))
  598. appleFilter := NewFilterAggregation().Filter(NewTermQuery("manufacturer", "Apple"))
  599. appleFilter = appleFilter.SubAggregation("sales", NewSumAggregation().Field("price"))
  600. h = h.SubAggregation("apple_sales", appleFilter)
  601. h = h.SubAggregation("apple_percentage",
  602. NewBucketScriptAggregation().
  603. GapPolicy("insert_zeros").
  604. AddBucketsPath("appleSales", "apple_sales>sales").
  605. AddBucketsPath("totalSales", "total_sales").
  606. Script(NewScript("params.appleSales / params.totalSales * 100")))
  607. builder = builder.Aggregation("sales_per_month", h)
  608. res, err := builder.Pretty(true).Do(context.TODO())
  609. if err != nil {
  610. t.Fatalf("%v (maybe scripting is disabled?)", err)
  611. }
  612. if res.Hits == nil {
  613. t.Errorf("expected Hits != nil; got: nil")
  614. }
  615. aggs := res.Aggregations
  616. if aggs == nil {
  617. t.Fatal("expected aggregations != nil; got: nil")
  618. }
  619. agg, found := aggs.DateHistogram("sales_per_month")
  620. if !found {
  621. t.Fatal("expected sales_per_month aggregation")
  622. }
  623. if agg == nil {
  624. t.Fatal("expected sales_per_month aggregation")
  625. }
  626. if got, want := len(agg.Buckets), 6; got != want {
  627. t.Fatalf("expected %d buckets; got: %d", want, got)
  628. }
  629. if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
  630. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  631. }
  632. if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
  633. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  634. }
  635. if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
  636. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  637. }
  638. if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
  639. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  640. }
  641. if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
  642. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  643. }
  644. if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
  645. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  646. }
  647. d, found := agg.Buckets[0].BucketScript("apple_percentage")
  648. if !found {
  649. t.Fatal("expected apple_percentage aggregation")
  650. }
  651. if d == nil {
  652. t.Fatal("expected apple_percentage aggregation")
  653. }
  654. if d.Value == nil {
  655. t.Fatal("expected apple_percentage value != nil")
  656. }
  657. if got, want := *d.Value, float64(100.0); got != want {
  658. t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
  659. }
  660. d, found = agg.Buckets[1].BucketScript("apple_percentage")
  661. if !found {
  662. t.Fatal("expected apple_percentage aggregation")
  663. }
  664. if d == nil {
  665. t.Fatal("expected apple_percentage aggregation")
  666. }
  667. if d.Value != nil {
  668. t.Fatal("expected apple_percentage value == nil")
  669. }
  670. d, found = agg.Buckets[2].BucketScript("apple_percentage")
  671. if !found {
  672. t.Fatal("expected apple_percentage aggregation")
  673. }
  674. if d == nil {
  675. t.Fatal("expected apple_percentage aggregation")
  676. }
  677. if d.Value == nil {
  678. t.Fatal("expected apple_percentage value != nil")
  679. }
  680. if got, want := *d.Value, float64(0.0); got != want {
  681. t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
  682. }
  683. d, found = agg.Buckets[3].BucketScript("apple_percentage")
  684. if !found {
  685. t.Fatal("expected apple_percentage aggregation")
  686. }
  687. if d == nil {
  688. t.Fatal("expected apple_percentage aggregation")
  689. }
  690. if d.Value == nil {
  691. t.Fatal("expected apple_percentage value != nil")
  692. }
  693. if got, want := *d.Value, float64(34.64052287581699); got != want {
  694. t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
  695. }
  696. d, found = agg.Buckets[4].BucketScript("apple_percentage")
  697. if !found {
  698. t.Fatal("expected apple_percentage aggregation")
  699. }
  700. if d == nil {
  701. t.Fatal("expected apple_percentage aggregation")
  702. }
  703. if d.Value == nil {
  704. t.Fatal("expected apple_percentage value != nil")
  705. }
  706. if got, want := *d.Value, float64(0.0); got != want {
  707. t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
  708. }
  709. d, found = agg.Buckets[5].BucketScript("apple_percentage")
  710. if !found {
  711. t.Fatal("expected apple_percentage aggregation")
  712. }
  713. if d == nil {
  714. t.Fatal("expected apple_percentage aggregation")
  715. }
  716. if d.Value == nil {
  717. t.Fatal("expected apple_percentage value != nil")
  718. }
  719. if got, want := *d.Value, float64(0.0); got != want {
  720. t.Fatalf("expected apple_percentage.value=%v; got: %v", want, got)
  721. }
  722. }
  723. func TestAggsIntegrationBucketSelector(t *testing.T) {
  724. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  725. client := setupTestClientAndCreateIndexAndAddDocs(t)
  726. esversion, err := client.ElasticsearchVersion(DefaultURL)
  727. if err != nil {
  728. t.Fatal(err)
  729. }
  730. if esversion < "2.0" {
  731. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  732. return
  733. }
  734. // Match all should return all documents
  735. builder := client.Search().
  736. Index(testIndexName).
  737. Type("order").
  738. Query(NewMatchAllQuery()).
  739. Pretty(true)
  740. h := NewDateHistogramAggregation().Field("time").Interval("month")
  741. h = h.SubAggregation("total_sales", NewSumAggregation().Field("price"))
  742. h = h.SubAggregation("sales_bucket_filter",
  743. NewBucketSelectorAggregation().
  744. AddBucketsPath("totalSales", "total_sales").
  745. Script(NewScript("params.totalSales <= 100")))
  746. builder = builder.Aggregation("sales_per_month", h)
  747. res, err := builder.Do(context.TODO())
  748. if err != nil {
  749. t.Fatalf("%v (maybe scripting is disabled?)", err)
  750. }
  751. if res.Hits == nil {
  752. t.Errorf("expected Hits != nil; got: nil")
  753. }
  754. aggs := res.Aggregations
  755. if aggs == nil {
  756. t.Fatal("expected aggregations != nil; got: nil")
  757. }
  758. agg, found := aggs.DateHistogram("sales_per_month")
  759. if !found {
  760. t.Fatal("expected sales_per_month aggregation")
  761. }
  762. if agg == nil {
  763. t.Fatal("expected sales_per_month aggregation")
  764. }
  765. if got, want := len(agg.Buckets), 2; got != want {
  766. t.Fatalf("expected %d buckets; got: %d", want, got)
  767. }
  768. if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
  769. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  770. }
  771. if got, want := agg.Buckets[1].DocCount, int64(2); got != want {
  772. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  773. }
  774. }
  775. func TestAggsIntegrationSerialDiff(t *testing.T) {
  776. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  777. client := setupTestClientAndCreateIndexAndAddDocs(t)
  778. esversion, err := client.ElasticsearchVersion(DefaultURL)
  779. if err != nil {
  780. t.Fatal(err)
  781. }
  782. if esversion < "2.0" {
  783. t.Skipf("Elasticsearch %s does not have pipeline aggregations.", esversion)
  784. return
  785. }
  786. // Match all should return all documents
  787. builder := client.Search().
  788. Index(testIndexName).
  789. Type("order").
  790. Query(NewMatchAllQuery()).
  791. Pretty(true)
  792. h := NewDateHistogramAggregation().Field("time").Interval("month")
  793. h = h.SubAggregation("sales", NewSumAggregation().Field("price"))
  794. h = h.SubAggregation("the_diff", NewSerialDiffAggregation().BucketsPath("sales").Lag(1))
  795. builder = builder.Aggregation("sales_per_month", h)
  796. res, err := builder.Do(context.TODO())
  797. if err != nil {
  798. t.Fatal(err)
  799. }
  800. if res.Hits == nil {
  801. t.Errorf("expected Hits != nil; got: nil")
  802. }
  803. aggs := res.Aggregations
  804. if aggs == nil {
  805. t.Fatal("expected aggregations != nil; got: nil")
  806. }
  807. agg, found := aggs.DateHistogram("sales_per_month")
  808. if !found {
  809. t.Fatal("expected sales_per_month aggregation")
  810. }
  811. if agg == nil {
  812. t.Fatal("expected sales_per_month aggregation")
  813. }
  814. if got, want := len(agg.Buckets), 6; got != want {
  815. t.Fatalf("expected %d buckets; got: %d", want, got)
  816. }
  817. if got, want := agg.Buckets[0].DocCount, int64(1); got != want {
  818. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  819. }
  820. if got, want := agg.Buckets[1].DocCount, int64(0); got != want {
  821. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  822. }
  823. if got, want := agg.Buckets[2].DocCount, int64(1); got != want {
  824. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  825. }
  826. if got, want := agg.Buckets[3].DocCount, int64(3); got != want {
  827. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  828. }
  829. if got, want := agg.Buckets[4].DocCount, int64(1); got != want {
  830. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  831. }
  832. if got, want := agg.Buckets[5].DocCount, int64(2); got != want {
  833. t.Fatalf("expected DocCount=%d; got: %d", want, got)
  834. }
  835. d, found := agg.Buckets[0].SerialDiff("the_diff")
  836. if found {
  837. t.Fatal("expected no the_diff aggregation")
  838. }
  839. if d != nil {
  840. t.Fatal("expected no the_diff aggregation")
  841. }
  842. d, found = agg.Buckets[1].SerialDiff("the_diff")
  843. if found {
  844. t.Fatal("expected no the_diff aggregation")
  845. }
  846. if d != nil {
  847. t.Fatal("expected no the_diff aggregation")
  848. }
  849. d, found = agg.Buckets[2].SerialDiff("the_diff")
  850. if found {
  851. t.Fatal("expected no the_diff aggregation")
  852. }
  853. if d != nil {
  854. t.Fatal("expected no the_diff aggregation")
  855. }
  856. d, found = agg.Buckets[3].SerialDiff("the_diff")
  857. if !found {
  858. t.Fatal("expected the_diff aggregation")
  859. }
  860. if d == nil {
  861. t.Fatal("expected the_diff aggregation")
  862. }
  863. if d.Value == nil {
  864. t.Fatal("expected the_diff value != nil")
  865. }
  866. if got, want := *d.Value, float64(2348.0); got != want {
  867. t.Fatalf("expected the_diff.value=%v; got: %v", want, got)
  868. }
  869. d, found = agg.Buckets[4].SerialDiff("the_diff")
  870. if !found {
  871. t.Fatal("expected the_diff aggregation")
  872. }
  873. if d == nil {
  874. t.Fatal("expected the_diff aggregation")
  875. }
  876. if d.Value == nil {
  877. t.Fatal("expected the_diff value != nil")
  878. }
  879. if got, want := *d.Value, float64(-1658.0); got != want {
  880. t.Fatalf("expected the_diff.value=%v; got: %v", want, got)
  881. }
  882. d, found = agg.Buckets[5].SerialDiff("the_diff")
  883. if !found {
  884. t.Fatal("expected the_diff aggregation")
  885. }
  886. if d == nil {
  887. t.Fatal("expected the_diff aggregation")
  888. }
  889. if d.Value == nil {
  890. t.Fatal("expected the_diff value != nil")
  891. }
  892. if got, want := *d.Value, float64(-722.0); got != want {
  893. t.Fatalf("expected the_diff.value=%v; got: %v", want, got)
  894. }
  895. }