search_aggs_test.go 94 KB

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