search_aggs_test.go 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313
  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. "encoding/json"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestAggs(t *testing.T) {
  13. // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  14. client := setupTestClientAndCreateIndex(t)
  15. tweet1 := tweet{
  16. User: "olivere",
  17. Retweets: 108,
  18. Message: "Welcome to Golang and Elasticsearch.",
  19. Image: "http://golang.org/doc/gopher/gophercolor.png",
  20. Tags: []string{"golang", "elasticsearch"},
  21. Location: "48.1333,11.5667", // lat,lon
  22. Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
  23. }
  24. tweet2 := tweet{
  25. User: "olivere",
  26. Retweets: 0,
  27. Message: "Another unrelated topic.",
  28. Tags: []string{"golang"},
  29. Location: "48.1189,11.4289", // lat,lon
  30. Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
  31. }
  32. tweet3 := tweet{
  33. User: "sandrae",
  34. Retweets: 12,
  35. Message: "Cycling is fun.",
  36. Tags: []string{"sports", "cycling"},
  37. Location: "47.7167,11.7167", // lat,lon
  38. Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
  39. }
  40. // Add all documents
  41. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. // Match all should return all documents
  58. all := NewMatchAllQuery()
  59. // Terms Aggregate by user name
  60. globalAgg := NewGlobalAggregation()
  61. usersAgg := NewTermsAggregation().Field("user").Size(10).OrderByCountDesc()
  62. retweetsAgg := NewTermsAggregation().Field("retweets").Size(10)
  63. avgRetweetsAgg := NewAvgAggregation().Field("retweets")
  64. avgRetweetsWithMetaAgg := NewAvgAggregation().Field("retweetsMeta").Meta(map[string]interface{}{"meta": true})
  65. minRetweetsAgg := NewMinAggregation().Field("retweets")
  66. maxRetweetsAgg := NewMaxAggregation().Field("retweets")
  67. sumRetweetsAgg := NewSumAggregation().Field("retweets")
  68. statsRetweetsAgg := NewStatsAggregation().Field("retweets")
  69. extstatsRetweetsAgg := NewExtendedStatsAggregation().Field("retweets")
  70. valueCountRetweetsAgg := NewValueCountAggregation().Field("retweets")
  71. percentilesRetweetsAgg := NewPercentilesAggregation().Field("retweets")
  72. percentileRanksRetweetsAgg := NewPercentileRanksAggregation().Field("retweets").Values(25, 50, 75)
  73. cardinalityAgg := NewCardinalityAggregation().Field("user")
  74. significantTermsAgg := NewSignificantTermsAggregation().Field("message")
  75. samplerAgg := NewSamplerAggregation().SubAggregation("tagged_with", NewTermsAggregation().Field("tags"))
  76. retweetsRangeAgg := NewRangeAggregation().Field("retweets").Lt(10).Between(10, 100).Gt(100)
  77. retweetsKeyedRangeAgg := NewRangeAggregation().Field("retweets").Keyed(true).Lt(10).Between(10, 100).Gt(100)
  78. dateRangeAgg := NewDateRangeAggregation().Field("created").Lt("2012-01-01").Between("2012-01-01", "2013-01-01").Gt("2013-01-01")
  79. missingTagsAgg := NewMissingAggregation().Field("tags")
  80. retweetsHistoAgg := NewHistogramAggregation().Field("retweets").Interval(100)
  81. dateHistoAgg := NewDateHistogramAggregation().Field("created").Interval("year")
  82. dateHistoKeyedAgg := NewDateHistogramAggregation().Field("created").Interval("year").Keyed(true)
  83. retweetsFilterAgg := NewFilterAggregation().Filter(
  84. NewRangeQuery("created").Gte("2012-01-01").Lte("2012-12-31")).
  85. SubAggregation("avgRetweetsSub", NewAvgAggregation().Field("retweets"))
  86. queryFilterAgg := NewFilterAggregation().Filter(NewTermQuery("tags", "golang"))
  87. topTagsHitsAgg := NewTopHitsAggregation().Sort("created", false).Size(5).FetchSource(true)
  88. topTagsAgg := NewTermsAggregation().Field("tags").Size(3).SubAggregation("top_tag_hits", topTagsHitsAgg)
  89. geoBoundsAgg := NewGeoBoundsAggregation().Field("location")
  90. geoHashAgg := NewGeoHashGridAggregation().Field("location").Precision(5)
  91. geoCentroidAgg := NewGeoCentroidAggregation().Field("location")
  92. // Run query
  93. builder := client.Search().Index(testIndexName).Query(all).Pretty(true)
  94. builder = builder.Aggregation("global", globalAgg)
  95. builder = builder.Aggregation("users", usersAgg)
  96. builder = builder.Aggregation("retweets", retweetsAgg)
  97. builder = builder.Aggregation("avgRetweets", avgRetweetsAgg)
  98. builder = builder.Aggregation("avgRetweetsWithMeta", avgRetweetsWithMetaAgg)
  99. builder = builder.Aggregation("minRetweets", minRetweetsAgg)
  100. builder = builder.Aggregation("maxRetweets", maxRetweetsAgg)
  101. builder = builder.Aggregation("sumRetweets", sumRetweetsAgg)
  102. builder = builder.Aggregation("statsRetweets", statsRetweetsAgg)
  103. builder = builder.Aggregation("extstatsRetweets", extstatsRetweetsAgg)
  104. builder = builder.Aggregation("valueCountRetweets", valueCountRetweetsAgg)
  105. builder = builder.Aggregation("percentilesRetweets", percentilesRetweetsAgg)
  106. builder = builder.Aggregation("percentileRanksRetweets", percentileRanksRetweetsAgg)
  107. builder = builder.Aggregation("usersCardinality", cardinalityAgg)
  108. builder = builder.Aggregation("significantTerms", significantTermsAgg)
  109. builder = builder.Aggregation("sample", samplerAgg)
  110. builder = builder.Aggregation("retweetsRange", retweetsRangeAgg)
  111. builder = builder.Aggregation("retweetsKeyedRange", retweetsKeyedRangeAgg)
  112. builder = builder.Aggregation("dateRange", dateRangeAgg)
  113. builder = builder.Aggregation("missingTags", missingTagsAgg)
  114. builder = builder.Aggregation("retweetsHisto", retweetsHistoAgg)
  115. builder = builder.Aggregation("dateHisto", dateHistoAgg)
  116. builder = builder.Aggregation("dateHistoKeyed", dateHistoKeyedAgg)
  117. builder = builder.Aggregation("retweetsFilter", retweetsFilterAgg)
  118. builder = builder.Aggregation("queryFilter", queryFilterAgg)
  119. builder = builder.Aggregation("top-tags", topTagsAgg)
  120. builder = builder.Aggregation("viewport", geoBoundsAgg)
  121. builder = builder.Aggregation("geohashed", geoHashAgg)
  122. builder = builder.Aggregation("centroid", geoCentroidAgg)
  123. // Unnamed filters
  124. countByUserAgg := NewFiltersAggregation().
  125. Filters(NewTermQuery("user", "olivere"), NewTermQuery("user", "sandrae"))
  126. builder = builder.Aggregation("countByUser", countByUserAgg)
  127. // Named filters
  128. countByUserAgg2 := NewFiltersAggregation().
  129. FilterWithName("olivere", NewTermQuery("user", "olivere")).
  130. FilterWithName("sandrae", NewTermQuery("user", "sandrae"))
  131. builder = builder.Aggregation("countByUser2", countByUserAgg2)
  132. // AvgBucket
  133. dateHisto := NewDateHistogramAggregation().Field("created").Interval("year")
  134. dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
  135. builder = builder.Aggregation("avgBucketDateHisto", dateHisto)
  136. builder = builder.Aggregation("avgSumOfRetweets", NewAvgBucketAggregation().BucketsPath("avgBucketDateHisto>sumOfRetweets"))
  137. // MinBucket
  138. dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
  139. dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
  140. builder = builder.Aggregation("minBucketDateHisto", dateHisto)
  141. builder = builder.Aggregation("minBucketSumOfRetweets", NewMinBucketAggregation().BucketsPath("minBucketDateHisto>sumOfRetweets"))
  142. // MaxBucket
  143. dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
  144. dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
  145. builder = builder.Aggregation("maxBucketDateHisto", dateHisto)
  146. builder = builder.Aggregation("maxBucketSumOfRetweets", NewMaxBucketAggregation().BucketsPath("maxBucketDateHisto>sumOfRetweets"))
  147. // SumBucket
  148. dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
  149. dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
  150. builder = builder.Aggregation("sumBucketDateHisto", dateHisto)
  151. builder = builder.Aggregation("sumBucketSumOfRetweets", NewSumBucketAggregation().BucketsPath("sumBucketDateHisto>sumOfRetweets"))
  152. // MovAvg
  153. dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
  154. dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
  155. dateHisto = dateHisto.SubAggregation("movingAvg", NewMovAvgAggregation().BucketsPath("sumOfRetweets"))
  156. builder = builder.Aggregation("movingAvgDateHisto", dateHisto)
  157. searchResult, err := builder.Do(context.TODO())
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. if searchResult.Hits == nil {
  162. t.Errorf("expected Hits != nil; got: nil")
  163. }
  164. if searchResult.Hits.TotalHits != 3 {
  165. t.Errorf("expected Hits.TotalHits = %d; got: %d", 3, searchResult.Hits.TotalHits)
  166. }
  167. if len(searchResult.Hits.Hits) != 3 {
  168. t.Errorf("expected len(Hits.Hits) = %d; got: %d", 3, len(searchResult.Hits.Hits))
  169. }
  170. agg := searchResult.Aggregations
  171. if agg == nil {
  172. t.Fatalf("expected Aggregations != nil; got: nil")
  173. }
  174. // Search for non-existent aggregate should return (nil, false)
  175. unknownAgg, found := agg.Terms("no-such-aggregate")
  176. if found {
  177. t.Errorf("expected unknown aggregation to not be found; got: %v", found)
  178. }
  179. if unknownAgg != nil {
  180. t.Errorf("expected unknown aggregation to return %v; got %v", nil, unknownAgg)
  181. }
  182. // Global
  183. globalAggRes, found := agg.Global("global")
  184. if !found {
  185. t.Errorf("expected %v; got: %v", true, found)
  186. }
  187. if globalAggRes == nil {
  188. t.Fatalf("expected != nil; got: nil")
  189. }
  190. if globalAggRes.DocCount != 3 {
  191. t.Errorf("expected DocCount = %d; got: %d", 3, globalAggRes.DocCount)
  192. }
  193. // Search for existent aggregate (by name) should return (aggregate, true)
  194. termsAggRes, found := agg.Terms("users")
  195. if !found {
  196. t.Errorf("expected %v; got: %v", true, found)
  197. }
  198. if termsAggRes == nil {
  199. t.Fatalf("expected != nil; got: nil")
  200. }
  201. if len(termsAggRes.Buckets) != 2 {
  202. t.Fatalf("expected %d; got: %d", 2, len(termsAggRes.Buckets))
  203. }
  204. if termsAggRes.Buckets[0].Key != "olivere" {
  205. t.Errorf("expected %q; got: %q", "olivere", termsAggRes.Buckets[0].Key)
  206. }
  207. if termsAggRes.Buckets[0].DocCount != 2 {
  208. t.Errorf("expected %d; got: %d", 2, termsAggRes.Buckets[0].DocCount)
  209. }
  210. if termsAggRes.Buckets[1].Key != "sandrae" {
  211. t.Errorf("expected %q; got: %q", "sandrae", termsAggRes.Buckets[1].Key)
  212. }
  213. if termsAggRes.Buckets[1].DocCount != 1 {
  214. t.Errorf("expected %d; got: %d", 1, termsAggRes.Buckets[1].DocCount)
  215. }
  216. // A terms aggregate with keys that are not strings
  217. retweetsAggRes, found := agg.Terms("retweets")
  218. if !found {
  219. t.Errorf("expected %v; got: %v", true, found)
  220. }
  221. if retweetsAggRes == nil {
  222. t.Fatalf("expected != nil; got: nil")
  223. }
  224. if len(retweetsAggRes.Buckets) != 3 {
  225. t.Fatalf("expected %d; got: %d", 3, len(retweetsAggRes.Buckets))
  226. }
  227. if retweetsAggRes.Buckets[0].Key != float64(0) {
  228. t.Errorf("expected %v; got: %v", float64(0), retweetsAggRes.Buckets[0].Key)
  229. }
  230. if got, err := retweetsAggRes.Buckets[0].KeyNumber.Int64(); err != nil {
  231. t.Errorf("expected %d; got: %v", 0, retweetsAggRes.Buckets[0].Key)
  232. } else if got != 0 {
  233. t.Errorf("expected %d; got: %d", 0, got)
  234. }
  235. if retweetsAggRes.Buckets[0].KeyNumber != "0" {
  236. t.Errorf("expected %q; got: %q", "0", retweetsAggRes.Buckets[0].KeyNumber)
  237. }
  238. if retweetsAggRes.Buckets[0].DocCount != 1 {
  239. t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[0].DocCount)
  240. }
  241. if retweetsAggRes.Buckets[1].Key != float64(12) {
  242. t.Errorf("expected %v; got: %v", float64(12), retweetsAggRes.Buckets[1].Key)
  243. }
  244. if got, err := retweetsAggRes.Buckets[1].KeyNumber.Int64(); err != nil {
  245. t.Errorf("expected %d; got: %v", 0, retweetsAggRes.Buckets[1].KeyNumber)
  246. } else if got != 12 {
  247. t.Errorf("expected %d; got: %d", 12, got)
  248. }
  249. if retweetsAggRes.Buckets[1].KeyNumber != "12" {
  250. t.Errorf("expected %q; got: %q", "12", retweetsAggRes.Buckets[1].KeyNumber)
  251. }
  252. if retweetsAggRes.Buckets[1].DocCount != 1 {
  253. t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[1].DocCount)
  254. }
  255. if retweetsAggRes.Buckets[2].Key != float64(108) {
  256. t.Errorf("expected %v; got: %v", float64(108), retweetsAggRes.Buckets[2].Key)
  257. }
  258. if got, err := retweetsAggRes.Buckets[2].KeyNumber.Int64(); err != nil {
  259. t.Errorf("expected %d; got: %v", 108, retweetsAggRes.Buckets[2].KeyNumber)
  260. } else if got != 108 {
  261. t.Errorf("expected %d; got: %d", 108, got)
  262. }
  263. if retweetsAggRes.Buckets[2].KeyNumber != "108" {
  264. t.Errorf("expected %q; got: %q", "108", retweetsAggRes.Buckets[2].KeyNumber)
  265. }
  266. if retweetsAggRes.Buckets[2].DocCount != 1 {
  267. t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[2].DocCount)
  268. }
  269. // avgRetweets
  270. avgAggRes, found := agg.Avg("avgRetweets")
  271. if !found {
  272. t.Errorf("expected %v; got: %v", true, found)
  273. }
  274. if avgAggRes == nil {
  275. t.Fatalf("expected != nil; got: nil")
  276. }
  277. if avgAggRes.Value == nil {
  278. t.Fatalf("expected != nil; got: %v", *avgAggRes.Value)
  279. }
  280. if *avgAggRes.Value != 40.0 {
  281. t.Errorf("expected %v; got: %v", 40.0, *avgAggRes.Value)
  282. }
  283. // avgRetweetsWithMeta
  284. avgMetaAggRes, found := agg.Avg("avgRetweetsWithMeta")
  285. if !found {
  286. t.Errorf("expected %v; got: %v", true, found)
  287. }
  288. if avgMetaAggRes == nil {
  289. t.Fatalf("expected != nil; got: nil")
  290. }
  291. if avgMetaAggRes.Meta == nil {
  292. t.Fatalf("expected != nil; got: %v", avgMetaAggRes.Meta)
  293. }
  294. metaDataValue, found := avgMetaAggRes.Meta["meta"]
  295. if !found {
  296. t.Fatalf("expected to return meta data key %q; got: %v", "meta", found)
  297. }
  298. if flag, ok := metaDataValue.(bool); !ok {
  299. t.Fatalf("expected to return meta data key type %T; got: %T", true, metaDataValue)
  300. } else if flag != true {
  301. t.Fatalf("expected to return meta data key value %v; got: %v", true, flag)
  302. }
  303. // minRetweets
  304. minAggRes, found := agg.Min("minRetweets")
  305. if !found {
  306. t.Errorf("expected %v; got: %v", true, found)
  307. }
  308. if minAggRes == nil {
  309. t.Fatalf("expected != nil; got: nil")
  310. }
  311. if minAggRes.Value == nil {
  312. t.Fatalf("expected != nil; got: %v", *minAggRes.Value)
  313. }
  314. if *minAggRes.Value != 0.0 {
  315. t.Errorf("expected %v; got: %v", 0.0, *minAggRes.Value)
  316. }
  317. // maxRetweets
  318. maxAggRes, found := agg.Max("maxRetweets")
  319. if !found {
  320. t.Errorf("expected %v; got: %v", true, found)
  321. }
  322. if maxAggRes == nil {
  323. t.Fatalf("expected != nil; got: nil")
  324. }
  325. if maxAggRes.Value == nil {
  326. t.Fatalf("expected != nil; got: %v", *maxAggRes.Value)
  327. }
  328. if *maxAggRes.Value != 108.0 {
  329. t.Errorf("expected %v; got: %v", 108.0, *maxAggRes.Value)
  330. }
  331. // sumRetweets
  332. sumAggRes, found := agg.Sum("sumRetweets")
  333. if !found {
  334. t.Errorf("expected %v; got: %v", true, found)
  335. }
  336. if sumAggRes == nil {
  337. t.Fatalf("expected != nil; got: nil")
  338. }
  339. if sumAggRes.Value == nil {
  340. t.Fatalf("expected != nil; got: %v", *sumAggRes.Value)
  341. }
  342. if *sumAggRes.Value != 120.0 {
  343. t.Errorf("expected %v; got: %v", 120.0, *sumAggRes.Value)
  344. }
  345. // statsRetweets
  346. statsAggRes, found := agg.Stats("statsRetweets")
  347. if !found {
  348. t.Errorf("expected %v; got: %v", true, found)
  349. }
  350. if statsAggRes == nil {
  351. t.Fatalf("expected != nil; got: nil")
  352. }
  353. if statsAggRes.Count != 3 {
  354. t.Errorf("expected %d; got: %d", 3, statsAggRes.Count)
  355. }
  356. if statsAggRes.Min == nil {
  357. t.Fatalf("expected != nil; got: %v", *statsAggRes.Min)
  358. }
  359. if *statsAggRes.Min != 0.0 {
  360. t.Errorf("expected %v; got: %v", 0.0, *statsAggRes.Min)
  361. }
  362. if statsAggRes.Max == nil {
  363. t.Fatalf("expected != nil; got: %v", *statsAggRes.Max)
  364. }
  365. if *statsAggRes.Max != 108.0 {
  366. t.Errorf("expected %v; got: %v", 108.0, *statsAggRes.Max)
  367. }
  368. if statsAggRes.Avg == nil {
  369. t.Fatalf("expected != nil; got: %v", *statsAggRes.Avg)
  370. }
  371. if *statsAggRes.Avg != 40.0 {
  372. t.Errorf("expected %v; got: %v", 40.0, *statsAggRes.Avg)
  373. }
  374. if statsAggRes.Sum == nil {
  375. t.Fatalf("expected != nil; got: %v", *statsAggRes.Sum)
  376. }
  377. if *statsAggRes.Sum != 120.0 {
  378. t.Errorf("expected %v; got: %v", 120.0, *statsAggRes.Sum)
  379. }
  380. // extstatsRetweets
  381. extStatsAggRes, found := agg.ExtendedStats("extstatsRetweets")
  382. if !found {
  383. t.Errorf("expected %v; got: %v", true, found)
  384. }
  385. if extStatsAggRes == nil {
  386. t.Fatalf("expected != nil; got: nil")
  387. }
  388. if extStatsAggRes.Count != 3 {
  389. t.Errorf("expected %d; got: %d", 3, extStatsAggRes.Count)
  390. }
  391. if extStatsAggRes.Min == nil {
  392. t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Min)
  393. }
  394. if *extStatsAggRes.Min != 0.0 {
  395. t.Errorf("expected %v; got: %v", 0.0, *extStatsAggRes.Min)
  396. }
  397. if extStatsAggRes.Max == nil {
  398. t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Max)
  399. }
  400. if *extStatsAggRes.Max != 108.0 {
  401. t.Errorf("expected %v; got: %v", 108.0, *extStatsAggRes.Max)
  402. }
  403. if extStatsAggRes.Avg == nil {
  404. t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Avg)
  405. }
  406. if *extStatsAggRes.Avg != 40.0 {
  407. t.Errorf("expected %v; got: %v", 40.0, *extStatsAggRes.Avg)
  408. }
  409. if extStatsAggRes.Sum == nil {
  410. t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Sum)
  411. }
  412. if *extStatsAggRes.Sum != 120.0 {
  413. t.Errorf("expected %v; got: %v", 120.0, *extStatsAggRes.Sum)
  414. }
  415. if extStatsAggRes.SumOfSquares == nil {
  416. t.Fatalf("expected != nil; got: %v", *extStatsAggRes.SumOfSquares)
  417. }
  418. if *extStatsAggRes.SumOfSquares != 11808.0 {
  419. t.Errorf("expected %v; got: %v", 11808.0, *extStatsAggRes.SumOfSquares)
  420. }
  421. if extStatsAggRes.Variance == nil {
  422. t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Variance)
  423. }
  424. if *extStatsAggRes.Variance != 2336.0 {
  425. t.Errorf("expected %v; got: %v", 2336.0, *extStatsAggRes.Variance)
  426. }
  427. if extStatsAggRes.StdDeviation == nil {
  428. t.Fatalf("expected != nil; got: %v", *extStatsAggRes.StdDeviation)
  429. }
  430. if *extStatsAggRes.StdDeviation != 48.33218389437829 {
  431. t.Errorf("expected %v; got: %v", 48.33218389437829, *extStatsAggRes.StdDeviation)
  432. }
  433. // valueCountRetweets
  434. valueCountAggRes, found := agg.ValueCount("valueCountRetweets")
  435. if !found {
  436. t.Errorf("expected %v; got: %v", true, found)
  437. }
  438. if valueCountAggRes == nil {
  439. t.Fatalf("expected != nil; got: nil")
  440. }
  441. if valueCountAggRes.Value == nil {
  442. t.Fatalf("expected != nil; got: %v", *valueCountAggRes.Value)
  443. }
  444. if *valueCountAggRes.Value != 3.0 {
  445. t.Errorf("expected %v; got: %v", 3.0, *valueCountAggRes.Value)
  446. }
  447. // percentilesRetweets
  448. percentilesAggRes, found := agg.Percentiles("percentilesRetweets")
  449. if !found {
  450. t.Errorf("expected %v; got: %v", true, found)
  451. }
  452. if percentilesAggRes == nil {
  453. t.Fatalf("expected != nil; got: nil")
  454. }
  455. // ES 1.4.x returns 7: {"1.0":...}
  456. // ES 1.5.0 returns 14: {"1.0":..., "1.0_as_string":...}
  457. // So we're relaxing the test here.
  458. if len(percentilesAggRes.Values) == 0 {
  459. t.Errorf("expected at least %d value; got: %d\nValues are: %#v", 1, len(percentilesAggRes.Values), percentilesAggRes.Values)
  460. }
  461. if _, found := percentilesAggRes.Values["0.0"]; found {
  462. t.Errorf("expected %v; got: %v", false, found)
  463. }
  464. if percentilesAggRes.Values["1.0"] != 0.24 {
  465. t.Errorf("expected %v; got: %v", 0.24, percentilesAggRes.Values["1.0"])
  466. }
  467. if percentilesAggRes.Values["25.0"] != 6.0 {
  468. t.Errorf("expected %v; got: %v", 6.0, percentilesAggRes.Values["25.0"])
  469. }
  470. if percentilesAggRes.Values["99.0"] != 106.08 {
  471. t.Errorf("expected %v; got: %v", 106.08, percentilesAggRes.Values["99.0"])
  472. }
  473. // percentileRanksRetweets
  474. percentileRanksAggRes, found := agg.PercentileRanks("percentileRanksRetweets")
  475. if !found {
  476. t.Errorf("expected %v; got: %v", true, found)
  477. }
  478. if percentileRanksAggRes == nil {
  479. t.Fatalf("expected != nil; got: nil")
  480. }
  481. if len(percentileRanksAggRes.Values) == 0 {
  482. t.Errorf("expected at least %d value; got %d\nValues are: %#v", 1, len(percentileRanksAggRes.Values), percentileRanksAggRes.Values)
  483. }
  484. if _, found := percentileRanksAggRes.Values["0.0"]; found {
  485. t.Errorf("expected %v; got: %v", true, found)
  486. }
  487. if percentileRanksAggRes.Values["25.0"] != 21.180555555555557 {
  488. t.Errorf("expected %v; got: %v", 21.180555555555557, percentileRanksAggRes.Values["25.0"])
  489. }
  490. if percentileRanksAggRes.Values["50.0"] != 29.86111111111111 {
  491. t.Errorf("expected %v; got: %v", 29.86111111111111, percentileRanksAggRes.Values["50.0"])
  492. }
  493. if percentileRanksAggRes.Values["75.0"] != 38.54166666666667 {
  494. t.Errorf("expected %v; got: %v", 38.54166666666667, percentileRanksAggRes.Values["75.0"])
  495. }
  496. // usersCardinality
  497. cardAggRes, found := agg.Cardinality("usersCardinality")
  498. if !found {
  499. t.Errorf("expected %v; got: %v", true, found)
  500. }
  501. if cardAggRes == nil {
  502. t.Fatalf("expected != nil; got: nil")
  503. }
  504. if cardAggRes.Value == nil {
  505. t.Fatalf("expected != nil; got: %v", *cardAggRes.Value)
  506. }
  507. if *cardAggRes.Value != 2 {
  508. t.Errorf("expected %v; got: %v", 2, *cardAggRes.Value)
  509. }
  510. // retweetsFilter
  511. filterAggRes, found := agg.Filter("retweetsFilter")
  512. if !found {
  513. t.Errorf("expected %v; got: %v", true, found)
  514. }
  515. if filterAggRes == nil {
  516. t.Fatalf("expected != nil; got: nil")
  517. }
  518. if filterAggRes.DocCount != 2 {
  519. t.Fatalf("expected %v; got: %v", 2, filterAggRes.DocCount)
  520. }
  521. // Retrieve sub-aggregation
  522. avgRetweetsAggRes, found := filterAggRes.Avg("avgRetweetsSub")
  523. if !found {
  524. t.Error("expected sub-aggregation \"avgRetweets\" to be found; got false")
  525. }
  526. if avgRetweetsAggRes == nil {
  527. t.Fatal("expected sub-aggregation \"avgRetweets\"; got nil")
  528. }
  529. if avgRetweetsAggRes.Value == nil {
  530. t.Fatalf("expected != nil; got: %v", avgRetweetsAggRes.Value)
  531. }
  532. if *avgRetweetsAggRes.Value != 54.0 {
  533. t.Errorf("expected %v; got: %v", 54.0, *avgRetweetsAggRes.Value)
  534. }
  535. // queryFilter
  536. queryFilterAggRes, found := agg.Filter("queryFilter")
  537. if !found {
  538. t.Errorf("expected %v; got: %v", true, found)
  539. }
  540. if queryFilterAggRes == nil {
  541. t.Fatalf("expected != nil; got: nil")
  542. }
  543. if queryFilterAggRes.DocCount != 2 {
  544. t.Fatalf("expected %v; got: %v", 2, queryFilterAggRes.DocCount)
  545. }
  546. // significantTerms
  547. stAggRes, found := agg.SignificantTerms("significantTerms")
  548. if !found {
  549. t.Errorf("expected %v; got: %v", true, found)
  550. }
  551. if stAggRes == nil {
  552. t.Fatalf("expected != nil; got: nil")
  553. }
  554. if stAggRes.DocCount != 3 {
  555. t.Errorf("expected %v; got: %v", 3, stAggRes.DocCount)
  556. }
  557. if len(stAggRes.Buckets) != 0 {
  558. t.Errorf("expected %v; got: %v", 0, len(stAggRes.Buckets))
  559. }
  560. // sampler
  561. samplerAggRes, found := agg.Sampler("sample")
  562. if !found {
  563. t.Errorf("expected %v; got: %v", true, found)
  564. }
  565. if samplerAggRes == nil {
  566. t.Fatalf("expected != nil; got: nil")
  567. }
  568. if samplerAggRes.DocCount != 3 {
  569. t.Errorf("expected %v; got: %v", 3, samplerAggRes.DocCount)
  570. }
  571. sub, found := samplerAggRes.Aggregations["tagged_with"]
  572. if !found {
  573. t.Fatalf("expected sub aggregation %q", "tagged_with")
  574. }
  575. if sub == nil {
  576. t.Fatalf("expected sub aggregation %q; got: %v", "tagged_with", sub)
  577. }
  578. // retweetsRange
  579. rangeAggRes, found := agg.Range("retweetsRange")
  580. if !found {
  581. t.Errorf("expected %v; got: %v", true, found)
  582. }
  583. if rangeAggRes == nil {
  584. t.Fatal("expected != nil; got: nil")
  585. }
  586. if len(rangeAggRes.Buckets) != 3 {
  587. t.Fatalf("expected %d; got: %d", 3, len(rangeAggRes.Buckets))
  588. }
  589. if rangeAggRes.Buckets[0].DocCount != 1 {
  590. t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[0].DocCount)
  591. }
  592. if rangeAggRes.Buckets[1].DocCount != 1 {
  593. t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[1].DocCount)
  594. }
  595. if rangeAggRes.Buckets[2].DocCount != 1 {
  596. t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[2].DocCount)
  597. }
  598. // retweetsKeyedRange
  599. keyedRangeAggRes, found := agg.KeyedRange("retweetsKeyedRange")
  600. if !found {
  601. t.Errorf("expected %v; got: %v", true, found)
  602. }
  603. if keyedRangeAggRes == nil {
  604. t.Fatal("expected != nil; got: nil")
  605. }
  606. if len(keyedRangeAggRes.Buckets) != 3 {
  607. t.Fatalf("expected %d; got: %d", 3, len(keyedRangeAggRes.Buckets))
  608. }
  609. _, found = keyedRangeAggRes.Buckets["no-such-key"]
  610. if found {
  611. t.Fatalf("expected bucket to not be found; got: %v", found)
  612. }
  613. bucket, found := keyedRangeAggRes.Buckets["*-10.0"]
  614. if !found {
  615. t.Fatalf("expected bucket to be found; got: %v", found)
  616. }
  617. if bucket.DocCount != 1 {
  618. t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
  619. }
  620. bucket, found = keyedRangeAggRes.Buckets["10.0-100.0"]
  621. if !found {
  622. t.Fatalf("expected bucket to be found; got: %v", found)
  623. }
  624. if bucket.DocCount != 1 {
  625. t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
  626. }
  627. bucket, found = keyedRangeAggRes.Buckets["100.0-*"]
  628. if !found {
  629. t.Fatalf("expected bucket to be found; got: %v", found)
  630. }
  631. if bucket.DocCount != 1 {
  632. t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
  633. }
  634. // dateRange
  635. dateRangeRes, found := agg.DateRange("dateRange")
  636. if !found {
  637. t.Errorf("expected %v; got: %v", true, found)
  638. }
  639. if dateRangeRes == nil {
  640. t.Fatal("expected != nil; got: nil")
  641. }
  642. if dateRangeRes.Buckets[0].DocCount != 1 {
  643. t.Errorf("expected %d; got: %d", 1, dateRangeRes.Buckets[0].DocCount)
  644. }
  645. if dateRangeRes.Buckets[0].From != nil {
  646. t.Fatal("expected From to be nil")
  647. }
  648. if dateRangeRes.Buckets[0].To == nil {
  649. t.Fatal("expected To to be != nil")
  650. }
  651. if *dateRangeRes.Buckets[0].To != 1.325376e+12 {
  652. t.Errorf("expected %v; got: %v", 1.325376e+12, *dateRangeRes.Buckets[0].To)
  653. }
  654. if dateRangeRes.Buckets[0].ToAsString != "2012-01-01T00:00:00.000Z" {
  655. t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", dateRangeRes.Buckets[0].ToAsString)
  656. }
  657. if dateRangeRes.Buckets[1].DocCount != 2 {
  658. t.Errorf("expected %d; got: %d", 2, dateRangeRes.Buckets[1].DocCount)
  659. }
  660. if dateRangeRes.Buckets[1].From == nil {
  661. t.Fatal("expected From to be != nil")
  662. }
  663. if *dateRangeRes.Buckets[1].From != 1.325376e+12 {
  664. t.Errorf("expected From = %v; got: %v", 1.325376e+12, *dateRangeRes.Buckets[1].From)
  665. }
  666. if dateRangeRes.Buckets[1].FromAsString != "2012-01-01T00:00:00.000Z" {
  667. t.Errorf("expected FromAsString = %q; got: %q", "2012-01-01T00:00:00.000Z", dateRangeRes.Buckets[1].FromAsString)
  668. }
  669. if dateRangeRes.Buckets[1].To == nil {
  670. t.Fatal("expected To to be != nil")
  671. }
  672. if *dateRangeRes.Buckets[1].To != 1.3569984e+12 {
  673. t.Errorf("expected To = %v; got: %v", 1.3569984e+12, *dateRangeRes.Buckets[1].To)
  674. }
  675. if dateRangeRes.Buckets[1].ToAsString != "2013-01-01T00:00:00.000Z" {
  676. t.Errorf("expected ToAsString = %q; got: %q", "2013-01-01T00:00:00.000Z", dateRangeRes.Buckets[1].ToAsString)
  677. }
  678. if dateRangeRes.Buckets[2].DocCount != 0 {
  679. t.Errorf("expected %d; got: %d", 0, dateRangeRes.Buckets[2].DocCount)
  680. }
  681. if dateRangeRes.Buckets[2].To != nil {
  682. t.Fatal("expected To to be nil")
  683. }
  684. if dateRangeRes.Buckets[2].From == nil {
  685. t.Fatal("expected From to be != nil")
  686. }
  687. if *dateRangeRes.Buckets[2].From != 1.3569984e+12 {
  688. t.Errorf("expected %v; got: %v", 1.3569984e+12, *dateRangeRes.Buckets[2].From)
  689. }
  690. if dateRangeRes.Buckets[2].FromAsString != "2013-01-01T00:00:00.000Z" {
  691. t.Errorf("expected %q; got: %q", "2013-01-01T00:00:00.000Z", dateRangeRes.Buckets[2].FromAsString)
  692. }
  693. // missingTags
  694. missingRes, found := agg.Missing("missingTags")
  695. if !found {
  696. t.Errorf("expected %v; got: %v", true, found)
  697. }
  698. if missingRes == nil {
  699. t.Fatalf("expected != nil; got: nil")
  700. }
  701. if missingRes.DocCount != 0 {
  702. t.Errorf("expected searchResult.Aggregations[\"missingTags\"].DocCount = %v; got %v", 0, missingRes.DocCount)
  703. }
  704. // retweetsHisto
  705. histoRes, found := agg.Histogram("retweetsHisto")
  706. if !found {
  707. t.Errorf("expected %v; got: %v", true, found)
  708. }
  709. if histoRes == nil {
  710. t.Fatalf("expected != nil; got: nil")
  711. }
  712. if len(histoRes.Buckets) != 2 {
  713. t.Fatalf("expected %d; got: %d", 2, len(histoRes.Buckets))
  714. }
  715. if histoRes.Buckets[0].DocCount != 2 {
  716. t.Errorf("expected %d; got: %d", 2, histoRes.Buckets[0].DocCount)
  717. }
  718. if histoRes.Buckets[0].Key != 0.0 {
  719. t.Errorf("expected %v; got: %v", 0.0, histoRes.Buckets[0].Key)
  720. }
  721. if histoRes.Buckets[1].DocCount != 1 {
  722. t.Errorf("expected %d; got: %d", 1, histoRes.Buckets[1].DocCount)
  723. }
  724. if histoRes.Buckets[1].Key != 100.0 {
  725. t.Errorf("expected %v; got: %+v", 100.0, histoRes.Buckets[1].Key)
  726. }
  727. // dateHisto
  728. dateHistoRes, found := agg.DateHistogram("dateHisto")
  729. if !found {
  730. t.Errorf("expected %v; got: %v", true, found)
  731. }
  732. if dateHistoRes == nil {
  733. t.Fatalf("expected != nil; got: nil")
  734. }
  735. if len(dateHistoRes.Buckets) != 2 {
  736. t.Fatalf("expected %d; got: %d", 2, len(dateHistoRes.Buckets))
  737. }
  738. if dateHistoRes.Buckets[0].DocCount != 1 {
  739. t.Errorf("expected %d; got: %d", 1, dateHistoRes.Buckets[0].DocCount)
  740. }
  741. if dateHistoRes.Buckets[0].Key != 1.29384e+12 {
  742. t.Errorf("expected %v; got: %v", 1.29384e+12, dateHistoRes.Buckets[0].Key)
  743. }
  744. if dateHistoRes.Buckets[0].KeyAsString == nil {
  745. t.Fatalf("expected != nil; got: %v", dateHistoRes.Buckets[0].KeyAsString)
  746. }
  747. if *dateHistoRes.Buckets[0].KeyAsString != "2011-01-01T00:00:00.000Z" {
  748. t.Errorf("expected %q; got: %q", "2011-01-01T00:00:00.000Z", *dateHistoRes.Buckets[0].KeyAsString)
  749. }
  750. if dateHistoRes.Buckets[1].DocCount != 2 {
  751. t.Errorf("expected %d; got: %d", 2, dateHistoRes.Buckets[1].DocCount)
  752. }
  753. if dateHistoRes.Buckets[1].Key != 1.325376e+12 {
  754. t.Errorf("expected %v; got: %v", 1.325376e+12, dateHistoRes.Buckets[1].Key)
  755. }
  756. if dateHistoRes.Buckets[1].KeyAsString == nil {
  757. t.Fatalf("expected != nil; got: %v", dateHistoRes.Buckets[1].KeyAsString)
  758. }
  759. if *dateHistoRes.Buckets[1].KeyAsString != "2012-01-01T00:00:00.000Z" {
  760. t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", *dateHistoRes.Buckets[1].KeyAsString)
  761. }
  762. // dateHistoKeyed
  763. {
  764. res, found := agg.KeyedDateHistogram("dateHistoKeyed")
  765. if !found {
  766. t.Errorf("expected %v; got: %v", true, found)
  767. }
  768. if res == nil {
  769. t.Fatalf("expected != nil; got: nil")
  770. }
  771. if len(res.Buckets) != 2 {
  772. t.Fatalf("expected %d; got: %d", 2, len(res.Buckets))
  773. }
  774. bucket, ok := res.Buckets["2011-01-01T00:00:00.000Z"]
  775. if !ok || bucket == nil {
  776. t.Fatalf("expected to have bucket with key %q", "2011-01-01T00:00:00.000Z")
  777. }
  778. if bucket.DocCount != 1 {
  779. t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
  780. }
  781. if bucket.Key != 1.29384e+12 {
  782. t.Errorf("expected %v; got: %v", 1.29384e+12, bucket.Key)
  783. }
  784. if bucket.KeyAsString == nil {
  785. t.Fatalf("expected != nil; got: %v", bucket.KeyAsString)
  786. }
  787. if *bucket.KeyAsString != "2011-01-01T00:00:00.000Z" {
  788. t.Errorf("expected %q; got: %q", "2011-01-01T00:00:00.000Z", *bucket.KeyAsString)
  789. }
  790. bucket, ok = res.Buckets["2012-01-01T00:00:00.000Z"]
  791. if !ok || bucket == nil {
  792. t.Fatalf("expected to have bucket with key %q", "2012-01-01T00:00:00.000Z")
  793. }
  794. if bucket.DocCount != 2 {
  795. t.Errorf("expected %d; got: %d", 2, bucket.DocCount)
  796. }
  797. if bucket.Key != 1.325376e+12 {
  798. t.Errorf("expected %v; got: %v", 1.325376e+12, bucket.Key)
  799. }
  800. if bucket.KeyAsString == nil {
  801. t.Fatalf("expected != nil; got: %v", bucket.KeyAsString)
  802. }
  803. if *bucket.KeyAsString != "2012-01-01T00:00:00.000Z" {
  804. t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", *bucket.KeyAsString)
  805. }
  806. }
  807. // topHits
  808. topTags, found := agg.Terms("top-tags")
  809. if !found {
  810. t.Errorf("expected %v; got: %v", true, found)
  811. }
  812. if topTags == nil {
  813. t.Fatalf("expected != nil; got: nil")
  814. }
  815. if topTags.DocCountErrorUpperBound != 0 {
  816. t.Errorf("expected %v; got: %v", 0, topTags.DocCountErrorUpperBound)
  817. }
  818. if topTags.SumOfOtherDocCount != 1 {
  819. t.Errorf("expected %v; got: %v", 1, topTags.SumOfOtherDocCount)
  820. }
  821. if len(topTags.Buckets) != 3 {
  822. t.Fatalf("expected %d; got: %d", 3, len(topTags.Buckets))
  823. }
  824. if topTags.Buckets[0].DocCount != 2 {
  825. t.Errorf("expected %d; got: %d", 2, topTags.Buckets[0].DocCount)
  826. }
  827. if topTags.Buckets[0].Key != "golang" {
  828. t.Errorf("expected %v; got: %v", "golang", topTags.Buckets[0].Key)
  829. }
  830. topHits, found := topTags.Buckets[0].TopHits("top_tag_hits")
  831. if !found {
  832. t.Errorf("expected %v; got: %v", true, found)
  833. }
  834. if topHits == nil {
  835. t.Fatal("expected != nil; got: nil")
  836. }
  837. if topHits.Hits == nil {
  838. t.Fatalf("expected != nil; got: nil")
  839. }
  840. if topHits.Hits.TotalHits != 2 {
  841. t.Errorf("expected %d; got: %d", 2, topHits.Hits.TotalHits)
  842. }
  843. if topHits.Hits.Hits == nil {
  844. t.Fatalf("expected != nil; got: nil")
  845. }
  846. if len(topHits.Hits.Hits) != 2 {
  847. t.Fatalf("expected %d; got: %d", 2, len(topHits.Hits.Hits))
  848. }
  849. hit := topHits.Hits.Hits[0]
  850. if !found {
  851. t.Fatalf("expected %v; got: %v", true, found)
  852. }
  853. if hit == nil {
  854. t.Fatal("expected != nil; got: nil")
  855. }
  856. var tw tweet
  857. if err := json.Unmarshal(*hit.Source, &tw); err != nil {
  858. t.Fatalf("expected no error; got: %v", err)
  859. }
  860. if tw.Message != "Welcome to Golang and Elasticsearch." {
  861. t.Errorf("expected %q; got: %q", "Welcome to Golang and Elasticsearch.", tw.Message)
  862. }
  863. if topTags.Buckets[1].DocCount != 1 {
  864. t.Errorf("expected %d; got: %d", 1, topTags.Buckets[1].DocCount)
  865. }
  866. if topTags.Buckets[1].Key != "cycling" {
  867. t.Errorf("expected %v; got: %v", "cycling", topTags.Buckets[1].Key)
  868. }
  869. topHits, found = topTags.Buckets[1].TopHits("top_tag_hits")
  870. if !found {
  871. t.Errorf("expected %v; got: %v", true, found)
  872. }
  873. if topHits == nil {
  874. t.Fatal("expected != nil; got: nil")
  875. }
  876. if topHits.Hits == nil {
  877. t.Fatal("expected != nil; got nil")
  878. }
  879. if topHits.Hits.TotalHits != 1 {
  880. t.Errorf("expected %d; got: %d", 1, topHits.Hits.TotalHits)
  881. }
  882. if topTags.Buckets[2].DocCount != 1 {
  883. t.Errorf("expected %d; got: %d", 1, topTags.Buckets[2].DocCount)
  884. }
  885. if topTags.Buckets[2].Key != "elasticsearch" {
  886. t.Errorf("expected %v; got: %v", "elasticsearch", topTags.Buckets[2].Key)
  887. }
  888. topHits, found = topTags.Buckets[2].TopHits("top_tag_hits")
  889. if !found {
  890. t.Errorf("expected %v; got: %v", true, found)
  891. }
  892. if topHits == nil {
  893. t.Fatal("expected != nil; got: nil")
  894. }
  895. if topHits.Hits == nil {
  896. t.Fatal("expected != nil; got: nil")
  897. }
  898. if topHits.Hits.TotalHits != 1 {
  899. t.Errorf("expected %d; got: %d", 1, topHits.Hits.TotalHits)
  900. }
  901. // viewport via geo_bounds (1.3.0 has an error in that it doesn't output the aggregation name)
  902. geoBoundsRes, found := agg.GeoBounds("viewport")
  903. if !found {
  904. t.Errorf("expected %v; got: %v", true, found)
  905. }
  906. if geoBoundsRes == nil {
  907. t.Fatalf("expected != nil; got: nil")
  908. }
  909. // geohashed via geohash
  910. geoHashRes, found := agg.GeoHash("geohashed")
  911. if !found {
  912. t.Errorf("expected %v; got: %v", true, found)
  913. }
  914. if geoHashRes == nil {
  915. t.Fatalf("expected != nil; got: nil")
  916. }
  917. // geo_centroid
  918. geoCentroidRes, found := agg.GeoCentroid("centroid")
  919. if !found {
  920. t.Errorf("expected %v; got: %v", true, found)
  921. }
  922. if geoCentroidRes == nil {
  923. t.Fatalf("expected != nil; got: nil")
  924. }
  925. // Filters agg "countByUser" (unnamed)
  926. countByUserAggRes, found := agg.Filters("countByUser")
  927. if !found {
  928. t.Errorf("expected %v; got: %v", true, found)
  929. }
  930. if countByUserAggRes == nil {
  931. t.Fatalf("expected != nil; got: nil")
  932. }
  933. if len(countByUserAggRes.Buckets) != 2 {
  934. t.Fatalf("expected %d; got: %d", 2, len(countByUserAggRes.Buckets))
  935. }
  936. if len(countByUserAggRes.NamedBuckets) != 0 {
  937. t.Fatalf("expected %d; got: %d", 0, len(countByUserAggRes.NamedBuckets))
  938. }
  939. if countByUserAggRes.Buckets[0].DocCount != 2 {
  940. t.Errorf("expected %d; got: %d", 2, countByUserAggRes.Buckets[0].DocCount)
  941. }
  942. if countByUserAggRes.Buckets[1].DocCount != 1 {
  943. t.Errorf("expected %d; got: %d", 1, countByUserAggRes.Buckets[1].DocCount)
  944. }
  945. // Filters agg "countByUser2" (named)
  946. countByUser2AggRes, found := agg.Filters("countByUser2")
  947. if !found {
  948. t.Errorf("expected %v; got: %v", true, found)
  949. }
  950. if countByUser2AggRes == nil {
  951. t.Fatalf("expected != nil; got: nil")
  952. }
  953. if len(countByUser2AggRes.Buckets) != 0 {
  954. t.Fatalf("expected %d; got: %d", 0, len(countByUser2AggRes.Buckets))
  955. }
  956. if len(countByUser2AggRes.NamedBuckets) != 2 {
  957. t.Fatalf("expected %d; got: %d", 2, len(countByUser2AggRes.NamedBuckets))
  958. }
  959. b, found := countByUser2AggRes.NamedBuckets["olivere"]
  960. if !found {
  961. t.Fatalf("expected bucket %q; got: %v", "olivere", found)
  962. }
  963. if b == nil {
  964. t.Fatalf("expected bucket %q; got: %v", "olivere", b)
  965. }
  966. if b.DocCount != 2 {
  967. t.Errorf("expected %d; got: %d", 2, b.DocCount)
  968. }
  969. b, found = countByUser2AggRes.NamedBuckets["sandrae"]
  970. if !found {
  971. t.Fatalf("expected bucket %q; got: %v", "sandrae", found)
  972. }
  973. if b == nil {
  974. t.Fatalf("expected bucket %q; got: %v", "sandrae", b)
  975. }
  976. if b.DocCount != 1 {
  977. t.Errorf("expected %d; got: %d", 1, b.DocCount)
  978. }
  979. }
  980. // TestAggsMarshal ensures that marshaling aggregations back into a string
  981. // does not yield base64 encoded data. See https://github.com/olivere/elastic/issues/51
  982. // and https://groups.google.com/forum/#!topic/Golang-Nuts/38ShOlhxAYY for details.
  983. func TestAggsMarshal(t *testing.T) {
  984. client := setupTestClientAndCreateIndex(t)
  985. tweet1 := tweet{
  986. User: "olivere",
  987. Retweets: 108,
  988. Message: "Welcome to Golang and Elasticsearch.",
  989. Image: "http://golang.org/doc/gopher/gophercolor.png",
  990. Tags: []string{"golang", "elasticsearch"},
  991. Location: "48.1333,11.5667", // lat,lon
  992. Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
  993. }
  994. // Add all documents
  995. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  996. if err != nil {
  997. t.Fatal(err)
  998. }
  999. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  1000. if err != nil {
  1001. t.Fatal(err)
  1002. }
  1003. // Match all should return all documents
  1004. all := NewMatchAllQuery()
  1005. dhagg := NewDateHistogramAggregation().Field("created").Interval("year")
  1006. // Run query
  1007. builder := client.Search().Index(testIndexName).Query(all)
  1008. builder = builder.Aggregation("dhagg", dhagg)
  1009. searchResult, err := builder.Do(context.TODO())
  1010. if err != nil {
  1011. t.Fatal(err)
  1012. }
  1013. if searchResult.TotalHits() != 1 {
  1014. t.Errorf("expected Hits.TotalHits = %d; got: %d", 1, searchResult.TotalHits())
  1015. }
  1016. if _, found := searchResult.Aggregations["dhagg"]; !found {
  1017. t.Fatalf("expected aggregation %q", "dhagg")
  1018. }
  1019. buf, err := json.Marshal(searchResult)
  1020. if err != nil {
  1021. t.Fatal(err)
  1022. }
  1023. s := string(buf)
  1024. if i := strings.Index(s, `{"dhagg":{"buckets":[{"key_as_string":"2012-01-01`); i < 0 {
  1025. t.Errorf("expected to serialize aggregation into string; got: %v", s)
  1026. }
  1027. }
  1028. func TestAggsMetricsMin(t *testing.T) {
  1029. s := `{
  1030. "min_price": {
  1031. "value": 10
  1032. }
  1033. }`
  1034. aggs := new(Aggregations)
  1035. err := json.Unmarshal([]byte(s), &aggs)
  1036. if err != nil {
  1037. t.Fatalf("expected no error decoding; got: %v", err)
  1038. }
  1039. agg, found := aggs.Min("min_price")
  1040. if !found {
  1041. t.Fatalf("expected aggregation to be found; got: %v", found)
  1042. }
  1043. if agg == nil {
  1044. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1045. }
  1046. if agg.Value == nil {
  1047. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  1048. }
  1049. if *agg.Value != float64(10) {
  1050. t.Fatalf("expected aggregation value = %v; got: %v", float64(10), *agg.Value)
  1051. }
  1052. }
  1053. func TestAggsMetricsMax(t *testing.T) {
  1054. s := `{
  1055. "max_price": {
  1056. "value": 35
  1057. }
  1058. }`
  1059. aggs := new(Aggregations)
  1060. err := json.Unmarshal([]byte(s), &aggs)
  1061. if err != nil {
  1062. t.Fatalf("expected no error decoding; got: %v", err)
  1063. }
  1064. agg, found := aggs.Max("max_price")
  1065. if !found {
  1066. t.Fatalf("expected aggregation to be found; got: %v", found)
  1067. }
  1068. if agg == nil {
  1069. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1070. }
  1071. if agg.Value == nil {
  1072. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  1073. }
  1074. if *agg.Value != float64(35) {
  1075. t.Fatalf("expected aggregation value = %v; got: %v", float64(35), *agg.Value)
  1076. }
  1077. }
  1078. func TestAggsMetricsSum(t *testing.T) {
  1079. s := `{
  1080. "intraday_return": {
  1081. "value": 2.18
  1082. }
  1083. }`
  1084. aggs := new(Aggregations)
  1085. err := json.Unmarshal([]byte(s), &aggs)
  1086. if err != nil {
  1087. t.Fatalf("expected no error decoding; got: %v", err)
  1088. }
  1089. agg, found := aggs.Sum("intraday_return")
  1090. if !found {
  1091. t.Fatalf("expected aggregation to be found; got: %v", found)
  1092. }
  1093. if agg == nil {
  1094. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1095. }
  1096. if agg.Value == nil {
  1097. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  1098. }
  1099. if *agg.Value != float64(2.18) {
  1100. t.Fatalf("expected aggregation value = %v; got: %v", float64(2.18), *agg.Value)
  1101. }
  1102. }
  1103. func TestAggsMetricsAvg(t *testing.T) {
  1104. s := `{
  1105. "avg_grade": {
  1106. "value": 75
  1107. }
  1108. }`
  1109. aggs := new(Aggregations)
  1110. err := json.Unmarshal([]byte(s), &aggs)
  1111. if err != nil {
  1112. t.Fatalf("expected no error decoding; got: %v", err)
  1113. }
  1114. agg, found := aggs.Avg("avg_grade")
  1115. if !found {
  1116. t.Fatalf("expected aggregation to be found; got: %v", found)
  1117. }
  1118. if agg == nil {
  1119. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1120. }
  1121. if agg.Value == nil {
  1122. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  1123. }
  1124. if *agg.Value != float64(75) {
  1125. t.Fatalf("expected aggregation value = %v; got: %v", float64(75), *agg.Value)
  1126. }
  1127. }
  1128. func TestAggsMetricsValueCount(t *testing.T) {
  1129. s := `{
  1130. "grades_count": {
  1131. "value": 10
  1132. }
  1133. }`
  1134. aggs := new(Aggregations)
  1135. err := json.Unmarshal([]byte(s), &aggs)
  1136. if err != nil {
  1137. t.Fatalf("expected no error decoding; got: %v", err)
  1138. }
  1139. agg, found := aggs.ValueCount("grades_count")
  1140. if !found {
  1141. t.Fatalf("expected aggregation to be found; got: %v", found)
  1142. }
  1143. if agg == nil {
  1144. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1145. }
  1146. if agg.Value == nil {
  1147. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  1148. }
  1149. if *agg.Value != float64(10) {
  1150. t.Fatalf("expected aggregation value = %v; got: %v", float64(10), *agg.Value)
  1151. }
  1152. }
  1153. func TestAggsMetricsCardinality(t *testing.T) {
  1154. s := `{
  1155. "author_count": {
  1156. "value": 12
  1157. }
  1158. }`
  1159. aggs := new(Aggregations)
  1160. err := json.Unmarshal([]byte(s), &aggs)
  1161. if err != nil {
  1162. t.Fatalf("expected no error decoding; got: %v", err)
  1163. }
  1164. agg, found := aggs.Cardinality("author_count")
  1165. if !found {
  1166. t.Fatalf("expected aggregation to be found; got: %v", found)
  1167. }
  1168. if agg == nil {
  1169. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1170. }
  1171. if agg.Value == nil {
  1172. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  1173. }
  1174. if *agg.Value != float64(12) {
  1175. t.Fatalf("expected aggregation value = %v; got: %v", float64(12), *agg.Value)
  1176. }
  1177. }
  1178. func TestAggsMetricsStats(t *testing.T) {
  1179. s := `{
  1180. "grades_stats": {
  1181. "count": 6,
  1182. "min": 60,
  1183. "max": 98,
  1184. "avg": 78.5,
  1185. "sum": 471
  1186. }
  1187. }`
  1188. aggs := new(Aggregations)
  1189. err := json.Unmarshal([]byte(s), &aggs)
  1190. if err != nil {
  1191. t.Fatalf("expected no error decoding; got: %v", err)
  1192. }
  1193. agg, found := aggs.Stats("grades_stats")
  1194. if !found {
  1195. t.Fatalf("expected aggregation to be found; got: %v", found)
  1196. }
  1197. if agg == nil {
  1198. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1199. }
  1200. if agg.Count != int64(6) {
  1201. t.Fatalf("expected aggregation Count = %v; got: %v", int64(6), agg.Count)
  1202. }
  1203. if agg.Min == nil {
  1204. t.Fatalf("expected aggregation Min != nil; got: %v", agg.Min)
  1205. }
  1206. if *agg.Min != float64(60) {
  1207. t.Fatalf("expected aggregation Min = %v; got: %v", float64(60), *agg.Min)
  1208. }
  1209. if agg.Max == nil {
  1210. t.Fatalf("expected aggregation Max != nil; got: %v", agg.Max)
  1211. }
  1212. if *agg.Max != float64(98) {
  1213. t.Fatalf("expected aggregation Max = %v; got: %v", float64(98), *agg.Max)
  1214. }
  1215. if agg.Avg == nil {
  1216. t.Fatalf("expected aggregation Avg != nil; got: %v", agg.Avg)
  1217. }
  1218. if *agg.Avg != float64(78.5) {
  1219. t.Fatalf("expected aggregation Avg = %v; got: %v", float64(78.5), *agg.Avg)
  1220. }
  1221. if agg.Sum == nil {
  1222. t.Fatalf("expected aggregation Sum != nil; got: %v", agg.Sum)
  1223. }
  1224. if *agg.Sum != float64(471) {
  1225. t.Fatalf("expected aggregation Sum = %v; got: %v", float64(471), *agg.Sum)
  1226. }
  1227. }
  1228. func TestAggsMetricsExtendedStats(t *testing.T) {
  1229. s := `{
  1230. "grades_stats": {
  1231. "count": 6,
  1232. "min": 72,
  1233. "max": 117.6,
  1234. "avg": 94.2,
  1235. "sum": 565.2,
  1236. "sum_of_squares": 54551.51999999999,
  1237. "variance": 218.2799999999976,
  1238. "std_deviation": 14.774302013969987
  1239. }
  1240. }`
  1241. aggs := new(Aggregations)
  1242. err := json.Unmarshal([]byte(s), &aggs)
  1243. if err != nil {
  1244. t.Fatalf("expected no error decoding; got: %v", err)
  1245. }
  1246. agg, found := aggs.ExtendedStats("grades_stats")
  1247. if !found {
  1248. t.Fatalf("expected aggregation to be found; got: %v", found)
  1249. }
  1250. if agg == nil {
  1251. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1252. }
  1253. if agg.Count != int64(6) {
  1254. t.Fatalf("expected aggregation Count = %v; got: %v", int64(6), agg.Count)
  1255. }
  1256. if agg.Min == nil {
  1257. t.Fatalf("expected aggregation Min != nil; got: %v", agg.Min)
  1258. }
  1259. if *agg.Min != float64(72) {
  1260. t.Fatalf("expected aggregation Min = %v; got: %v", float64(72), *agg.Min)
  1261. }
  1262. if agg.Max == nil {
  1263. t.Fatalf("expected aggregation Max != nil; got: %v", agg.Max)
  1264. }
  1265. if *agg.Max != float64(117.6) {
  1266. t.Fatalf("expected aggregation Max = %v; got: %v", float64(117.6), *agg.Max)
  1267. }
  1268. if agg.Avg == nil {
  1269. t.Fatalf("expected aggregation Avg != nil; got: %v", agg.Avg)
  1270. }
  1271. if *agg.Avg != float64(94.2) {
  1272. t.Fatalf("expected aggregation Avg = %v; got: %v", float64(94.2), *agg.Avg)
  1273. }
  1274. if agg.Sum == nil {
  1275. t.Fatalf("expected aggregation Sum != nil; got: %v", agg.Sum)
  1276. }
  1277. if *agg.Sum != float64(565.2) {
  1278. t.Fatalf("expected aggregation Sum = %v; got: %v", float64(565.2), *agg.Sum)
  1279. }
  1280. if agg.SumOfSquares == nil {
  1281. t.Fatalf("expected aggregation sum_of_squares != nil; got: %v", agg.SumOfSquares)
  1282. }
  1283. if *agg.SumOfSquares != float64(54551.51999999999) {
  1284. t.Fatalf("expected aggregation sum_of_squares = %v; got: %v", float64(54551.51999999999), *agg.SumOfSquares)
  1285. }
  1286. if agg.Variance == nil {
  1287. t.Fatalf("expected aggregation Variance != nil; got: %v", agg.Variance)
  1288. }
  1289. if *agg.Variance != float64(218.2799999999976) {
  1290. t.Fatalf("expected aggregation Variance = %v; got: %v", float64(218.2799999999976), *agg.Variance)
  1291. }
  1292. if agg.StdDeviation == nil {
  1293. t.Fatalf("expected aggregation StdDeviation != nil; got: %v", agg.StdDeviation)
  1294. }
  1295. if *agg.StdDeviation != float64(14.774302013969987) {
  1296. t.Fatalf("expected aggregation StdDeviation = %v; got: %v", float64(14.774302013969987), *agg.StdDeviation)
  1297. }
  1298. }
  1299. func TestAggsMatrixStats(t *testing.T) {
  1300. s := `{
  1301. "matrixstats": {
  1302. "fields": [{
  1303. "name": "income",
  1304. "count": 50,
  1305. "mean": 51985.1,
  1306. "variance": 7.383377037755103E7,
  1307. "skewness": 0.5595114003506483,
  1308. "kurtosis": 2.5692365287787124,
  1309. "covariance": {
  1310. "income": 7.383377037755103E7,
  1311. "poverty": -21093.65836734694
  1312. },
  1313. "correlation": {
  1314. "income": 1.0,
  1315. "poverty": -0.8352655256272504
  1316. }
  1317. }, {
  1318. "name": "poverty",
  1319. "count": 51,
  1320. "mean": 12.732000000000001,
  1321. "variance": 8.637730612244896,
  1322. "skewness": 0.4516049811903419,
  1323. "kurtosis": 2.8615929677997767,
  1324. "covariance": {
  1325. "income": -21093.65836734694,
  1326. "poverty": 8.637730612244896
  1327. },
  1328. "correlation": {
  1329. "income": -0.8352655256272504,
  1330. "poverty": 1.0
  1331. }
  1332. }]
  1333. }
  1334. }`
  1335. aggs := new(Aggregations)
  1336. err := json.Unmarshal([]byte(s), &aggs)
  1337. if err != nil {
  1338. t.Fatalf("expected no error decoding; got: %v", err)
  1339. }
  1340. agg, found := aggs.MatrixStats("matrixstats")
  1341. if !found {
  1342. t.Fatalf("expected aggregation to be found; got: %v", found)
  1343. }
  1344. if agg == nil {
  1345. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1346. }
  1347. if want, got := 2, len(agg.Fields); want != got {
  1348. t.Fatalf("expected aggregaton len(Fields) = %v; got: %v", want, got)
  1349. }
  1350. field := agg.Fields[0]
  1351. if want, got := "income", field.Name; want != got {
  1352. t.Fatalf("expected aggregation field name == %q; got: %q", want, got)
  1353. }
  1354. if want, got := int64(50), field.Count; want != got {
  1355. t.Fatalf("expected aggregation field count == %v; got: %v", want, got)
  1356. }
  1357. if want, got := 51985.1, field.Mean; want != got {
  1358. t.Fatalf("expected aggregation field mean == %v; got: %v", want, got)
  1359. }
  1360. if want, got := 7.383377037755103e7, field.Variance; want != got {
  1361. t.Fatalf("expected aggregation field variance == %v; got: %v", want, got)
  1362. }
  1363. if want, got := 0.5595114003506483, field.Skewness; want != got {
  1364. t.Fatalf("expected aggregation field skewness == %v; got: %v", want, got)
  1365. }
  1366. if want, got := 2.5692365287787124, field.Kurtosis; want != got {
  1367. t.Fatalf("expected aggregation field kurtosis == %v; got: %v", want, got)
  1368. }
  1369. if field.Covariance == nil {
  1370. t.Fatalf("expected aggregation field covariance != nil; got: %v", nil)
  1371. }
  1372. if want, got := 7.383377037755103e7, field.Covariance["income"]; want != got {
  1373. t.Fatalf("expected aggregation field covariance == %v; got: %v", want, got)
  1374. }
  1375. if want, got := -21093.65836734694, field.Covariance["poverty"]; want != got {
  1376. t.Fatalf("expected aggregation field covariance == %v; got: %v", want, got)
  1377. }
  1378. if field.Correlation == nil {
  1379. t.Fatalf("expected aggregation field correlation != nil; got: %v", nil)
  1380. }
  1381. if want, got := 1.0, field.Correlation["income"]; want != got {
  1382. t.Fatalf("expected aggregation field correlation == %v; got: %v", want, got)
  1383. }
  1384. if want, got := -0.8352655256272504, field.Correlation["poverty"]; want != got {
  1385. t.Fatalf("expected aggregation field correlation == %v; got: %v", want, got)
  1386. }
  1387. field = agg.Fields[1]
  1388. if want, got := "poverty", field.Name; want != got {
  1389. t.Fatalf("expected aggregation field name == %q; got: %q", want, got)
  1390. }
  1391. if want, got := int64(51), field.Count; want != got {
  1392. t.Fatalf("expected aggregation field count == %v; got: %v", want, got)
  1393. }
  1394. }
  1395. func TestAggsMetricsPercentiles(t *testing.T) {
  1396. s := `{
  1397. "load_time_outlier": {
  1398. "values" : {
  1399. "1.0": 15,
  1400. "5.0": 20,
  1401. "25.0": 23,
  1402. "50.0": 25,
  1403. "75.0": 29,
  1404. "95.0": 60,
  1405. "99.0": 150
  1406. }
  1407. }
  1408. }`
  1409. aggs := new(Aggregations)
  1410. err := json.Unmarshal([]byte(s), &aggs)
  1411. if err != nil {
  1412. t.Fatalf("expected no error decoding; got: %v", err)
  1413. }
  1414. agg, found := aggs.Percentiles("load_time_outlier")
  1415. if !found {
  1416. t.Fatalf("expected aggregation to be found; got: %v", found)
  1417. }
  1418. if agg == nil {
  1419. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1420. }
  1421. if agg.Values == nil {
  1422. t.Fatalf("expected aggregation Values != nil; got: %v", agg.Values)
  1423. }
  1424. if len(agg.Values) != 7 {
  1425. t.Fatalf("expected %d aggregation Values; got: %d", 7, len(agg.Values))
  1426. }
  1427. if agg.Values["1.0"] != float64(15) {
  1428. t.Errorf("expected aggregation value for \"1.0\" = %v; got: %v", float64(15), agg.Values["1.0"])
  1429. }
  1430. if agg.Values["5.0"] != float64(20) {
  1431. t.Errorf("expected aggregation value for \"5.0\" = %v; got: %v", float64(20), agg.Values["5.0"])
  1432. }
  1433. if agg.Values["25.0"] != float64(23) {
  1434. t.Errorf("expected aggregation value for \"25.0\" = %v; got: %v", float64(23), agg.Values["25.0"])
  1435. }
  1436. if agg.Values["50.0"] != float64(25) {
  1437. t.Errorf("expected aggregation value for \"50.0\" = %v; got: %v", float64(25), agg.Values["50.0"])
  1438. }
  1439. if agg.Values["75.0"] != float64(29) {
  1440. t.Errorf("expected aggregation value for \"75.0\" = %v; got: %v", float64(29), agg.Values["75.0"])
  1441. }
  1442. if agg.Values["95.0"] != float64(60) {
  1443. t.Errorf("expected aggregation value for \"95.0\" = %v; got: %v", float64(60), agg.Values["95.0"])
  1444. }
  1445. if agg.Values["99.0"] != float64(150) {
  1446. t.Errorf("expected aggregation value for \"99.0\" = %v; got: %v", float64(150), agg.Values["99.0"])
  1447. }
  1448. }
  1449. func TestAggsMetricsPercentileRanks(t *testing.T) {
  1450. s := `{
  1451. "load_time_outlier": {
  1452. "values" : {
  1453. "15": 92,
  1454. "30": 100
  1455. }
  1456. }
  1457. }`
  1458. aggs := new(Aggregations)
  1459. err := json.Unmarshal([]byte(s), &aggs)
  1460. if err != nil {
  1461. t.Fatalf("expected no error decoding; got: %v", err)
  1462. }
  1463. agg, found := aggs.PercentileRanks("load_time_outlier")
  1464. if !found {
  1465. t.Fatalf("expected aggregation to be found; got: %v", found)
  1466. }
  1467. if agg == nil {
  1468. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1469. }
  1470. if agg.Values == nil {
  1471. t.Fatalf("expected aggregation Values != nil; got: %v", agg.Values)
  1472. }
  1473. if len(agg.Values) != 2 {
  1474. t.Fatalf("expected %d aggregation Values; got: %d", 7, len(agg.Values))
  1475. }
  1476. if agg.Values["15"] != float64(92) {
  1477. t.Errorf("expected aggregation value for \"15\" = %v; got: %v", float64(92), agg.Values["15"])
  1478. }
  1479. if agg.Values["30"] != float64(100) {
  1480. t.Errorf("expected aggregation value for \"30\" = %v; got: %v", float64(100), agg.Values["30"])
  1481. }
  1482. }
  1483. func TestAggsMetricsTopHits(t *testing.T) {
  1484. s := `{
  1485. "top-tags": {
  1486. "buckets": [
  1487. {
  1488. "key": "windows-7",
  1489. "doc_count": 25365,
  1490. "top_tags_hits": {
  1491. "hits": {
  1492. "total": 25365,
  1493. "max_score": 1,
  1494. "hits": [
  1495. {
  1496. "_index": "stack",
  1497. "_type": "question",
  1498. "_id": "602679",
  1499. "_score": 1,
  1500. "_source": {
  1501. "title": "Windows port opening"
  1502. },
  1503. "sort": [
  1504. 1370143231177
  1505. ]
  1506. }
  1507. ]
  1508. }
  1509. }
  1510. },
  1511. {
  1512. "key": "linux",
  1513. "doc_count": 18342,
  1514. "top_tags_hits": {
  1515. "hits": {
  1516. "total": 18342,
  1517. "max_score": 1,
  1518. "hits": [
  1519. {
  1520. "_index": "stack",
  1521. "_type": "question",
  1522. "_id": "602672",
  1523. "_score": 1,
  1524. "_source": {
  1525. "title": "Ubuntu RFID Screensaver lock-unlock"
  1526. },
  1527. "sort": [
  1528. 1370143379747
  1529. ]
  1530. }
  1531. ]
  1532. }
  1533. }
  1534. },
  1535. {
  1536. "key": "windows",
  1537. "doc_count": 18119,
  1538. "top_tags_hits": {
  1539. "hits": {
  1540. "total": 18119,
  1541. "max_score": 1,
  1542. "hits": [
  1543. {
  1544. "_index": "stack",
  1545. "_type": "question",
  1546. "_id": "602678",
  1547. "_score": 1,
  1548. "_source": {
  1549. "title": "If I change my computers date / time, what could be affected?"
  1550. },
  1551. "sort": [
  1552. 1370142868283
  1553. ]
  1554. }
  1555. ]
  1556. }
  1557. }
  1558. }
  1559. ]
  1560. }
  1561. }`
  1562. aggs := new(Aggregations)
  1563. err := json.Unmarshal([]byte(s), &aggs)
  1564. if err != nil {
  1565. t.Fatalf("expected no error decoding; got: %v", err)
  1566. }
  1567. agg, found := aggs.Terms("top-tags")
  1568. if !found {
  1569. t.Fatalf("expected aggregation to be found; got: %v", found)
  1570. }
  1571. if agg == nil {
  1572. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1573. }
  1574. if agg.Buckets == nil {
  1575. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  1576. }
  1577. if len(agg.Buckets) != 3 {
  1578. t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
  1579. }
  1580. if agg.Buckets[0].Key != "windows-7" {
  1581. t.Errorf("expected bucket key = %q; got: %q", "windows-7", agg.Buckets[0].Key)
  1582. }
  1583. if agg.Buckets[1].Key != "linux" {
  1584. t.Errorf("expected bucket key = %q; got: %q", "linux", agg.Buckets[1].Key)
  1585. }
  1586. if agg.Buckets[2].Key != "windows" {
  1587. t.Errorf("expected bucket key = %q; got: %q", "windows", agg.Buckets[2].Key)
  1588. }
  1589. // Sub-aggregation of top-hits
  1590. subAgg, found := agg.Buckets[0].TopHits("top_tags_hits")
  1591. if !found {
  1592. t.Fatalf("expected sub aggregation to be found; got: %v", found)
  1593. }
  1594. if subAgg == nil {
  1595. t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
  1596. }
  1597. if subAgg.Hits == nil {
  1598. t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
  1599. }
  1600. if subAgg.Hits.TotalHits != 25365 {
  1601. t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 25365, subAgg.Hits.TotalHits)
  1602. }
  1603. if subAgg.Hits.MaxScore == nil {
  1604. t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
  1605. }
  1606. if *subAgg.Hits.MaxScore != float64(1.0) {
  1607. t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
  1608. }
  1609. subAgg, found = agg.Buckets[1].TopHits("top_tags_hits")
  1610. if !found {
  1611. t.Fatalf("expected sub aggregation to be found; got: %v", found)
  1612. }
  1613. if subAgg == nil {
  1614. t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
  1615. }
  1616. if subAgg.Hits == nil {
  1617. t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
  1618. }
  1619. if subAgg.Hits.TotalHits != 18342 {
  1620. t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 18342, subAgg.Hits.TotalHits)
  1621. }
  1622. if subAgg.Hits.MaxScore == nil {
  1623. t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
  1624. }
  1625. if *subAgg.Hits.MaxScore != float64(1.0) {
  1626. t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
  1627. }
  1628. subAgg, found = agg.Buckets[2].TopHits("top_tags_hits")
  1629. if !found {
  1630. t.Fatalf("expected sub aggregation to be found; got: %v", found)
  1631. }
  1632. if subAgg == nil {
  1633. t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
  1634. }
  1635. if subAgg.Hits == nil {
  1636. t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
  1637. }
  1638. if subAgg.Hits.TotalHits != 18119 {
  1639. t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 18119, subAgg.Hits.TotalHits)
  1640. }
  1641. if subAgg.Hits.MaxScore == nil {
  1642. t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
  1643. }
  1644. if *subAgg.Hits.MaxScore != float64(1.0) {
  1645. t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
  1646. }
  1647. }
  1648. func TestAggsBucketGlobal(t *testing.T) {
  1649. s := `{
  1650. "all_products" : {
  1651. "doc_count" : 100,
  1652. "avg_price" : {
  1653. "value" : 56.3
  1654. }
  1655. }
  1656. }`
  1657. aggs := new(Aggregations)
  1658. err := json.Unmarshal([]byte(s), &aggs)
  1659. if err != nil {
  1660. t.Fatalf("expected no error decoding; got: %v", err)
  1661. }
  1662. agg, found := aggs.Global("all_products")
  1663. if !found {
  1664. t.Fatalf("expected aggregation to be found; got: %v", found)
  1665. }
  1666. if agg == nil {
  1667. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1668. }
  1669. if agg.DocCount != 100 {
  1670. t.Fatalf("expected aggregation DocCount = %d; got: %d", 100, agg.DocCount)
  1671. }
  1672. // Sub-aggregation
  1673. subAgg, found := agg.Avg("avg_price")
  1674. if !found {
  1675. t.Fatalf("expected sub-aggregation to be found; got: %v", found)
  1676. }
  1677. if subAgg == nil {
  1678. t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
  1679. }
  1680. if subAgg.Value == nil {
  1681. t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
  1682. }
  1683. if *subAgg.Value != float64(56.3) {
  1684. t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(56.3), *subAgg.Value)
  1685. }
  1686. }
  1687. func TestAggsBucketFilter(t *testing.T) {
  1688. s := `{
  1689. "in_stock_products" : {
  1690. "doc_count" : 100,
  1691. "avg_price" : { "value" : 56.3 }
  1692. }
  1693. }`
  1694. aggs := new(Aggregations)
  1695. err := json.Unmarshal([]byte(s), &aggs)
  1696. if err != nil {
  1697. t.Fatalf("expected no error decoding; got: %v", err)
  1698. }
  1699. agg, found := aggs.Filter("in_stock_products")
  1700. if !found {
  1701. t.Fatalf("expected aggregation to be found; got: %v", found)
  1702. }
  1703. if agg == nil {
  1704. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1705. }
  1706. if agg.DocCount != 100 {
  1707. t.Fatalf("expected aggregation DocCount = %d; got: %d", 100, agg.DocCount)
  1708. }
  1709. // Sub-aggregation
  1710. subAgg, found := agg.Avg("avg_price")
  1711. if !found {
  1712. t.Fatalf("expected sub-aggregation to be found; got: %v", found)
  1713. }
  1714. if subAgg == nil {
  1715. t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
  1716. }
  1717. if subAgg.Value == nil {
  1718. t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
  1719. }
  1720. if *subAgg.Value != float64(56.3) {
  1721. t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(56.3), *subAgg.Value)
  1722. }
  1723. }
  1724. func TestAggsBucketFiltersWithBuckets(t *testing.T) {
  1725. s := `{
  1726. "messages" : {
  1727. "buckets" : [
  1728. {
  1729. "doc_count" : 34,
  1730. "monthly" : {
  1731. "buckets" : []
  1732. }
  1733. },
  1734. {
  1735. "doc_count" : 439,
  1736. "monthly" : {
  1737. "buckets" : []
  1738. }
  1739. }
  1740. ]
  1741. }
  1742. }`
  1743. aggs := new(Aggregations)
  1744. err := json.Unmarshal([]byte(s), &aggs)
  1745. if err != nil {
  1746. t.Fatalf("expected no error decoding; got: %v", err)
  1747. }
  1748. agg, found := aggs.Filters("messages")
  1749. if !found {
  1750. t.Fatalf("expected aggregation to be found; got: %v", found)
  1751. }
  1752. if agg == nil {
  1753. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1754. }
  1755. if agg.Buckets == nil {
  1756. t.Fatalf("expected aggregation buckets != %v; got: %v", nil, agg.Buckets)
  1757. }
  1758. if len(agg.Buckets) != 2 {
  1759. t.Fatalf("expected %d buckets; got: %d", 2, len(agg.Buckets))
  1760. }
  1761. if agg.Buckets[0].DocCount != 34 {
  1762. t.Fatalf("expected DocCount = %d; got: %d", 34, agg.Buckets[0].DocCount)
  1763. }
  1764. subAgg, found := agg.Buckets[0].Histogram("monthly")
  1765. if !found {
  1766. t.Fatalf("expected sub aggregation to be found; got: %v", found)
  1767. }
  1768. if subAgg == nil {
  1769. t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
  1770. }
  1771. if agg.Buckets[1].DocCount != 439 {
  1772. t.Fatalf("expected DocCount = %d; got: %d", 439, agg.Buckets[1].DocCount)
  1773. }
  1774. subAgg, found = agg.Buckets[1].Histogram("monthly")
  1775. if !found {
  1776. t.Fatalf("expected sub aggregation to be found; got: %v", found)
  1777. }
  1778. if subAgg == nil {
  1779. t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
  1780. }
  1781. }
  1782. func TestAggsBucketFiltersWithNamedBuckets(t *testing.T) {
  1783. s := `{
  1784. "messages" : {
  1785. "buckets" : {
  1786. "errors" : {
  1787. "doc_count" : 34,
  1788. "monthly" : {
  1789. "buckets" : []
  1790. }
  1791. },
  1792. "warnings" : {
  1793. "doc_count" : 439,
  1794. "monthly" : {
  1795. "buckets" : []
  1796. }
  1797. }
  1798. }
  1799. }
  1800. }`
  1801. aggs := new(Aggregations)
  1802. err := json.Unmarshal([]byte(s), &aggs)
  1803. if err != nil {
  1804. t.Fatalf("expected no error decoding; got: %v", err)
  1805. }
  1806. agg, found := aggs.Filters("messages")
  1807. if !found {
  1808. t.Fatalf("expected aggregation to be found; got: %v", found)
  1809. }
  1810. if agg == nil {
  1811. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1812. }
  1813. if agg.NamedBuckets == nil {
  1814. t.Fatalf("expected aggregation buckets != %v; got: %v", nil, agg.NamedBuckets)
  1815. }
  1816. if len(agg.NamedBuckets) != 2 {
  1817. t.Fatalf("expected %d buckets; got: %d", 2, len(agg.NamedBuckets))
  1818. }
  1819. if agg.NamedBuckets["errors"].DocCount != 34 {
  1820. t.Fatalf("expected DocCount = %d; got: %d", 34, agg.NamedBuckets["errors"].DocCount)
  1821. }
  1822. subAgg, found := agg.NamedBuckets["errors"].Histogram("monthly")
  1823. if !found {
  1824. t.Fatalf("expected sub aggregation to be found; got: %v", found)
  1825. }
  1826. if subAgg == nil {
  1827. t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
  1828. }
  1829. if agg.NamedBuckets["warnings"].DocCount != 439 {
  1830. t.Fatalf("expected DocCount = %d; got: %d", 439, agg.NamedBuckets["warnings"].DocCount)
  1831. }
  1832. subAgg, found = agg.NamedBuckets["warnings"].Histogram("monthly")
  1833. if !found {
  1834. t.Fatalf("expected sub aggregation to be found; got: %v", found)
  1835. }
  1836. if subAgg == nil {
  1837. t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
  1838. }
  1839. }
  1840. func TestAggsBucketMissing(t *testing.T) {
  1841. s := `{
  1842. "products_without_a_price" : {
  1843. "doc_count" : 10
  1844. }
  1845. }`
  1846. aggs := new(Aggregations)
  1847. err := json.Unmarshal([]byte(s), &aggs)
  1848. if err != nil {
  1849. t.Fatalf("expected no error decoding; got: %v", err)
  1850. }
  1851. agg, found := aggs.Missing("products_without_a_price")
  1852. if !found {
  1853. t.Fatalf("expected aggregation to be found; got: %v", found)
  1854. }
  1855. if agg == nil {
  1856. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1857. }
  1858. if agg.DocCount != 10 {
  1859. t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
  1860. }
  1861. }
  1862. func TestAggsBucketNested(t *testing.T) {
  1863. s := `{
  1864. "resellers": {
  1865. "min_price": {
  1866. "value" : 350
  1867. }
  1868. }
  1869. }`
  1870. aggs := new(Aggregations)
  1871. err := json.Unmarshal([]byte(s), &aggs)
  1872. if err != nil {
  1873. t.Fatalf("expected no error decoding; got: %v", err)
  1874. }
  1875. agg, found := aggs.Nested("resellers")
  1876. if !found {
  1877. t.Fatalf("expected aggregation to be found; got: %v", found)
  1878. }
  1879. if agg == nil {
  1880. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1881. }
  1882. if agg.DocCount != 0 {
  1883. t.Fatalf("expected aggregation DocCount = %d; got: %d", 0, agg.DocCount)
  1884. }
  1885. // Sub-aggregation
  1886. subAgg, found := agg.Avg("min_price")
  1887. if !found {
  1888. t.Fatalf("expected sub-aggregation to be found; got: %v", found)
  1889. }
  1890. if subAgg == nil {
  1891. t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
  1892. }
  1893. if subAgg.Value == nil {
  1894. t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
  1895. }
  1896. if *subAgg.Value != float64(350) {
  1897. t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(350), *subAgg.Value)
  1898. }
  1899. }
  1900. func TestAggsBucketReverseNested(t *testing.T) {
  1901. s := `{
  1902. "comment_to_issue": {
  1903. "doc_count" : 10
  1904. }
  1905. }`
  1906. aggs := new(Aggregations)
  1907. err := json.Unmarshal([]byte(s), &aggs)
  1908. if err != nil {
  1909. t.Fatalf("expected no error decoding; got: %v", err)
  1910. }
  1911. agg, found := aggs.ReverseNested("comment_to_issue")
  1912. if !found {
  1913. t.Fatalf("expected aggregation to be found; got: %v", found)
  1914. }
  1915. if agg == nil {
  1916. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1917. }
  1918. if agg.DocCount != 10 {
  1919. t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
  1920. }
  1921. }
  1922. func TestAggsBucketChildren(t *testing.T) {
  1923. s := `{
  1924. "to-answers": {
  1925. "doc_count" : 10
  1926. }
  1927. }`
  1928. aggs := new(Aggregations)
  1929. err := json.Unmarshal([]byte(s), &aggs)
  1930. if err != nil {
  1931. t.Fatalf("expected no error decoding; got: %v", err)
  1932. }
  1933. agg, found := aggs.Children("to-answers")
  1934. if !found {
  1935. t.Fatalf("expected aggregation to be found; got: %v", found)
  1936. }
  1937. if agg == nil {
  1938. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1939. }
  1940. if agg.DocCount != 10 {
  1941. t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
  1942. }
  1943. }
  1944. func TestAggsBucketTerms(t *testing.T) {
  1945. s := `{
  1946. "users" : {
  1947. "doc_count_error_upper_bound" : 1,
  1948. "sum_other_doc_count" : 2,
  1949. "buckets" : [ {
  1950. "key" : "olivere",
  1951. "doc_count" : 2
  1952. }, {
  1953. "key" : "sandrae",
  1954. "doc_count" : 1
  1955. } ]
  1956. }
  1957. }`
  1958. aggs := new(Aggregations)
  1959. err := json.Unmarshal([]byte(s), &aggs)
  1960. if err != nil {
  1961. t.Fatalf("expected no error decoding; got: %v", err)
  1962. }
  1963. agg, found := aggs.Terms("users")
  1964. if !found {
  1965. t.Fatalf("expected aggregation to be found; got: %v", found)
  1966. }
  1967. if agg == nil {
  1968. t.Fatalf("expected aggregation != nil; got: %v", agg)
  1969. }
  1970. if agg.Buckets == nil {
  1971. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  1972. }
  1973. if len(agg.Buckets) != 2 {
  1974. t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
  1975. }
  1976. if agg.Buckets[0].Key != "olivere" {
  1977. t.Errorf("expected key %q; got: %q", "olivere", agg.Buckets[0].Key)
  1978. }
  1979. if agg.Buckets[0].DocCount != 2 {
  1980. t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
  1981. }
  1982. if agg.Buckets[1].Key != "sandrae" {
  1983. t.Errorf("expected key %q; got: %q", "sandrae", agg.Buckets[1].Key)
  1984. }
  1985. if agg.Buckets[1].DocCount != 1 {
  1986. t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
  1987. }
  1988. }
  1989. func TestAggsBucketTermsWithNumericKeys(t *testing.T) {
  1990. s := `{
  1991. "users" : {
  1992. "doc_count_error_upper_bound" : 1,
  1993. "sum_other_doc_count" : 2,
  1994. "buckets" : [ {
  1995. "key" : 17,
  1996. "doc_count" : 2
  1997. }, {
  1998. "key" : 21,
  1999. "doc_count" : 1
  2000. } ]
  2001. }
  2002. }`
  2003. aggs := new(Aggregations)
  2004. err := json.Unmarshal([]byte(s), &aggs)
  2005. if err != nil {
  2006. t.Fatalf("expected no error decoding; got: %v", err)
  2007. }
  2008. agg, found := aggs.Terms("users")
  2009. if !found {
  2010. t.Fatalf("expected aggregation to be found; got: %v", found)
  2011. }
  2012. if agg == nil {
  2013. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2014. }
  2015. if agg.Buckets == nil {
  2016. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2017. }
  2018. if len(agg.Buckets) != 2 {
  2019. t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
  2020. }
  2021. if agg.Buckets[0].Key != float64(17) {
  2022. t.Errorf("expected key %v; got: %v", 17, agg.Buckets[0].Key)
  2023. }
  2024. if got, err := agg.Buckets[0].KeyNumber.Int64(); err != nil {
  2025. t.Errorf("expected to convert key to int64; got: %v", err)
  2026. } else if got != 17 {
  2027. t.Errorf("expected key %v; got: %v", 17, agg.Buckets[0].Key)
  2028. }
  2029. if agg.Buckets[0].DocCount != 2 {
  2030. t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
  2031. }
  2032. if agg.Buckets[1].Key != float64(21) {
  2033. t.Errorf("expected key %v; got: %v", 21, agg.Buckets[1].Key)
  2034. }
  2035. if got, err := agg.Buckets[1].KeyNumber.Int64(); err != nil {
  2036. t.Errorf("expected to convert key to int64; got: %v", err)
  2037. } else if got != 21 {
  2038. t.Errorf("expected key %v; got: %v", 21, agg.Buckets[1].Key)
  2039. }
  2040. if agg.Buckets[1].DocCount != 1 {
  2041. t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
  2042. }
  2043. }
  2044. func TestAggsBucketTermsWithBoolKeys(t *testing.T) {
  2045. s := `{
  2046. "users" : {
  2047. "doc_count_error_upper_bound" : 1,
  2048. "sum_other_doc_count" : 2,
  2049. "buckets" : [ {
  2050. "key" : true,
  2051. "doc_count" : 2
  2052. }, {
  2053. "key" : false,
  2054. "doc_count" : 1
  2055. } ]
  2056. }
  2057. }`
  2058. aggs := new(Aggregations)
  2059. err := json.Unmarshal([]byte(s), &aggs)
  2060. if err != nil {
  2061. t.Fatalf("expected no error decoding; got: %v", err)
  2062. }
  2063. agg, found := aggs.Terms("users")
  2064. if !found {
  2065. t.Fatalf("expected aggregation to be found; got: %v", found)
  2066. }
  2067. if agg == nil {
  2068. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2069. }
  2070. if agg.Buckets == nil {
  2071. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2072. }
  2073. if len(agg.Buckets) != 2 {
  2074. t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
  2075. }
  2076. if agg.Buckets[0].Key != true {
  2077. t.Errorf("expected key %v; got: %v", true, agg.Buckets[0].Key)
  2078. }
  2079. if agg.Buckets[0].DocCount != 2 {
  2080. t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
  2081. }
  2082. if agg.Buckets[1].Key != false {
  2083. t.Errorf("expected key %v; got: %v", false, agg.Buckets[1].Key)
  2084. }
  2085. if agg.Buckets[1].DocCount != 1 {
  2086. t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
  2087. }
  2088. }
  2089. func TestAggsBucketSignificantTerms(t *testing.T) {
  2090. s := `{
  2091. "significantCrimeTypes" : {
  2092. "doc_count": 47347,
  2093. "buckets" : [
  2094. {
  2095. "key": "Bicycle theft",
  2096. "doc_count": 3640,
  2097. "score": 0.371235374214817,
  2098. "bg_count": 66799
  2099. }
  2100. ]
  2101. }
  2102. }`
  2103. aggs := new(Aggregations)
  2104. err := json.Unmarshal([]byte(s), &aggs)
  2105. if err != nil {
  2106. t.Fatalf("expected no error decoding; got: %v", err)
  2107. }
  2108. agg, found := aggs.SignificantTerms("significantCrimeTypes")
  2109. if !found {
  2110. t.Fatalf("expected aggregation to be found; got: %v", found)
  2111. }
  2112. if agg == nil {
  2113. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2114. }
  2115. if agg.DocCount != 47347 {
  2116. t.Fatalf("expected aggregation DocCount != %d; got: %d", 47347, agg.DocCount)
  2117. }
  2118. if agg.Buckets == nil {
  2119. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2120. }
  2121. if len(agg.Buckets) != 1 {
  2122. t.Errorf("expected %d bucket entries; got: %d", 1, len(agg.Buckets))
  2123. }
  2124. if agg.Buckets[0].Key != "Bicycle theft" {
  2125. t.Errorf("expected key = %q; got: %q", "Bicycle theft", agg.Buckets[0].Key)
  2126. }
  2127. if agg.Buckets[0].DocCount != 3640 {
  2128. t.Errorf("expected doc count = %d; got: %d", 3640, agg.Buckets[0].DocCount)
  2129. }
  2130. if agg.Buckets[0].Score != float64(0.371235374214817) {
  2131. t.Errorf("expected score = %v; got: %v", float64(0.371235374214817), agg.Buckets[0].Score)
  2132. }
  2133. if agg.Buckets[0].BgCount != 66799 {
  2134. t.Errorf("expected BgCount = %d; got: %d", 66799, agg.Buckets[0].BgCount)
  2135. }
  2136. }
  2137. func TestAggsBucketSampler(t *testing.T) {
  2138. s := `{
  2139. "sample" : {
  2140. "doc_count": 1000,
  2141. "keywords": {
  2142. "doc_count": 1000,
  2143. "buckets" : [
  2144. {
  2145. "key": "bend",
  2146. "doc_count": 58,
  2147. "score": 37.982536582524276,
  2148. "bg_count": 103
  2149. }
  2150. ]
  2151. }
  2152. }
  2153. }`
  2154. aggs := new(Aggregations)
  2155. err := json.Unmarshal([]byte(s), &aggs)
  2156. if err != nil {
  2157. t.Fatalf("expected no error decoding; got: %v", err)
  2158. }
  2159. agg, found := aggs.Sampler("sample")
  2160. if !found {
  2161. t.Fatalf("expected aggregation to be found; got: %v", found)
  2162. }
  2163. if agg == nil {
  2164. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2165. }
  2166. if agg.DocCount != 1000 {
  2167. t.Fatalf("expected aggregation DocCount != %d; got: %d", 1000, agg.DocCount)
  2168. }
  2169. sub, found := agg.Aggregations["keywords"]
  2170. if !found {
  2171. t.Fatalf("expected sub aggregation %q", "keywords")
  2172. }
  2173. if sub == nil {
  2174. t.Fatalf("expected sub aggregation %q; got: %v", "keywords", sub)
  2175. }
  2176. }
  2177. func TestAggsBucketRange(t *testing.T) {
  2178. s := `{
  2179. "price_ranges" : {
  2180. "buckets": [
  2181. {
  2182. "to": 50,
  2183. "doc_count": 2
  2184. },
  2185. {
  2186. "from": 50,
  2187. "to": 100,
  2188. "doc_count": 4
  2189. },
  2190. {
  2191. "from": 100,
  2192. "doc_count": 4
  2193. }
  2194. ]
  2195. }
  2196. }`
  2197. aggs := new(Aggregations)
  2198. err := json.Unmarshal([]byte(s), &aggs)
  2199. if err != nil {
  2200. t.Fatalf("expected no error decoding; got: %v", err)
  2201. }
  2202. agg, found := aggs.Range("price_ranges")
  2203. if !found {
  2204. t.Fatalf("expected aggregation to be found; got: %v", found)
  2205. }
  2206. if agg == nil {
  2207. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2208. }
  2209. if agg.Buckets == nil {
  2210. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2211. }
  2212. if len(agg.Buckets) != 3 {
  2213. t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
  2214. }
  2215. if agg.Buckets[0].From != nil {
  2216. t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
  2217. }
  2218. if agg.Buckets[0].To == nil {
  2219. t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
  2220. }
  2221. if *agg.Buckets[0].To != float64(50) {
  2222. t.Errorf("expected To = %v; got: %v", float64(50), *agg.Buckets[0].To)
  2223. }
  2224. if agg.Buckets[0].DocCount != 2 {
  2225. t.Errorf("expected DocCount = %d; got: %d", 2, agg.Buckets[0].DocCount)
  2226. }
  2227. if agg.Buckets[1].From == nil {
  2228. t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
  2229. }
  2230. if *agg.Buckets[1].From != float64(50) {
  2231. t.Errorf("expected From = %v; got: %v", float64(50), *agg.Buckets[1].From)
  2232. }
  2233. if agg.Buckets[1].To == nil {
  2234. t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[1].To)
  2235. }
  2236. if *agg.Buckets[1].To != float64(100) {
  2237. t.Errorf("expected To = %v; got: %v", float64(100), *agg.Buckets[1].To)
  2238. }
  2239. if agg.Buckets[1].DocCount != 4 {
  2240. t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[1].DocCount)
  2241. }
  2242. if agg.Buckets[2].From == nil {
  2243. t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[2].From)
  2244. }
  2245. if *agg.Buckets[2].From != float64(100) {
  2246. t.Errorf("expected From = %v; got: %v", float64(100), *agg.Buckets[2].From)
  2247. }
  2248. if agg.Buckets[2].To != nil {
  2249. t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[2].To)
  2250. }
  2251. if agg.Buckets[2].DocCount != 4 {
  2252. t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[2].DocCount)
  2253. }
  2254. }
  2255. func TestAggsBucketDateRange(t *testing.T) {
  2256. s := `{
  2257. "range": {
  2258. "buckets": [
  2259. {
  2260. "to": 1.3437792E+12,
  2261. "to_as_string": "08-2012",
  2262. "doc_count": 7
  2263. },
  2264. {
  2265. "from": 1.3437792E+12,
  2266. "from_as_string": "08-2012",
  2267. "doc_count": 2
  2268. }
  2269. ]
  2270. }
  2271. }`
  2272. aggs := new(Aggregations)
  2273. err := json.Unmarshal([]byte(s), &aggs)
  2274. if err != nil {
  2275. t.Fatalf("expected no error decoding; got: %v", err)
  2276. }
  2277. agg, found := aggs.DateRange("range")
  2278. if !found {
  2279. t.Fatalf("expected aggregation to be found; got: %v", found)
  2280. }
  2281. if agg == nil {
  2282. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2283. }
  2284. if agg.Buckets == nil {
  2285. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2286. }
  2287. if len(agg.Buckets) != 2 {
  2288. t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
  2289. }
  2290. if agg.Buckets[0].From != nil {
  2291. t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
  2292. }
  2293. if agg.Buckets[0].To == nil {
  2294. t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
  2295. }
  2296. if *agg.Buckets[0].To != float64(1.3437792E+12) {
  2297. t.Errorf("expected To = %v; got: %v", float64(1.3437792E+12), *agg.Buckets[0].To)
  2298. }
  2299. if agg.Buckets[0].ToAsString != "08-2012" {
  2300. t.Errorf("expected ToAsString = %q; got: %q", "08-2012", agg.Buckets[0].ToAsString)
  2301. }
  2302. if agg.Buckets[0].DocCount != 7 {
  2303. t.Errorf("expected DocCount = %d; got: %d", 7, agg.Buckets[0].DocCount)
  2304. }
  2305. if agg.Buckets[1].From == nil {
  2306. t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
  2307. }
  2308. if *agg.Buckets[1].From != float64(1.3437792E+12) {
  2309. t.Errorf("expected From = %v; got: %v", float64(1.3437792E+12), *agg.Buckets[1].From)
  2310. }
  2311. if agg.Buckets[1].FromAsString != "08-2012" {
  2312. t.Errorf("expected FromAsString = %q; got: %q", "08-2012", agg.Buckets[1].FromAsString)
  2313. }
  2314. if agg.Buckets[1].To != nil {
  2315. t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[1].To)
  2316. }
  2317. if agg.Buckets[1].DocCount != 2 {
  2318. t.Errorf("expected DocCount = %d; got: %d", 2, agg.Buckets[1].DocCount)
  2319. }
  2320. }
  2321. func TestAggsBucketIPRange(t *testing.T) {
  2322. s := `{
  2323. "ip_ranges": {
  2324. "buckets" : [
  2325. {
  2326. "to": 167772165,
  2327. "to_as_string": "10.0.0.5",
  2328. "doc_count": 4
  2329. },
  2330. {
  2331. "from": 167772165,
  2332. "from_as_string": "10.0.0.5",
  2333. "doc_count": 6
  2334. }
  2335. ]
  2336. }
  2337. }`
  2338. aggs := new(Aggregations)
  2339. err := json.Unmarshal([]byte(s), &aggs)
  2340. if err != nil {
  2341. t.Fatalf("expected no error decoding; got: %v", err)
  2342. }
  2343. agg, found := aggs.IPRange("ip_ranges")
  2344. if !found {
  2345. t.Fatalf("expected aggregation to be found; got: %v", found)
  2346. }
  2347. if agg == nil {
  2348. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2349. }
  2350. if agg.Buckets == nil {
  2351. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2352. }
  2353. if len(agg.Buckets) != 2 {
  2354. t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
  2355. }
  2356. if agg.Buckets[0].From != nil {
  2357. t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
  2358. }
  2359. if agg.Buckets[0].To == nil {
  2360. t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
  2361. }
  2362. if *agg.Buckets[0].To != float64(167772165) {
  2363. t.Errorf("expected To = %v; got: %v", float64(167772165), *agg.Buckets[0].To)
  2364. }
  2365. if agg.Buckets[0].ToAsString != "10.0.0.5" {
  2366. t.Errorf("expected ToAsString = %q; got: %q", "10.0.0.5", agg.Buckets[0].ToAsString)
  2367. }
  2368. if agg.Buckets[0].DocCount != 4 {
  2369. t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[0].DocCount)
  2370. }
  2371. if agg.Buckets[1].From == nil {
  2372. t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
  2373. }
  2374. if *agg.Buckets[1].From != float64(167772165) {
  2375. t.Errorf("expected From = %v; got: %v", float64(167772165), *agg.Buckets[1].From)
  2376. }
  2377. if agg.Buckets[1].FromAsString != "10.0.0.5" {
  2378. t.Errorf("expected FromAsString = %q; got: %q", "10.0.0.5", agg.Buckets[1].FromAsString)
  2379. }
  2380. if agg.Buckets[1].To != nil {
  2381. t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[1].To)
  2382. }
  2383. if agg.Buckets[1].DocCount != 6 {
  2384. t.Errorf("expected DocCount = %d; got: %d", 6, agg.Buckets[1].DocCount)
  2385. }
  2386. }
  2387. func TestAggsBucketHistogram(t *testing.T) {
  2388. s := `{
  2389. "prices" : {
  2390. "buckets": [
  2391. {
  2392. "key": 0,
  2393. "doc_count": 2
  2394. },
  2395. {
  2396. "key": 50,
  2397. "doc_count": 4
  2398. },
  2399. {
  2400. "key": 150,
  2401. "doc_count": 3
  2402. }
  2403. ]
  2404. }
  2405. }`
  2406. aggs := new(Aggregations)
  2407. err := json.Unmarshal([]byte(s), &aggs)
  2408. if err != nil {
  2409. t.Fatalf("expected no error decoding; got: %v", err)
  2410. }
  2411. agg, found := aggs.Histogram("prices")
  2412. if !found {
  2413. t.Fatalf("expected aggregation to be found; got: %v", found)
  2414. }
  2415. if agg == nil {
  2416. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2417. }
  2418. if agg.Buckets == nil {
  2419. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2420. }
  2421. if len(agg.Buckets) != 3 {
  2422. t.Errorf("expected %d buckets; got: %d", 3, len(agg.Buckets))
  2423. }
  2424. if agg.Buckets[0].Key != 0 {
  2425. t.Errorf("expected key = %v; got: %v", 0, agg.Buckets[0].Key)
  2426. }
  2427. if agg.Buckets[0].KeyAsString != nil {
  2428. t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[0].KeyAsString)
  2429. }
  2430. if agg.Buckets[0].DocCount != 2 {
  2431. t.Errorf("expected doc count = %d; got: %d", 2, agg.Buckets[0].DocCount)
  2432. }
  2433. if agg.Buckets[1].Key != 50 {
  2434. t.Errorf("expected key = %v; got: %v", 50, agg.Buckets[1].Key)
  2435. }
  2436. if agg.Buckets[1].KeyAsString != nil {
  2437. t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[1].KeyAsString)
  2438. }
  2439. if agg.Buckets[1].DocCount != 4 {
  2440. t.Errorf("expected doc count = %d; got: %d", 4, agg.Buckets[1].DocCount)
  2441. }
  2442. if agg.Buckets[2].Key != 150 {
  2443. t.Errorf("expected key = %v; got: %v", 150, agg.Buckets[2].Key)
  2444. }
  2445. if agg.Buckets[2].KeyAsString != nil {
  2446. t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[2].KeyAsString)
  2447. }
  2448. if agg.Buckets[2].DocCount != 3 {
  2449. t.Errorf("expected doc count = %d; got: %d", 3, agg.Buckets[2].DocCount)
  2450. }
  2451. }
  2452. func TestAggsBucketDateHistogram(t *testing.T) {
  2453. s := `{
  2454. "articles_over_time": {
  2455. "buckets": [
  2456. {
  2457. "key_as_string": "2013-02-02",
  2458. "key": 1328140800000,
  2459. "doc_count": 1
  2460. },
  2461. {
  2462. "key_as_string": "2013-03-02",
  2463. "key": 1330646400000,
  2464. "doc_count": 2
  2465. }
  2466. ]
  2467. }
  2468. }`
  2469. aggs := new(Aggregations)
  2470. err := json.Unmarshal([]byte(s), &aggs)
  2471. if err != nil {
  2472. t.Fatalf("expected no error decoding; got: %v", err)
  2473. }
  2474. agg, found := aggs.DateHistogram("articles_over_time")
  2475. if !found {
  2476. t.Fatalf("expected aggregation to be found; got: %v", found)
  2477. }
  2478. if agg == nil {
  2479. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2480. }
  2481. if agg.Buckets == nil {
  2482. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2483. }
  2484. if len(agg.Buckets) != 2 {
  2485. t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
  2486. }
  2487. if agg.Buckets[0].Key != 1328140800000 {
  2488. t.Errorf("expected key %v; got: %v", 1328140800000, agg.Buckets[0].Key)
  2489. }
  2490. if agg.Buckets[0].KeyAsString == nil {
  2491. t.Fatalf("expected key_as_string != nil; got: %v", agg.Buckets[0].KeyAsString)
  2492. }
  2493. if *agg.Buckets[0].KeyAsString != "2013-02-02" {
  2494. t.Errorf("expected key_as_string %q; got: %q", "2013-02-02", *agg.Buckets[0].KeyAsString)
  2495. }
  2496. if agg.Buckets[0].DocCount != 1 {
  2497. t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[0].DocCount)
  2498. }
  2499. if agg.Buckets[1].Key != 1330646400000 {
  2500. t.Errorf("expected key %v; got: %v", 1330646400000, agg.Buckets[1].Key)
  2501. }
  2502. if agg.Buckets[1].KeyAsString == nil {
  2503. t.Fatalf("expected key_as_string != nil; got: %v", agg.Buckets[1].KeyAsString)
  2504. }
  2505. if *agg.Buckets[1].KeyAsString != "2013-03-02" {
  2506. t.Errorf("expected key_as_string %q; got: %q", "2013-03-02", *agg.Buckets[1].KeyAsString)
  2507. }
  2508. if agg.Buckets[1].DocCount != 2 {
  2509. t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[1].DocCount)
  2510. }
  2511. }
  2512. func TestAggsMetricsGeoBounds(t *testing.T) {
  2513. s := `{
  2514. "viewport": {
  2515. "bounds": {
  2516. "top_left": {
  2517. "lat": 80.45,
  2518. "lon": -160.22
  2519. },
  2520. "bottom_right": {
  2521. "lat": 40.65,
  2522. "lon": 42.57
  2523. }
  2524. }
  2525. }
  2526. }`
  2527. aggs := new(Aggregations)
  2528. err := json.Unmarshal([]byte(s), &aggs)
  2529. if err != nil {
  2530. t.Fatalf("expected no error decoding; got: %v", err)
  2531. }
  2532. agg, found := aggs.GeoBounds("viewport")
  2533. if !found {
  2534. t.Fatalf("expected aggregation to be found; got: %v", found)
  2535. }
  2536. if agg == nil {
  2537. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2538. }
  2539. if agg.Bounds.TopLeft.Latitude != float64(80.45) {
  2540. t.Fatalf("expected Bounds.TopLeft.Latitude != %v; got: %v", float64(80.45), agg.Bounds.TopLeft.Latitude)
  2541. }
  2542. if agg.Bounds.TopLeft.Longitude != float64(-160.22) {
  2543. t.Fatalf("expected Bounds.TopLeft.Longitude != %v; got: %v", float64(-160.22), agg.Bounds.TopLeft.Longitude)
  2544. }
  2545. if agg.Bounds.BottomRight.Latitude != float64(40.65) {
  2546. t.Fatalf("expected Bounds.BottomRight.Latitude != %v; got: %v", float64(40.65), agg.Bounds.BottomRight.Latitude)
  2547. }
  2548. if agg.Bounds.BottomRight.Longitude != float64(42.57) {
  2549. t.Fatalf("expected Bounds.BottomRight.Longitude != %v; got: %v", float64(42.57), agg.Bounds.BottomRight.Longitude)
  2550. }
  2551. }
  2552. func TestAggsBucketGeoHash(t *testing.T) {
  2553. s := `{
  2554. "myLarge-GrainGeoHashGrid": {
  2555. "buckets": [
  2556. {
  2557. "key": "svz",
  2558. "doc_count": 10964
  2559. },
  2560. {
  2561. "key": "sv8",
  2562. "doc_count": 3198
  2563. }
  2564. ]
  2565. }
  2566. }`
  2567. aggs := new(Aggregations)
  2568. err := json.Unmarshal([]byte(s), &aggs)
  2569. if err != nil {
  2570. t.Fatalf("expected no error decoding; got: %v", err)
  2571. }
  2572. agg, found := aggs.GeoHash("myLarge-GrainGeoHashGrid")
  2573. if !found {
  2574. t.Fatalf("expected aggregation to be found; got: %v", found)
  2575. }
  2576. if agg == nil {
  2577. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2578. }
  2579. if agg.Buckets == nil {
  2580. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2581. }
  2582. if len(agg.Buckets) != 2 {
  2583. t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
  2584. }
  2585. if agg.Buckets[0].Key != "svz" {
  2586. t.Errorf("expected key %q; got: %q", "svz", agg.Buckets[0].Key)
  2587. }
  2588. if agg.Buckets[0].DocCount != 10964 {
  2589. t.Errorf("expected doc count %d; got: %d", 10964, agg.Buckets[0].DocCount)
  2590. }
  2591. if agg.Buckets[1].Key != "sv8" {
  2592. t.Errorf("expected key %q; got: %q", "sv8", agg.Buckets[1].Key)
  2593. }
  2594. if agg.Buckets[1].DocCount != 3198 {
  2595. t.Errorf("expected doc count %d; got: %d", 3198, agg.Buckets[1].DocCount)
  2596. }
  2597. }
  2598. func TestAggsMetricsGeoCentroid(t *testing.T) {
  2599. s := `{
  2600. "centroid": {
  2601. "location": {
  2602. "lat": 80.45,
  2603. "lon": -160.22
  2604. },
  2605. "count": 6
  2606. }
  2607. }`
  2608. aggs := new(Aggregations)
  2609. err := json.Unmarshal([]byte(s), &aggs)
  2610. if err != nil {
  2611. t.Fatalf("expected no error decoding; got: %v", err)
  2612. }
  2613. agg, found := aggs.GeoCentroid("centroid")
  2614. if !found {
  2615. t.Fatalf("expected aggregation to be found; got: %v", found)
  2616. }
  2617. if agg == nil {
  2618. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2619. }
  2620. if agg.Location.Latitude != float64(80.45) {
  2621. t.Fatalf("expected Location.Latitude != %v; got: %v", float64(80.45), agg.Location.Latitude)
  2622. }
  2623. if agg.Location.Longitude != float64(-160.22) {
  2624. t.Fatalf("expected Location.Longitude != %v; got: %v", float64(-160.22), agg.Location.Longitude)
  2625. }
  2626. if agg.Count != int(6) {
  2627. t.Fatalf("expected Count != %v; got: %v", int(6), agg.Count)
  2628. }
  2629. }
  2630. func TestAggsBucketGeoDistance(t *testing.T) {
  2631. s := `{
  2632. "rings" : {
  2633. "buckets": [
  2634. {
  2635. "unit": "km",
  2636. "to": 100.0,
  2637. "doc_count": 3
  2638. },
  2639. {
  2640. "unit": "km",
  2641. "from": 100.0,
  2642. "to": 300.0,
  2643. "doc_count": 1
  2644. },
  2645. {
  2646. "unit": "km",
  2647. "from": 300.0,
  2648. "doc_count": 7
  2649. }
  2650. ]
  2651. }
  2652. }`
  2653. aggs := new(Aggregations)
  2654. err := json.Unmarshal([]byte(s), &aggs)
  2655. if err != nil {
  2656. t.Fatalf("expected no error decoding; got: %v", err)
  2657. }
  2658. agg, found := aggs.GeoDistance("rings")
  2659. if !found {
  2660. t.Fatalf("expected aggregation to be found; got: %v", found)
  2661. }
  2662. if agg == nil {
  2663. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2664. }
  2665. if agg.Buckets == nil {
  2666. t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
  2667. }
  2668. if len(agg.Buckets) != 3 {
  2669. t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
  2670. }
  2671. if agg.Buckets[0].From != nil {
  2672. t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
  2673. }
  2674. if agg.Buckets[0].To == nil {
  2675. t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
  2676. }
  2677. if *agg.Buckets[0].To != float64(100.0) {
  2678. t.Errorf("expected To = %v; got: %v", float64(100.0), *agg.Buckets[0].To)
  2679. }
  2680. if agg.Buckets[0].DocCount != 3 {
  2681. t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[0].DocCount)
  2682. }
  2683. if agg.Buckets[1].From == nil {
  2684. t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
  2685. }
  2686. if *agg.Buckets[1].From != float64(100.0) {
  2687. t.Errorf("expected From = %v; got: %v", float64(100.0), *agg.Buckets[1].From)
  2688. }
  2689. if agg.Buckets[1].To == nil {
  2690. t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[1].To)
  2691. }
  2692. if *agg.Buckets[1].To != float64(300.0) {
  2693. t.Errorf("expected From = %v; got: %v", float64(300.0), *agg.Buckets[1].To)
  2694. }
  2695. if agg.Buckets[1].DocCount != 1 {
  2696. t.Errorf("expected DocCount = %d; got: %d", 1, agg.Buckets[1].DocCount)
  2697. }
  2698. if agg.Buckets[2].From == nil {
  2699. t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[2].From)
  2700. }
  2701. if *agg.Buckets[2].From != float64(300.0) {
  2702. t.Errorf("expected From = %v; got: %v", float64(300.0), *agg.Buckets[2].From)
  2703. }
  2704. if agg.Buckets[2].To != nil {
  2705. t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[2].To)
  2706. }
  2707. if agg.Buckets[2].DocCount != 7 {
  2708. t.Errorf("expected DocCount = %d; got: %d", 7, agg.Buckets[2].DocCount)
  2709. }
  2710. }
  2711. func TestAggsSubAggregates(t *testing.T) {
  2712. rs := `{
  2713. "users" : {
  2714. "doc_count_error_upper_bound" : 1,
  2715. "sum_other_doc_count" : 2,
  2716. "buckets" : [ {
  2717. "key" : "olivere",
  2718. "doc_count" : 2,
  2719. "ts" : {
  2720. "buckets" : [ {
  2721. "key_as_string" : "2012-01-01T00:00:00.000Z",
  2722. "key" : 1325376000000,
  2723. "doc_count" : 2
  2724. } ]
  2725. }
  2726. }, {
  2727. "key" : "sandrae",
  2728. "doc_count" : 1,
  2729. "ts" : {
  2730. "buckets" : [ {
  2731. "key_as_string" : "2011-01-01T00:00:00.000Z",
  2732. "key" : 1293840000000,
  2733. "doc_count" : 1
  2734. } ]
  2735. }
  2736. } ]
  2737. }
  2738. }`
  2739. aggs := new(Aggregations)
  2740. err := json.Unmarshal([]byte(rs), &aggs)
  2741. if err != nil {
  2742. t.Fatalf("expected no error decoding; got: %v", err)
  2743. }
  2744. // Access top-level aggregation
  2745. users, found := aggs.Terms("users")
  2746. if !found {
  2747. t.Fatalf("expected users aggregation to be found; got: %v", found)
  2748. }
  2749. if users == nil {
  2750. t.Fatalf("expected users aggregation; got: %v", users)
  2751. }
  2752. if users.Buckets == nil {
  2753. t.Fatalf("expected users buckets; got: %v", users.Buckets)
  2754. }
  2755. if len(users.Buckets) != 2 {
  2756. t.Errorf("expected %d bucket entries; got: %d", 2, len(users.Buckets))
  2757. }
  2758. if users.Buckets[0].Key != "olivere" {
  2759. t.Errorf("expected key %q; got: %q", "olivere", users.Buckets[0].Key)
  2760. }
  2761. if users.Buckets[0].DocCount != 2 {
  2762. t.Errorf("expected doc count %d; got: %d", 2, users.Buckets[0].DocCount)
  2763. }
  2764. if users.Buckets[1].Key != "sandrae" {
  2765. t.Errorf("expected key %q; got: %q", "sandrae", users.Buckets[1].Key)
  2766. }
  2767. if users.Buckets[1].DocCount != 1 {
  2768. t.Errorf("expected doc count %d; got: %d", 1, users.Buckets[1].DocCount)
  2769. }
  2770. // Access sub-aggregation
  2771. ts, found := users.Buckets[0].DateHistogram("ts")
  2772. if !found {
  2773. t.Fatalf("expected ts aggregation to be found; got: %v", found)
  2774. }
  2775. if ts == nil {
  2776. t.Fatalf("expected ts aggregation; got: %v", ts)
  2777. }
  2778. if ts.Buckets == nil {
  2779. t.Fatalf("expected ts buckets; got: %v", ts.Buckets)
  2780. }
  2781. if len(ts.Buckets) != 1 {
  2782. t.Errorf("expected %d bucket entries; got: %d", 1, len(ts.Buckets))
  2783. }
  2784. if ts.Buckets[0].Key != 1325376000000 {
  2785. t.Errorf("expected key %v; got: %v", 1325376000000, ts.Buckets[0].Key)
  2786. }
  2787. if ts.Buckets[0].KeyAsString == nil {
  2788. t.Fatalf("expected key_as_string != %v; got: %v", nil, ts.Buckets[0].KeyAsString)
  2789. }
  2790. if *ts.Buckets[0].KeyAsString != "2012-01-01T00:00:00.000Z" {
  2791. t.Errorf("expected key_as_string %q; got: %q", "2012-01-01T00:00:00.000Z", *ts.Buckets[0].KeyAsString)
  2792. }
  2793. }
  2794. func TestAggsPipelineAvgBucket(t *testing.T) {
  2795. s := `{
  2796. "avg_monthly_sales" : {
  2797. "value" : 328.33333333333333
  2798. }
  2799. }`
  2800. aggs := new(Aggregations)
  2801. err := json.Unmarshal([]byte(s), &aggs)
  2802. if err != nil {
  2803. t.Fatalf("expected no error decoding; got: %v", err)
  2804. }
  2805. agg, found := aggs.AvgBucket("avg_monthly_sales")
  2806. if !found {
  2807. t.Fatalf("expected aggregation to be found; got: %v", found)
  2808. }
  2809. if agg == nil {
  2810. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2811. }
  2812. if agg.Value == nil {
  2813. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  2814. }
  2815. if *agg.Value != float64(328.33333333333333) {
  2816. t.Fatalf("expected aggregation value = %v; got: %v", float64(328.33333333333333), *agg.Value)
  2817. }
  2818. }
  2819. func TestAggsPipelineSumBucket(t *testing.T) {
  2820. s := `{
  2821. "sum_monthly_sales" : {
  2822. "value" : 985
  2823. }
  2824. }`
  2825. aggs := new(Aggregations)
  2826. err := json.Unmarshal([]byte(s), &aggs)
  2827. if err != nil {
  2828. t.Fatalf("expected no error decoding; got: %v", err)
  2829. }
  2830. agg, found := aggs.SumBucket("sum_monthly_sales")
  2831. if !found {
  2832. t.Fatalf("expected aggregation to be found; got: %v", found)
  2833. }
  2834. if agg == nil {
  2835. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2836. }
  2837. if agg.Value == nil {
  2838. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  2839. }
  2840. if *agg.Value != float64(985) {
  2841. t.Fatalf("expected aggregation value = %v; got: %v", float64(985), *agg.Value)
  2842. }
  2843. }
  2844. func TestAggsPipelineMaxBucket(t *testing.T) {
  2845. s := `{
  2846. "max_monthly_sales" : {
  2847. "keys": ["2015/01/01 00:00:00"],
  2848. "value" : 550
  2849. }
  2850. }`
  2851. aggs := new(Aggregations)
  2852. err := json.Unmarshal([]byte(s), &aggs)
  2853. if err != nil {
  2854. t.Fatalf("expected no error decoding; got: %v", err)
  2855. }
  2856. agg, found := aggs.MaxBucket("max_monthly_sales")
  2857. if !found {
  2858. t.Fatalf("expected aggregation to be found; got: %v", found)
  2859. }
  2860. if agg == nil {
  2861. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2862. }
  2863. if len(agg.Keys) != 1 {
  2864. t.Fatalf("expected 1 key; got: %d", len(agg.Keys))
  2865. }
  2866. if got, want := agg.Keys[0], "2015/01/01 00:00:00"; got != want {
  2867. t.Fatalf("expected key %q; got: %v (%T)", want, got, got)
  2868. }
  2869. if agg.Value == nil {
  2870. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  2871. }
  2872. if *agg.Value != float64(550) {
  2873. t.Fatalf("expected aggregation value = %v; got: %v", float64(550), *agg.Value)
  2874. }
  2875. }
  2876. func TestAggsPipelineMinBucket(t *testing.T) {
  2877. s := `{
  2878. "min_monthly_sales" : {
  2879. "keys": ["2015/02/01 00:00:00"],
  2880. "value" : 60
  2881. }
  2882. }`
  2883. aggs := new(Aggregations)
  2884. err := json.Unmarshal([]byte(s), &aggs)
  2885. if err != nil {
  2886. t.Fatalf("expected no error decoding; got: %v", err)
  2887. }
  2888. agg, found := aggs.MinBucket("min_monthly_sales")
  2889. if !found {
  2890. t.Fatalf("expected aggregation to be found; got: %v", found)
  2891. }
  2892. if agg == nil {
  2893. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2894. }
  2895. if len(agg.Keys) != 1 {
  2896. t.Fatalf("expected 1 key; got: %d", len(agg.Keys))
  2897. }
  2898. if got, want := agg.Keys[0], "2015/02/01 00:00:00"; got != want {
  2899. t.Fatalf("expected key %q; got: %v (%T)", want, got, got)
  2900. }
  2901. if agg.Value == nil {
  2902. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  2903. }
  2904. if *agg.Value != float64(60) {
  2905. t.Fatalf("expected aggregation value = %v; got: %v", float64(60), *agg.Value)
  2906. }
  2907. }
  2908. func TestAggsPipelineMovAvg(t *testing.T) {
  2909. s := `{
  2910. "the_movavg" : {
  2911. "value" : 12.0
  2912. }
  2913. }`
  2914. aggs := new(Aggregations)
  2915. err := json.Unmarshal([]byte(s), &aggs)
  2916. if err != nil {
  2917. t.Fatalf("expected no error decoding; got: %v", err)
  2918. }
  2919. agg, found := aggs.MovAvg("the_movavg")
  2920. if !found {
  2921. t.Fatalf("expected aggregation to be found; got: %v", found)
  2922. }
  2923. if agg == nil {
  2924. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2925. }
  2926. if agg.Value == nil {
  2927. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  2928. }
  2929. if *agg.Value != float64(12.0) {
  2930. t.Fatalf("expected aggregation value = %v; got: %v", float64(12.0), *agg.Value)
  2931. }
  2932. }
  2933. func TestAggsPipelineDerivative(t *testing.T) {
  2934. s := `{
  2935. "sales_deriv" : {
  2936. "value" : 315
  2937. }
  2938. }`
  2939. aggs := new(Aggregations)
  2940. err := json.Unmarshal([]byte(s), &aggs)
  2941. if err != nil {
  2942. t.Fatalf("expected no error decoding; got: %v", err)
  2943. }
  2944. agg, found := aggs.Derivative("sales_deriv")
  2945. if !found {
  2946. t.Fatalf("expected aggregation to be found; got: %v", found)
  2947. }
  2948. if agg == nil {
  2949. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2950. }
  2951. if agg.Value == nil {
  2952. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  2953. }
  2954. if *agg.Value != float64(315) {
  2955. t.Fatalf("expected aggregation value = %v; got: %v", float64(315), *agg.Value)
  2956. }
  2957. }
  2958. func TestAggsPipelinePercentilesBucket(t *testing.T) {
  2959. s := `{
  2960. "sales_percentiles": {
  2961. "values": {
  2962. "25.0": 100,
  2963. "50.0": 200,
  2964. "75.0": 300
  2965. }
  2966. }
  2967. }`
  2968. aggs := new(Aggregations)
  2969. err := json.Unmarshal([]byte(s), &aggs)
  2970. if err != nil {
  2971. t.Fatalf("expected no error decoding; got: %v", err)
  2972. }
  2973. agg, found := aggs.PercentilesBucket("sales_percentiles")
  2974. if !found {
  2975. t.Fatalf("expected aggregation to be found; got: %v", found)
  2976. }
  2977. if agg == nil {
  2978. t.Fatalf("expected aggregation != nil; got: %v", agg)
  2979. }
  2980. if len(agg.Values) != 3 {
  2981. t.Fatalf("expected aggregation map with three entries; got: %v", agg.Values)
  2982. }
  2983. }
  2984. func TestAggsPipelineStatsBucket(t *testing.T) {
  2985. s := `{
  2986. "stats_monthly_sales": {
  2987. "count": 3,
  2988. "min": 60.0,
  2989. "max": 550.0,
  2990. "avg": 328.3333333333333,
  2991. "sum": 985.0
  2992. }
  2993. }`
  2994. aggs := new(Aggregations)
  2995. err := json.Unmarshal([]byte(s), &aggs)
  2996. if err != nil {
  2997. t.Fatalf("expected no error decoding; got: %v", err)
  2998. }
  2999. agg, found := aggs.StatsBucket("stats_monthly_sales")
  3000. if !found {
  3001. t.Fatalf("expected aggregation to be found; got: %v", found)
  3002. }
  3003. if agg == nil {
  3004. t.Fatalf("expected aggregation != nil; got: %v", agg)
  3005. }
  3006. if agg.Count != 3 {
  3007. t.Fatalf("expected aggregation count = %v; got: %v", 3, agg.Count)
  3008. }
  3009. if agg.Min == nil {
  3010. t.Fatalf("expected aggregation min != nil; got: %v", agg.Min)
  3011. }
  3012. if *agg.Min != float64(60.0) {
  3013. t.Fatalf("expected aggregation min = %v; got: %v", float64(60.0), *agg.Min)
  3014. }
  3015. if agg.Max == nil {
  3016. t.Fatalf("expected aggregation max != nil; got: %v", agg.Max)
  3017. }
  3018. if *agg.Max != float64(550.0) {
  3019. t.Fatalf("expected aggregation max = %v; got: %v", float64(550.0), *agg.Max)
  3020. }
  3021. if agg.Avg == nil {
  3022. t.Fatalf("expected aggregation avg != nil; got: %v", agg.Avg)
  3023. }
  3024. if *agg.Avg != float64(328.3333333333333) {
  3025. t.Fatalf("expected aggregation average = %v; got: %v", float64(328.3333333333333), *agg.Avg)
  3026. }
  3027. if agg.Sum == nil {
  3028. t.Fatalf("expected aggregation sum != nil; got: %v", agg.Sum)
  3029. }
  3030. if *agg.Sum != float64(985.0) {
  3031. t.Fatalf("expected aggregation sum = %v; got: %v", float64(985.0), *agg.Sum)
  3032. }
  3033. }
  3034. func TestAggsPipelineCumulativeSum(t *testing.T) {
  3035. s := `{
  3036. "cumulative_sales" : {
  3037. "value" : 550
  3038. }
  3039. }`
  3040. aggs := new(Aggregations)
  3041. err := json.Unmarshal([]byte(s), &aggs)
  3042. if err != nil {
  3043. t.Fatalf("expected no error decoding; got: %v", err)
  3044. }
  3045. agg, found := aggs.CumulativeSum("cumulative_sales")
  3046. if !found {
  3047. t.Fatalf("expected aggregation to be found; got: %v", found)
  3048. }
  3049. if agg == nil {
  3050. t.Fatalf("expected aggregation != nil; got: %v", agg)
  3051. }
  3052. if agg.Value == nil {
  3053. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  3054. }
  3055. if *agg.Value != float64(550) {
  3056. t.Fatalf("expected aggregation value = %v; got: %v", float64(550), *agg.Value)
  3057. }
  3058. }
  3059. func TestAggsPipelineBucketScript(t *testing.T) {
  3060. s := `{
  3061. "t-shirt-percentage" : {
  3062. "value" : 20
  3063. }
  3064. }`
  3065. aggs := new(Aggregations)
  3066. err := json.Unmarshal([]byte(s), &aggs)
  3067. if err != nil {
  3068. t.Fatalf("expected no error decoding; got: %v", err)
  3069. }
  3070. agg, found := aggs.BucketScript("t-shirt-percentage")
  3071. if !found {
  3072. t.Fatalf("expected aggregation to be found; got: %v", found)
  3073. }
  3074. if agg == nil {
  3075. t.Fatalf("expected aggregation != nil; got: %v", agg)
  3076. }
  3077. if agg.Value == nil {
  3078. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  3079. }
  3080. if *agg.Value != float64(20) {
  3081. t.Fatalf("expected aggregation value = %v; got: %v", float64(20), *agg.Value)
  3082. }
  3083. }
  3084. func TestAggsPipelineSerialDiff(t *testing.T) {
  3085. s := `{
  3086. "the_diff" : {
  3087. "value" : -722.0
  3088. }
  3089. }`
  3090. aggs := new(Aggregations)
  3091. err := json.Unmarshal([]byte(s), &aggs)
  3092. if err != nil {
  3093. t.Fatalf("expected no error decoding; got: %v", err)
  3094. }
  3095. agg, found := aggs.SerialDiff("the_diff")
  3096. if !found {
  3097. t.Fatalf("expected aggregation to be found; got: %v", found)
  3098. }
  3099. if agg == nil {
  3100. t.Fatalf("expected aggregation != nil; got: %v", agg)
  3101. }
  3102. if agg.Value == nil {
  3103. t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
  3104. }
  3105. if *agg.Value != float64(-722.0) {
  3106. t.Fatalf("expected aggregation value = %v; got: %v", float64(20), *agg.Value)
  3107. }
  3108. }