12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263 |
- // Copyright 2012-present Oliver Eilhard. All rights reserved.
- // Use of this source code is governed by a MIT-license.
- // See http://olivere.mit-license.org/license.txt for details.
- package elastic
- import (
- "context"
- "encoding/json"
- "strings"
- "testing"
- "time"
- )
- func TestAggs(t *testing.T) {
- // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere",
- Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Image: "http://golang.org/doc/gopher/gophercolor.png",
- Tags: []string{"golang", "elasticsearch"},
- Location: "48.1333,11.5667", // lat,lon
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere",
- Retweets: 0,
- Message: "Another unrelated topic.",
- Tags: []string{"golang"},
- Location: "48.1189,11.4289", // lat,lon
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae",
- Retweets: 12,
- Message: "Cycling is fun.",
- Tags: []string{"sports", "cycling"},
- Location: "47.7167,11.7167", // lat,lon
- Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
- }
- // Add all documents
- _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Flush().Index(testIndexName).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- // Match all should return all documents
- all := NewMatchAllQuery()
- // Terms Aggregate by user name
- globalAgg := NewGlobalAggregation()
- usersAgg := NewTermsAggregation().Field("user").Size(10).OrderByCountDesc()
- retweetsAgg := NewTermsAggregation().Field("retweets").Size(10)
- avgRetweetsAgg := NewAvgAggregation().Field("retweets")
- avgRetweetsWithMetaAgg := NewAvgAggregation().Field("retweetsMeta").Meta(map[string]interface{}{"meta": true})
- minRetweetsAgg := NewMinAggregation().Field("retweets")
- maxRetweetsAgg := NewMaxAggregation().Field("retweets")
- sumRetweetsAgg := NewSumAggregation().Field("retweets")
- statsRetweetsAgg := NewStatsAggregation().Field("retweets")
- extstatsRetweetsAgg := NewExtendedStatsAggregation().Field("retweets")
- valueCountRetweetsAgg := NewValueCountAggregation().Field("retweets")
- percentilesRetweetsAgg := NewPercentilesAggregation().Field("retweets")
- percentileRanksRetweetsAgg := NewPercentileRanksAggregation().Field("retweets").Values(25, 50, 75)
- cardinalityAgg := NewCardinalityAggregation().Field("user")
- significantTermsAgg := NewSignificantTermsAggregation().Field("message")
- samplerAgg := NewSamplerAggregation().SubAggregation("tagged_with", NewTermsAggregation().Field("tags"))
- retweetsRangeAgg := NewRangeAggregation().Field("retweets").Lt(10).Between(10, 100).Gt(100)
- retweetsKeyedRangeAgg := NewRangeAggregation().Field("retweets").Keyed(true).Lt(10).Between(10, 100).Gt(100)
- dateRangeAgg := NewDateRangeAggregation().Field("created").Lt("2012-01-01").Between("2012-01-01", "2013-01-01").Gt("2013-01-01")
- missingTagsAgg := NewMissingAggregation().Field("tags")
- retweetsHistoAgg := NewHistogramAggregation().Field("retweets").Interval(100)
- dateHistoAgg := NewDateHistogramAggregation().Field("created").Interval("year")
- retweetsFilterAgg := NewFilterAggregation().Filter(
- NewRangeQuery("created").Gte("2012-01-01").Lte("2012-12-31")).
- SubAggregation("avgRetweetsSub", NewAvgAggregation().Field("retweets"))
- queryFilterAgg := NewFilterAggregation().Filter(NewTermQuery("tags", "golang"))
- topTagsHitsAgg := NewTopHitsAggregation().Sort("created", false).Size(5).FetchSource(true)
- topTagsAgg := NewTermsAggregation().Field("tags").Size(3).SubAggregation("top_tag_hits", topTagsHitsAgg)
- geoBoundsAgg := NewGeoBoundsAggregation().Field("location")
- geoHashAgg := NewGeoHashGridAggregation().Field("location").Precision(5)
- geoCentroidAgg := NewGeoCentroidAggregation().Field("location")
- // Run query
- builder := client.Search().Index(testIndexName).Query(all).Pretty(true)
- builder = builder.Aggregation("global", globalAgg)
- builder = builder.Aggregation("users", usersAgg)
- builder = builder.Aggregation("retweets", retweetsAgg)
- builder = builder.Aggregation("avgRetweets", avgRetweetsAgg)
- builder = builder.Aggregation("avgRetweetsWithMeta", avgRetweetsWithMetaAgg)
- builder = builder.Aggregation("minRetweets", minRetweetsAgg)
- builder = builder.Aggregation("maxRetweets", maxRetweetsAgg)
- builder = builder.Aggregation("sumRetweets", sumRetweetsAgg)
- builder = builder.Aggregation("statsRetweets", statsRetweetsAgg)
- builder = builder.Aggregation("extstatsRetweets", extstatsRetweetsAgg)
- builder = builder.Aggregation("valueCountRetweets", valueCountRetweetsAgg)
- builder = builder.Aggregation("percentilesRetweets", percentilesRetweetsAgg)
- builder = builder.Aggregation("percentileRanksRetweets", percentileRanksRetweetsAgg)
- builder = builder.Aggregation("usersCardinality", cardinalityAgg)
- builder = builder.Aggregation("significantTerms", significantTermsAgg)
- builder = builder.Aggregation("sample", samplerAgg)
- builder = builder.Aggregation("retweetsRange", retweetsRangeAgg)
- builder = builder.Aggregation("retweetsKeyedRange", retweetsKeyedRangeAgg)
- builder = builder.Aggregation("dateRange", dateRangeAgg)
- builder = builder.Aggregation("missingTags", missingTagsAgg)
- builder = builder.Aggregation("retweetsHisto", retweetsHistoAgg)
- builder = builder.Aggregation("dateHisto", dateHistoAgg)
- builder = builder.Aggregation("retweetsFilter", retweetsFilterAgg)
- builder = builder.Aggregation("queryFilter", queryFilterAgg)
- builder = builder.Aggregation("top-tags", topTagsAgg)
- builder = builder.Aggregation("viewport", geoBoundsAgg)
- builder = builder.Aggregation("geohashed", geoHashAgg)
- builder = builder.Aggregation("centroid", geoCentroidAgg)
- // Unnamed filters
- countByUserAgg := NewFiltersAggregation().
- Filters(NewTermQuery("user", "olivere"), NewTermQuery("user", "sandrae"))
- builder = builder.Aggregation("countByUser", countByUserAgg)
- // Named filters
- countByUserAgg2 := NewFiltersAggregation().
- FilterWithName("olivere", NewTermQuery("user", "olivere")).
- FilterWithName("sandrae", NewTermQuery("user", "sandrae"))
- builder = builder.Aggregation("countByUser2", countByUserAgg2)
- // AvgBucket
- dateHisto := NewDateHistogramAggregation().Field("created").Interval("year")
- dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
- builder = builder.Aggregation("avgBucketDateHisto", dateHisto)
- builder = builder.Aggregation("avgSumOfRetweets", NewAvgBucketAggregation().BucketsPath("avgBucketDateHisto>sumOfRetweets"))
- // MinBucket
- dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
- dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
- builder = builder.Aggregation("minBucketDateHisto", dateHisto)
- builder = builder.Aggregation("minBucketSumOfRetweets", NewMinBucketAggregation().BucketsPath("minBucketDateHisto>sumOfRetweets"))
- // MaxBucket
- dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
- dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
- builder = builder.Aggregation("maxBucketDateHisto", dateHisto)
- builder = builder.Aggregation("maxBucketSumOfRetweets", NewMaxBucketAggregation().BucketsPath("maxBucketDateHisto>sumOfRetweets"))
- // SumBucket
- dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
- dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
- builder = builder.Aggregation("sumBucketDateHisto", dateHisto)
- builder = builder.Aggregation("sumBucketSumOfRetweets", NewSumBucketAggregation().BucketsPath("sumBucketDateHisto>sumOfRetweets"))
- // MovAvg
- dateHisto = NewDateHistogramAggregation().Field("created").Interval("year")
- dateHisto = dateHisto.SubAggregation("sumOfRetweets", NewSumAggregation().Field("retweets"))
- dateHisto = dateHisto.SubAggregation("movingAvg", NewMovAvgAggregation().BucketsPath("sumOfRetweets"))
- builder = builder.Aggregation("movingAvgDateHisto", dateHisto)
- searchResult, err := builder.Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected Hits != nil; got: nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected Hits.TotalHits = %d; got: %d", 3, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 3 {
- t.Errorf("expected len(Hits.Hits) = %d; got: %d", 3, len(searchResult.Hits.Hits))
- }
- agg := searchResult.Aggregations
- if agg == nil {
- t.Fatalf("expected Aggregations != nil; got: nil")
- }
- // Search for non-existent aggregate should return (nil, false)
- unknownAgg, found := agg.Terms("no-such-aggregate")
- if found {
- t.Errorf("expected unknown aggregation to not be found; got: %v", found)
- }
- if unknownAgg != nil {
- t.Errorf("expected unknown aggregation to return %v; got %v", nil, unknownAgg)
- }
- // Global
- globalAggRes, found := agg.Global("global")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if globalAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if globalAggRes.DocCount != 3 {
- t.Errorf("expected DocCount = %d; got: %d", 3, globalAggRes.DocCount)
- }
- // Search for existent aggregate (by name) should return (aggregate, true)
- termsAggRes, found := agg.Terms("users")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if termsAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(termsAggRes.Buckets) != 2 {
- t.Fatalf("expected %d; got: %d", 2, len(termsAggRes.Buckets))
- }
- if termsAggRes.Buckets[0].Key != "olivere" {
- t.Errorf("expected %q; got: %q", "olivere", termsAggRes.Buckets[0].Key)
- }
- if termsAggRes.Buckets[0].DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, termsAggRes.Buckets[0].DocCount)
- }
- if termsAggRes.Buckets[1].Key != "sandrae" {
- t.Errorf("expected %q; got: %q", "sandrae", termsAggRes.Buckets[1].Key)
- }
- if termsAggRes.Buckets[1].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, termsAggRes.Buckets[1].DocCount)
- }
- // A terms aggregate with keys that are not strings
- retweetsAggRes, found := agg.Terms("retweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if retweetsAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(retweetsAggRes.Buckets) != 3 {
- t.Fatalf("expected %d; got: %d", 3, len(retweetsAggRes.Buckets))
- }
- if retweetsAggRes.Buckets[0].Key != float64(0) {
- t.Errorf("expected %v; got: %v", float64(0), retweetsAggRes.Buckets[0].Key)
- }
- if got, err := retweetsAggRes.Buckets[0].KeyNumber.Int64(); err != nil {
- t.Errorf("expected %d; got: %v", 0, retweetsAggRes.Buckets[0].Key)
- } else if got != 0 {
- t.Errorf("expected %d; got: %d", 0, got)
- }
- if retweetsAggRes.Buckets[0].KeyNumber != "0" {
- t.Errorf("expected %q; got: %q", "0", retweetsAggRes.Buckets[0].KeyNumber)
- }
- if retweetsAggRes.Buckets[0].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[0].DocCount)
- }
- if retweetsAggRes.Buckets[1].Key != float64(12) {
- t.Errorf("expected %v; got: %v", float64(12), retweetsAggRes.Buckets[1].Key)
- }
- if got, err := retweetsAggRes.Buckets[1].KeyNumber.Int64(); err != nil {
- t.Errorf("expected %d; got: %v", 0, retweetsAggRes.Buckets[1].KeyNumber)
- } else if got != 12 {
- t.Errorf("expected %d; got: %d", 12, got)
- }
- if retweetsAggRes.Buckets[1].KeyNumber != "12" {
- t.Errorf("expected %q; got: %q", "12", retweetsAggRes.Buckets[1].KeyNumber)
- }
- if retweetsAggRes.Buckets[1].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[1].DocCount)
- }
- if retweetsAggRes.Buckets[2].Key != float64(108) {
- t.Errorf("expected %v; got: %v", float64(108), retweetsAggRes.Buckets[2].Key)
- }
- if got, err := retweetsAggRes.Buckets[2].KeyNumber.Int64(); err != nil {
- t.Errorf("expected %d; got: %v", 108, retweetsAggRes.Buckets[2].KeyNumber)
- } else if got != 108 {
- t.Errorf("expected %d; got: %d", 108, got)
- }
- if retweetsAggRes.Buckets[2].KeyNumber != "108" {
- t.Errorf("expected %q; got: %q", "108", retweetsAggRes.Buckets[2].KeyNumber)
- }
- if retweetsAggRes.Buckets[2].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, retweetsAggRes.Buckets[2].DocCount)
- }
- // avgRetweets
- avgAggRes, found := agg.Avg("avgRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if avgAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if avgAggRes.Value == nil {
- t.Fatalf("expected != nil; got: %v", *avgAggRes.Value)
- }
- if *avgAggRes.Value != 40.0 {
- t.Errorf("expected %v; got: %v", 40.0, *avgAggRes.Value)
- }
- // avgRetweetsWithMeta
- avgMetaAggRes, found := agg.Avg("avgRetweetsWithMeta")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if avgMetaAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if avgMetaAggRes.Meta == nil {
- t.Fatalf("expected != nil; got: %v", avgMetaAggRes.Meta)
- }
- metaDataValue, found := avgMetaAggRes.Meta["meta"]
- if !found {
- t.Fatalf("expected to return meta data key %q; got: %v", "meta", found)
- }
- if flag, ok := metaDataValue.(bool); !ok {
- t.Fatalf("expected to return meta data key type %T; got: %T", true, metaDataValue)
- } else if flag != true {
- t.Fatalf("expected to return meta data key value %v; got: %v", true, flag)
- }
- // minRetweets
- minAggRes, found := agg.Min("minRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if minAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if minAggRes.Value == nil {
- t.Fatalf("expected != nil; got: %v", *minAggRes.Value)
- }
- if *minAggRes.Value != 0.0 {
- t.Errorf("expected %v; got: %v", 0.0, *minAggRes.Value)
- }
- // maxRetweets
- maxAggRes, found := agg.Max("maxRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if maxAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if maxAggRes.Value == nil {
- t.Fatalf("expected != nil; got: %v", *maxAggRes.Value)
- }
- if *maxAggRes.Value != 108.0 {
- t.Errorf("expected %v; got: %v", 108.0, *maxAggRes.Value)
- }
- // sumRetweets
- sumAggRes, found := agg.Sum("sumRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if sumAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if sumAggRes.Value == nil {
- t.Fatalf("expected != nil; got: %v", *sumAggRes.Value)
- }
- if *sumAggRes.Value != 120.0 {
- t.Errorf("expected %v; got: %v", 120.0, *sumAggRes.Value)
- }
- // statsRetweets
- statsAggRes, found := agg.Stats("statsRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if statsAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if statsAggRes.Count != 3 {
- t.Errorf("expected %d; got: %d", 3, statsAggRes.Count)
- }
- if statsAggRes.Min == nil {
- t.Fatalf("expected != nil; got: %v", *statsAggRes.Min)
- }
- if *statsAggRes.Min != 0.0 {
- t.Errorf("expected %v; got: %v", 0.0, *statsAggRes.Min)
- }
- if statsAggRes.Max == nil {
- t.Fatalf("expected != nil; got: %v", *statsAggRes.Max)
- }
- if *statsAggRes.Max != 108.0 {
- t.Errorf("expected %v; got: %v", 108.0, *statsAggRes.Max)
- }
- if statsAggRes.Avg == nil {
- t.Fatalf("expected != nil; got: %v", *statsAggRes.Avg)
- }
- if *statsAggRes.Avg != 40.0 {
- t.Errorf("expected %v; got: %v", 40.0, *statsAggRes.Avg)
- }
- if statsAggRes.Sum == nil {
- t.Fatalf("expected != nil; got: %v", *statsAggRes.Sum)
- }
- if *statsAggRes.Sum != 120.0 {
- t.Errorf("expected %v; got: %v", 120.0, *statsAggRes.Sum)
- }
- // extstatsRetweets
- extStatsAggRes, found := agg.ExtendedStats("extstatsRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if extStatsAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if extStatsAggRes.Count != 3 {
- t.Errorf("expected %d; got: %d", 3, extStatsAggRes.Count)
- }
- if extStatsAggRes.Min == nil {
- t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Min)
- }
- if *extStatsAggRes.Min != 0.0 {
- t.Errorf("expected %v; got: %v", 0.0, *extStatsAggRes.Min)
- }
- if extStatsAggRes.Max == nil {
- t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Max)
- }
- if *extStatsAggRes.Max != 108.0 {
- t.Errorf("expected %v; got: %v", 108.0, *extStatsAggRes.Max)
- }
- if extStatsAggRes.Avg == nil {
- t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Avg)
- }
- if *extStatsAggRes.Avg != 40.0 {
- t.Errorf("expected %v; got: %v", 40.0, *extStatsAggRes.Avg)
- }
- if extStatsAggRes.Sum == nil {
- t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Sum)
- }
- if *extStatsAggRes.Sum != 120.0 {
- t.Errorf("expected %v; got: %v", 120.0, *extStatsAggRes.Sum)
- }
- if extStatsAggRes.SumOfSquares == nil {
- t.Fatalf("expected != nil; got: %v", *extStatsAggRes.SumOfSquares)
- }
- if *extStatsAggRes.SumOfSquares != 11808.0 {
- t.Errorf("expected %v; got: %v", 11808.0, *extStatsAggRes.SumOfSquares)
- }
- if extStatsAggRes.Variance == nil {
- t.Fatalf("expected != nil; got: %v", *extStatsAggRes.Variance)
- }
- if *extStatsAggRes.Variance != 2336.0 {
- t.Errorf("expected %v; got: %v", 2336.0, *extStatsAggRes.Variance)
- }
- if extStatsAggRes.StdDeviation == nil {
- t.Fatalf("expected != nil; got: %v", *extStatsAggRes.StdDeviation)
- }
- if *extStatsAggRes.StdDeviation != 48.33218389437829 {
- t.Errorf("expected %v; got: %v", 48.33218389437829, *extStatsAggRes.StdDeviation)
- }
- // valueCountRetweets
- valueCountAggRes, found := agg.ValueCount("valueCountRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if valueCountAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if valueCountAggRes.Value == nil {
- t.Fatalf("expected != nil; got: %v", *valueCountAggRes.Value)
- }
- if *valueCountAggRes.Value != 3.0 {
- t.Errorf("expected %v; got: %v", 3.0, *valueCountAggRes.Value)
- }
- // percentilesRetweets
- percentilesAggRes, found := agg.Percentiles("percentilesRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if percentilesAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- // ES 1.4.x returns 7: {"1.0":...}
- // ES 1.5.0 returns 14: {"1.0":..., "1.0_as_string":...}
- // So we're relaxing the test here.
- if len(percentilesAggRes.Values) == 0 {
- t.Errorf("expected at least %d value; got: %d\nValues are: %#v", 1, len(percentilesAggRes.Values), percentilesAggRes.Values)
- }
- if _, found := percentilesAggRes.Values["0.0"]; found {
- t.Errorf("expected %v; got: %v", false, found)
- }
- if percentilesAggRes.Values["1.0"] != 0.24 {
- t.Errorf("expected %v; got: %v", 0.24, percentilesAggRes.Values["1.0"])
- }
- if percentilesAggRes.Values["25.0"] != 6.0 {
- t.Errorf("expected %v; got: %v", 6.0, percentilesAggRes.Values["25.0"])
- }
- if percentilesAggRes.Values["99.0"] != 106.08 {
- t.Errorf("expected %v; got: %v", 106.08, percentilesAggRes.Values["99.0"])
- }
- // percentileRanksRetweets
- percentileRanksAggRes, found := agg.PercentileRanks("percentileRanksRetweets")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if percentileRanksAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(percentileRanksAggRes.Values) == 0 {
- t.Errorf("expected at least %d value; got %d\nValues are: %#v", 1, len(percentileRanksAggRes.Values), percentileRanksAggRes.Values)
- }
- if _, found := percentileRanksAggRes.Values["0.0"]; found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if percentileRanksAggRes.Values["25.0"] != 21.180555555555557 {
- t.Errorf("expected %v; got: %v", 21.180555555555557, percentileRanksAggRes.Values["25.0"])
- }
- if percentileRanksAggRes.Values["50.0"] != 29.86111111111111 {
- t.Errorf("expected %v; got: %v", 29.86111111111111, percentileRanksAggRes.Values["50.0"])
- }
- if percentileRanksAggRes.Values["75.0"] != 38.54166666666667 {
- t.Errorf("expected %v; got: %v", 38.54166666666667, percentileRanksAggRes.Values["75.0"])
- }
- // usersCardinality
- cardAggRes, found := agg.Cardinality("usersCardinality")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if cardAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if cardAggRes.Value == nil {
- t.Fatalf("expected != nil; got: %v", *cardAggRes.Value)
- }
- if *cardAggRes.Value != 2 {
- t.Errorf("expected %v; got: %v", 2, *cardAggRes.Value)
- }
- // retweetsFilter
- filterAggRes, found := agg.Filter("retweetsFilter")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if filterAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if filterAggRes.DocCount != 2 {
- t.Fatalf("expected %v; got: %v", 2, filterAggRes.DocCount)
- }
- // Retrieve sub-aggregation
- avgRetweetsAggRes, found := filterAggRes.Avg("avgRetweetsSub")
- if !found {
- t.Error("expected sub-aggregation \"avgRetweets\" to be found; got false")
- }
- if avgRetweetsAggRes == nil {
- t.Fatal("expected sub-aggregation \"avgRetweets\"; got nil")
- }
- if avgRetweetsAggRes.Value == nil {
- t.Fatalf("expected != nil; got: %v", avgRetweetsAggRes.Value)
- }
- if *avgRetweetsAggRes.Value != 54.0 {
- t.Errorf("expected %v; got: %v", 54.0, *avgRetweetsAggRes.Value)
- }
- // queryFilter
- queryFilterAggRes, found := agg.Filter("queryFilter")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if queryFilterAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if queryFilterAggRes.DocCount != 2 {
- t.Fatalf("expected %v; got: %v", 2, queryFilterAggRes.DocCount)
- }
- // significantTerms
- stAggRes, found := agg.SignificantTerms("significantTerms")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if stAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if stAggRes.DocCount != 3 {
- t.Errorf("expected %v; got: %v", 3, stAggRes.DocCount)
- }
- if len(stAggRes.Buckets) != 0 {
- t.Errorf("expected %v; got: %v", 0, len(stAggRes.Buckets))
- }
- // sampler
- samplerAggRes, found := agg.Sampler("sample")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if samplerAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if samplerAggRes.DocCount != 3 {
- t.Errorf("expected %v; got: %v", 3, samplerAggRes.DocCount)
- }
- sub, found := samplerAggRes.Aggregations["tagged_with"]
- if !found {
- t.Fatalf("expected sub aggregation %q", "tagged_with")
- }
- if sub == nil {
- t.Fatalf("expected sub aggregation %q; got: %v", "tagged_with", sub)
- }
- // retweetsRange
- rangeAggRes, found := agg.Range("retweetsRange")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if rangeAggRes == nil {
- t.Fatal("expected != nil; got: nil")
- }
- if len(rangeAggRes.Buckets) != 3 {
- t.Fatalf("expected %d; got: %d", 3, len(rangeAggRes.Buckets))
- }
- if rangeAggRes.Buckets[0].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[0].DocCount)
- }
- if rangeAggRes.Buckets[1].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[1].DocCount)
- }
- if rangeAggRes.Buckets[2].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, rangeAggRes.Buckets[2].DocCount)
- }
- // retweetsKeyedRange
- keyedRangeAggRes, found := agg.KeyedRange("retweetsKeyedRange")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if keyedRangeAggRes == nil {
- t.Fatal("expected != nil; got: nil")
- }
- if len(keyedRangeAggRes.Buckets) != 3 {
- t.Fatalf("expected %d; got: %d", 3, len(keyedRangeAggRes.Buckets))
- }
- _, found = keyedRangeAggRes.Buckets["no-such-key"]
- if found {
- t.Fatalf("expected bucket to not be found; got: %v", found)
- }
- bucket, found := keyedRangeAggRes.Buckets["*-10.0"]
- if !found {
- t.Fatalf("expected bucket to be found; got: %v", found)
- }
- if bucket.DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
- }
- bucket, found = keyedRangeAggRes.Buckets["10.0-100.0"]
- if !found {
- t.Fatalf("expected bucket to be found; got: %v", found)
- }
- if bucket.DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
- }
- bucket, found = keyedRangeAggRes.Buckets["100.0-*"]
- if !found {
- t.Fatalf("expected bucket to be found; got: %v", found)
- }
- if bucket.DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
- }
- // dateRange
- dateRangeRes, found := agg.DateRange("dateRange")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if dateRangeRes == nil {
- t.Fatal("expected != nil; got: nil")
- }
- if dateRangeRes.Buckets[0].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, dateRangeRes.Buckets[0].DocCount)
- }
- if dateRangeRes.Buckets[0].From != nil {
- t.Fatal("expected From to be nil")
- }
- if dateRangeRes.Buckets[0].To == nil {
- t.Fatal("expected To to be != nil")
- }
- if *dateRangeRes.Buckets[0].To != 1.325376e+12 {
- t.Errorf("expected %v; got: %v", 1.325376e+12, *dateRangeRes.Buckets[0].To)
- }
- if dateRangeRes.Buckets[0].ToAsString != "2012-01-01T00:00:00.000Z" {
- t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", dateRangeRes.Buckets[0].ToAsString)
- }
- if dateRangeRes.Buckets[1].DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, dateRangeRes.Buckets[1].DocCount)
- }
- if dateRangeRes.Buckets[1].From == nil {
- t.Fatal("expected From to be != nil")
- }
- if *dateRangeRes.Buckets[1].From != 1.325376e+12 {
- t.Errorf("expected From = %v; got: %v", 1.325376e+12, *dateRangeRes.Buckets[1].From)
- }
- if dateRangeRes.Buckets[1].FromAsString != "2012-01-01T00:00:00.000Z" {
- t.Errorf("expected FromAsString = %q; got: %q", "2012-01-01T00:00:00.000Z", dateRangeRes.Buckets[1].FromAsString)
- }
- if dateRangeRes.Buckets[1].To == nil {
- t.Fatal("expected To to be != nil")
- }
- if *dateRangeRes.Buckets[1].To != 1.3569984e+12 {
- t.Errorf("expected To = %v; got: %v", 1.3569984e+12, *dateRangeRes.Buckets[1].To)
- }
- if dateRangeRes.Buckets[1].ToAsString != "2013-01-01T00:00:00.000Z" {
- t.Errorf("expected ToAsString = %q; got: %q", "2013-01-01T00:00:00.000Z", dateRangeRes.Buckets[1].ToAsString)
- }
- if dateRangeRes.Buckets[2].DocCount != 0 {
- t.Errorf("expected %d; got: %d", 0, dateRangeRes.Buckets[2].DocCount)
- }
- if dateRangeRes.Buckets[2].To != nil {
- t.Fatal("expected To to be nil")
- }
- if dateRangeRes.Buckets[2].From == nil {
- t.Fatal("expected From to be != nil")
- }
- if *dateRangeRes.Buckets[2].From != 1.3569984e+12 {
- t.Errorf("expected %v; got: %v", 1.3569984e+12, *dateRangeRes.Buckets[2].From)
- }
- if dateRangeRes.Buckets[2].FromAsString != "2013-01-01T00:00:00.000Z" {
- t.Errorf("expected %q; got: %q", "2013-01-01T00:00:00.000Z", dateRangeRes.Buckets[2].FromAsString)
- }
- // missingTags
- missingRes, found := agg.Missing("missingTags")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if missingRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if missingRes.DocCount != 0 {
- t.Errorf("expected searchResult.Aggregations[\"missingTags\"].DocCount = %v; got %v", 0, missingRes.DocCount)
- }
- // retweetsHisto
- histoRes, found := agg.Histogram("retweetsHisto")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if histoRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(histoRes.Buckets) != 2 {
- t.Fatalf("expected %d; got: %d", 2, len(histoRes.Buckets))
- }
- if histoRes.Buckets[0].DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, histoRes.Buckets[0].DocCount)
- }
- if histoRes.Buckets[0].Key != 0.0 {
- t.Errorf("expected %v; got: %v", 0.0, histoRes.Buckets[0].Key)
- }
- if histoRes.Buckets[1].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, histoRes.Buckets[1].DocCount)
- }
- if histoRes.Buckets[1].Key != 100.0 {
- t.Errorf("expected %v; got: %+v", 100.0, histoRes.Buckets[1].Key)
- }
- // dateHisto
- dateHistoRes, found := agg.DateHistogram("dateHisto")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if dateHistoRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(dateHistoRes.Buckets) != 2 {
- t.Fatalf("expected %d; got: %d", 2, len(dateHistoRes.Buckets))
- }
- if dateHistoRes.Buckets[0].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, dateHistoRes.Buckets[0].DocCount)
- }
- if dateHistoRes.Buckets[0].Key != 1.29384e+12 {
- t.Errorf("expected %v; got: %v", 1.29384e+12, dateHistoRes.Buckets[0].Key)
- }
- if dateHistoRes.Buckets[0].KeyAsString == nil {
- t.Fatalf("expected != nil; got: %v", dateHistoRes.Buckets[0].KeyAsString)
- }
- if *dateHistoRes.Buckets[0].KeyAsString != "2011-01-01T00:00:00.000Z" {
- t.Errorf("expected %q; got: %q", "2011-01-01T00:00:00.000Z", *dateHistoRes.Buckets[0].KeyAsString)
- }
- if dateHistoRes.Buckets[1].DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, dateHistoRes.Buckets[1].DocCount)
- }
- if dateHistoRes.Buckets[1].Key != 1.325376e+12 {
- t.Errorf("expected %v; got: %v", 1.325376e+12, dateHistoRes.Buckets[1].Key)
- }
- if dateHistoRes.Buckets[1].KeyAsString == nil {
- t.Fatalf("expected != nil; got: %v", dateHistoRes.Buckets[1].KeyAsString)
- }
- if *dateHistoRes.Buckets[1].KeyAsString != "2012-01-01T00:00:00.000Z" {
- t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", *dateHistoRes.Buckets[1].KeyAsString)
- }
- // topHits
- topTags, found := agg.Terms("top-tags")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if topTags == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if topTags.DocCountErrorUpperBound != 0 {
- t.Errorf("expected %v; got: %v", 0, topTags.DocCountErrorUpperBound)
- }
- if topTags.SumOfOtherDocCount != 1 {
- t.Errorf("expected %v; got: %v", 1, topTags.SumOfOtherDocCount)
- }
- if len(topTags.Buckets) != 3 {
- t.Fatalf("expected %d; got: %d", 3, len(topTags.Buckets))
- }
- if topTags.Buckets[0].DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, topTags.Buckets[0].DocCount)
- }
- if topTags.Buckets[0].Key != "golang" {
- t.Errorf("expected %v; got: %v", "golang", topTags.Buckets[0].Key)
- }
- topHits, found := topTags.Buckets[0].TopHits("top_tag_hits")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if topHits == nil {
- t.Fatal("expected != nil; got: nil")
- }
- if topHits.Hits == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if topHits.Hits.TotalHits != 2 {
- t.Errorf("expected %d; got: %d", 2, topHits.Hits.TotalHits)
- }
- if topHits.Hits.Hits == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(topHits.Hits.Hits) != 2 {
- t.Fatalf("expected %d; got: %d", 2, len(topHits.Hits.Hits))
- }
- hit := topHits.Hits.Hits[0]
- if !found {
- t.Fatalf("expected %v; got: %v", true, found)
- }
- if hit == nil {
- t.Fatal("expected != nil; got: nil")
- }
- var tw tweet
- if err := json.Unmarshal(*hit.Source, &tw); err != nil {
- t.Fatalf("expected no error; got: %v", err)
- }
- if tw.Message != "Welcome to Golang and Elasticsearch." {
- t.Errorf("expected %q; got: %q", "Welcome to Golang and Elasticsearch.", tw.Message)
- }
- if topTags.Buckets[1].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, topTags.Buckets[1].DocCount)
- }
- if topTags.Buckets[1].Key != "cycling" {
- t.Errorf("expected %v; got: %v", "cycling", topTags.Buckets[1].Key)
- }
- topHits, found = topTags.Buckets[1].TopHits("top_tag_hits")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if topHits == nil {
- t.Fatal("expected != nil; got: nil")
- }
- if topHits.Hits == nil {
- t.Fatal("expected != nil; got nil")
- }
- if topHits.Hits.TotalHits != 1 {
- t.Errorf("expected %d; got: %d", 1, topHits.Hits.TotalHits)
- }
- if topTags.Buckets[2].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, topTags.Buckets[2].DocCount)
- }
- if topTags.Buckets[2].Key != "elasticsearch" {
- t.Errorf("expected %v; got: %v", "elasticsearch", topTags.Buckets[2].Key)
- }
- topHits, found = topTags.Buckets[2].TopHits("top_tag_hits")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if topHits == nil {
- t.Fatal("expected != nil; got: nil")
- }
- if topHits.Hits == nil {
- t.Fatal("expected != nil; got: nil")
- }
- if topHits.Hits.TotalHits != 1 {
- t.Errorf("expected %d; got: %d", 1, topHits.Hits.TotalHits)
- }
- // viewport via geo_bounds (1.3.0 has an error in that it doesn't output the aggregation name)
- geoBoundsRes, found := agg.GeoBounds("viewport")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if geoBoundsRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- // geohashed via geohash
- geoHashRes, found := agg.GeoHash("geohashed")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if geoHashRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- // geo_centroid
- geoCentroidRes, found := agg.GeoCentroid("centroid")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if geoCentroidRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- // Filters agg "countByUser" (unnamed)
- countByUserAggRes, found := agg.Filters("countByUser")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if countByUserAggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(countByUserAggRes.Buckets) != 2 {
- t.Fatalf("expected %d; got: %d", 2, len(countByUserAggRes.Buckets))
- }
- if len(countByUserAggRes.NamedBuckets) != 0 {
- t.Fatalf("expected %d; got: %d", 0, len(countByUserAggRes.NamedBuckets))
- }
- if countByUserAggRes.Buckets[0].DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, countByUserAggRes.Buckets[0].DocCount)
- }
- if countByUserAggRes.Buckets[1].DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, countByUserAggRes.Buckets[1].DocCount)
- }
- // Filters agg "countByUser2" (named)
- countByUser2AggRes, found := agg.Filters("countByUser2")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if countByUser2AggRes == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(countByUser2AggRes.Buckets) != 0 {
- t.Fatalf("expected %d; got: %d", 0, len(countByUser2AggRes.Buckets))
- }
- if len(countByUser2AggRes.NamedBuckets) != 2 {
- t.Fatalf("expected %d; got: %d", 2, len(countByUser2AggRes.NamedBuckets))
- }
- b, found := countByUser2AggRes.NamedBuckets["olivere"]
- if !found {
- t.Fatalf("expected bucket %q; got: %v", "olivere", found)
- }
- if b == nil {
- t.Fatalf("expected bucket %q; got: %v", "olivere", b)
- }
- if b.DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, b.DocCount)
- }
- b, found = countByUser2AggRes.NamedBuckets["sandrae"]
- if !found {
- t.Fatalf("expected bucket %q; got: %v", "sandrae", found)
- }
- if b == nil {
- t.Fatalf("expected bucket %q; got: %v", "sandrae", b)
- }
- if b.DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, b.DocCount)
- }
- }
- // TestAggsMarshal ensures that marshaling aggregations back into a string
- // does not yield base64 encoded data. See https://github.com/olivere/elastic/issues/51
- // and https://groups.google.com/forum/#!topic/Golang-Nuts/38ShOlhxAYY for details.
- func TestAggsMarshal(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere",
- Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Image: "http://golang.org/doc/gopher/gophercolor.png",
- Tags: []string{"golang", "elasticsearch"},
- Location: "48.1333,11.5667", // lat,lon
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- // Add all documents
- _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Flush().Index(testIndexName).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- // Match all should return all documents
- all := NewMatchAllQuery()
- dhagg := NewDateHistogramAggregation().Field("created").Interval("year")
- // Run query
- builder := client.Search().Index(testIndexName).Query(all)
- builder = builder.Aggregation("dhagg", dhagg)
- searchResult, err := builder.Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.TotalHits() != 1 {
- t.Errorf("expected Hits.TotalHits = %d; got: %d", 1, searchResult.TotalHits())
- }
- if _, found := searchResult.Aggregations["dhagg"]; !found {
- t.Fatalf("expected aggregation %q", "dhagg")
- }
- buf, err := json.Marshal(searchResult)
- if err != nil {
- t.Fatal(err)
- }
- s := string(buf)
- if i := strings.Index(s, `{"dhagg":{"buckets":[{"key_as_string":"2012-01-01`); i < 0 {
- t.Errorf("expected to serialize aggregation into string; got: %v", s)
- }
- }
- func TestAggsMetricsMin(t *testing.T) {
- s := `{
- "min_price": {
- "value": 10
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Min("min_price")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(10) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(10), *agg.Value)
- }
- }
- func TestAggsMetricsMax(t *testing.T) {
- s := `{
- "max_price": {
- "value": 35
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Max("max_price")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(35) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(35), *agg.Value)
- }
- }
- func TestAggsMetricsSum(t *testing.T) {
- s := `{
- "intraday_return": {
- "value": 2.18
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Sum("intraday_return")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(2.18) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(2.18), *agg.Value)
- }
- }
- func TestAggsMetricsAvg(t *testing.T) {
- s := `{
- "avg_grade": {
- "value": 75
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Avg("avg_grade")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(75) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(75), *agg.Value)
- }
- }
- func TestAggsMetricsValueCount(t *testing.T) {
- s := `{
- "grades_count": {
- "value": 10
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.ValueCount("grades_count")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(10) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(10), *agg.Value)
- }
- }
- func TestAggsMetricsCardinality(t *testing.T) {
- s := `{
- "author_count": {
- "value": 12
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Cardinality("author_count")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(12) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(12), *agg.Value)
- }
- }
- func TestAggsMetricsStats(t *testing.T) {
- s := `{
- "grades_stats": {
- "count": 6,
- "min": 60,
- "max": 98,
- "avg": 78.5,
- "sum": 471
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Stats("grades_stats")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Count != int64(6) {
- t.Fatalf("expected aggregation Count = %v; got: %v", int64(6), agg.Count)
- }
- if agg.Min == nil {
- t.Fatalf("expected aggregation Min != nil; got: %v", agg.Min)
- }
- if *agg.Min != float64(60) {
- t.Fatalf("expected aggregation Min = %v; got: %v", float64(60), *agg.Min)
- }
- if agg.Max == nil {
- t.Fatalf("expected aggregation Max != nil; got: %v", agg.Max)
- }
- if *agg.Max != float64(98) {
- t.Fatalf("expected aggregation Max = %v; got: %v", float64(98), *agg.Max)
- }
- if agg.Avg == nil {
- t.Fatalf("expected aggregation Avg != nil; got: %v", agg.Avg)
- }
- if *agg.Avg != float64(78.5) {
- t.Fatalf("expected aggregation Avg = %v; got: %v", float64(78.5), *agg.Avg)
- }
- if agg.Sum == nil {
- t.Fatalf("expected aggregation Sum != nil; got: %v", agg.Sum)
- }
- if *agg.Sum != float64(471) {
- t.Fatalf("expected aggregation Sum = %v; got: %v", float64(471), *agg.Sum)
- }
- }
- func TestAggsMetricsExtendedStats(t *testing.T) {
- s := `{
- "grades_stats": {
- "count": 6,
- "min": 72,
- "max": 117.6,
- "avg": 94.2,
- "sum": 565.2,
- "sum_of_squares": 54551.51999999999,
- "variance": 218.2799999999976,
- "std_deviation": 14.774302013969987
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.ExtendedStats("grades_stats")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Count != int64(6) {
- t.Fatalf("expected aggregation Count = %v; got: %v", int64(6), agg.Count)
- }
- if agg.Min == nil {
- t.Fatalf("expected aggregation Min != nil; got: %v", agg.Min)
- }
- if *agg.Min != float64(72) {
- t.Fatalf("expected aggregation Min = %v; got: %v", float64(72), *agg.Min)
- }
- if agg.Max == nil {
- t.Fatalf("expected aggregation Max != nil; got: %v", agg.Max)
- }
- if *agg.Max != float64(117.6) {
- t.Fatalf("expected aggregation Max = %v; got: %v", float64(117.6), *agg.Max)
- }
- if agg.Avg == nil {
- t.Fatalf("expected aggregation Avg != nil; got: %v", agg.Avg)
- }
- if *agg.Avg != float64(94.2) {
- t.Fatalf("expected aggregation Avg = %v; got: %v", float64(94.2), *agg.Avg)
- }
- if agg.Sum == nil {
- t.Fatalf("expected aggregation Sum != nil; got: %v", agg.Sum)
- }
- if *agg.Sum != float64(565.2) {
- t.Fatalf("expected aggregation Sum = %v; got: %v", float64(565.2), *agg.Sum)
- }
- if agg.SumOfSquares == nil {
- t.Fatalf("expected aggregation sum_of_squares != nil; got: %v", agg.SumOfSquares)
- }
- if *agg.SumOfSquares != float64(54551.51999999999) {
- t.Fatalf("expected aggregation sum_of_squares = %v; got: %v", float64(54551.51999999999), *agg.SumOfSquares)
- }
- if agg.Variance == nil {
- t.Fatalf("expected aggregation Variance != nil; got: %v", agg.Variance)
- }
- if *agg.Variance != float64(218.2799999999976) {
- t.Fatalf("expected aggregation Variance = %v; got: %v", float64(218.2799999999976), *agg.Variance)
- }
- if agg.StdDeviation == nil {
- t.Fatalf("expected aggregation StdDeviation != nil; got: %v", agg.StdDeviation)
- }
- if *agg.StdDeviation != float64(14.774302013969987) {
- t.Fatalf("expected aggregation StdDeviation = %v; got: %v", float64(14.774302013969987), *agg.StdDeviation)
- }
- }
- func TestAggsMatrixStats(t *testing.T) {
- s := `{
- "matrixstats": {
- "fields": [{
- "name": "income",
- "count": 50,
- "mean": 51985.1,
- "variance": 7.383377037755103E7,
- "skewness": 0.5595114003506483,
- "kurtosis": 2.5692365287787124,
- "covariance": {
- "income": 7.383377037755103E7,
- "poverty": -21093.65836734694
- },
- "correlation": {
- "income": 1.0,
- "poverty": -0.8352655256272504
- }
- }, {
- "name": "poverty",
- "count": 51,
- "mean": 12.732000000000001,
- "variance": 8.637730612244896,
- "skewness": 0.4516049811903419,
- "kurtosis": 2.8615929677997767,
- "covariance": {
- "income": -21093.65836734694,
- "poverty": 8.637730612244896
- },
- "correlation": {
- "income": -0.8352655256272504,
- "poverty": 1.0
- }
- }]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.MatrixStats("matrixstats")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if want, got := 2, len(agg.Fields); want != got {
- t.Fatalf("expected aggregaton len(Fields) = %v; got: %v", want, got)
- }
- field := agg.Fields[0]
- if want, got := "income", field.Name; want != got {
- t.Fatalf("expected aggregation field name == %q; got: %q", want, got)
- }
- if want, got := int64(50), field.Count; want != got {
- t.Fatalf("expected aggregation field count == %v; got: %v", want, got)
- }
- if want, got := 51985.1, field.Mean; want != got {
- t.Fatalf("expected aggregation field mean == %v; got: %v", want, got)
- }
- if want, got := 7.383377037755103e7, field.Variance; want != got {
- t.Fatalf("expected aggregation field variance == %v; got: %v", want, got)
- }
- if want, got := 0.5595114003506483, field.Skewness; want != got {
- t.Fatalf("expected aggregation field skewness == %v; got: %v", want, got)
- }
- if want, got := 2.5692365287787124, field.Kurtosis; want != got {
- t.Fatalf("expected aggregation field kurtosis == %v; got: %v", want, got)
- }
- if field.Covariance == nil {
- t.Fatalf("expected aggregation field covariance != nil; got: %v", nil)
- }
- if want, got := 7.383377037755103e7, field.Covariance["income"]; want != got {
- t.Fatalf("expected aggregation field covariance == %v; got: %v", want, got)
- }
- if want, got := -21093.65836734694, field.Covariance["poverty"]; want != got {
- t.Fatalf("expected aggregation field covariance == %v; got: %v", want, got)
- }
- if field.Correlation == nil {
- t.Fatalf("expected aggregation field correlation != nil; got: %v", nil)
- }
- if want, got := 1.0, field.Correlation["income"]; want != got {
- t.Fatalf("expected aggregation field correlation == %v; got: %v", want, got)
- }
- if want, got := -0.8352655256272504, field.Correlation["poverty"]; want != got {
- t.Fatalf("expected aggregation field correlation == %v; got: %v", want, got)
- }
- field = agg.Fields[1]
- if want, got := "poverty", field.Name; want != got {
- t.Fatalf("expected aggregation field name == %q; got: %q", want, got)
- }
- if want, got := int64(51), field.Count; want != got {
- t.Fatalf("expected aggregation field count == %v; got: %v", want, got)
- }
- }
- func TestAggsMetricsPercentiles(t *testing.T) {
- s := `{
- "load_time_outlier": {
- "values" : {
- "1.0": 15,
- "5.0": 20,
- "25.0": 23,
- "50.0": 25,
- "75.0": 29,
- "95.0": 60,
- "99.0": 150
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Percentiles("load_time_outlier")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Values == nil {
- t.Fatalf("expected aggregation Values != nil; got: %v", agg.Values)
- }
- if len(agg.Values) != 7 {
- t.Fatalf("expected %d aggregation Values; got: %d", 7, len(agg.Values))
- }
- if agg.Values["1.0"] != float64(15) {
- t.Errorf("expected aggregation value for \"1.0\" = %v; got: %v", float64(15), agg.Values["1.0"])
- }
- if agg.Values["5.0"] != float64(20) {
- t.Errorf("expected aggregation value for \"5.0\" = %v; got: %v", float64(20), agg.Values["5.0"])
- }
- if agg.Values["25.0"] != float64(23) {
- t.Errorf("expected aggregation value for \"25.0\" = %v; got: %v", float64(23), agg.Values["25.0"])
- }
- if agg.Values["50.0"] != float64(25) {
- t.Errorf("expected aggregation value for \"50.0\" = %v; got: %v", float64(25), agg.Values["50.0"])
- }
- if agg.Values["75.0"] != float64(29) {
- t.Errorf("expected aggregation value for \"75.0\" = %v; got: %v", float64(29), agg.Values["75.0"])
- }
- if agg.Values["95.0"] != float64(60) {
- t.Errorf("expected aggregation value for \"95.0\" = %v; got: %v", float64(60), agg.Values["95.0"])
- }
- if agg.Values["99.0"] != float64(150) {
- t.Errorf("expected aggregation value for \"99.0\" = %v; got: %v", float64(150), agg.Values["99.0"])
- }
- }
- func TestAggsMetricsPercentileRanks(t *testing.T) {
- s := `{
- "load_time_outlier": {
- "values" : {
- "15": 92,
- "30": 100
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.PercentileRanks("load_time_outlier")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Values == nil {
- t.Fatalf("expected aggregation Values != nil; got: %v", agg.Values)
- }
- if len(agg.Values) != 2 {
- t.Fatalf("expected %d aggregation Values; got: %d", 7, len(agg.Values))
- }
- if agg.Values["15"] != float64(92) {
- t.Errorf("expected aggregation value for \"15\" = %v; got: %v", float64(92), agg.Values["15"])
- }
- if agg.Values["30"] != float64(100) {
- t.Errorf("expected aggregation value for \"30\" = %v; got: %v", float64(100), agg.Values["30"])
- }
- }
- func TestAggsMetricsTopHits(t *testing.T) {
- s := `{
- "top-tags": {
- "buckets": [
- {
- "key": "windows-7",
- "doc_count": 25365,
- "top_tags_hits": {
- "hits": {
- "total": 25365,
- "max_score": 1,
- "hits": [
- {
- "_index": "stack",
- "_type": "question",
- "_id": "602679",
- "_score": 1,
- "_source": {
- "title": "Windows port opening"
- },
- "sort": [
- 1370143231177
- ]
- }
- ]
- }
- }
- },
- {
- "key": "linux",
- "doc_count": 18342,
- "top_tags_hits": {
- "hits": {
- "total": 18342,
- "max_score": 1,
- "hits": [
- {
- "_index": "stack",
- "_type": "question",
- "_id": "602672",
- "_score": 1,
- "_source": {
- "title": "Ubuntu RFID Screensaver lock-unlock"
- },
- "sort": [
- 1370143379747
- ]
- }
- ]
- }
- }
- },
- {
- "key": "windows",
- "doc_count": 18119,
- "top_tags_hits": {
- "hits": {
- "total": 18119,
- "max_score": 1,
- "hits": [
- {
- "_index": "stack",
- "_type": "question",
- "_id": "602678",
- "_score": 1,
- "_source": {
- "title": "If I change my computers date / time, what could be affected?"
- },
- "sort": [
- 1370142868283
- ]
- }
- ]
- }
- }
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Terms("top-tags")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 3 {
- t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != "windows-7" {
- t.Errorf("expected bucket key = %q; got: %q", "windows-7", agg.Buckets[0].Key)
- }
- if agg.Buckets[1].Key != "linux" {
- t.Errorf("expected bucket key = %q; got: %q", "linux", agg.Buckets[1].Key)
- }
- if agg.Buckets[2].Key != "windows" {
- t.Errorf("expected bucket key = %q; got: %q", "windows", agg.Buckets[2].Key)
- }
- // Sub-aggregation of top-hits
- subAgg, found := agg.Buckets[0].TopHits("top_tags_hits")
- if !found {
- t.Fatalf("expected sub aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
- }
- if subAgg.Hits == nil {
- t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
- }
- if subAgg.Hits.TotalHits != 25365 {
- t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 25365, subAgg.Hits.TotalHits)
- }
- if subAgg.Hits.MaxScore == nil {
- t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
- }
- if *subAgg.Hits.MaxScore != float64(1.0) {
- t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
- }
- subAgg, found = agg.Buckets[1].TopHits("top_tags_hits")
- if !found {
- t.Fatalf("expected sub aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
- }
- if subAgg.Hits == nil {
- t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
- }
- if subAgg.Hits.TotalHits != 18342 {
- t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 18342, subAgg.Hits.TotalHits)
- }
- if subAgg.Hits.MaxScore == nil {
- t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
- }
- if *subAgg.Hits.MaxScore != float64(1.0) {
- t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
- }
- subAgg, found = agg.Buckets[2].TopHits("top_tags_hits")
- if !found {
- t.Fatalf("expected sub aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub aggregation != nil; got: %v", subAgg)
- }
- if subAgg.Hits == nil {
- t.Fatalf("expected sub aggregation Hits != nil; got: %v", subAgg.Hits)
- }
- if subAgg.Hits.TotalHits != 18119 {
- t.Fatalf("expected sub aggregation Hits.TotalHits = %d; got: %d", 18119, subAgg.Hits.TotalHits)
- }
- if subAgg.Hits.MaxScore == nil {
- t.Fatalf("expected sub aggregation Hits.MaxScore != %v; got: %v", nil, *subAgg.Hits.MaxScore)
- }
- if *subAgg.Hits.MaxScore != float64(1.0) {
- t.Fatalf("expected sub aggregation Hits.MaxScore = %v; got: %v", float64(1.0), *subAgg.Hits.MaxScore)
- }
- }
- func TestAggsBucketGlobal(t *testing.T) {
- s := `{
- "all_products" : {
- "doc_count" : 100,
- "avg_price" : {
- "value" : 56.3
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Global("all_products")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 100 {
- t.Fatalf("expected aggregation DocCount = %d; got: %d", 100, agg.DocCount)
- }
- // Sub-aggregation
- subAgg, found := agg.Avg("avg_price")
- if !found {
- t.Fatalf("expected sub-aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
- }
- if subAgg.Value == nil {
- t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
- }
- if *subAgg.Value != float64(56.3) {
- t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(56.3), *subAgg.Value)
- }
- }
- func TestAggsBucketFilter(t *testing.T) {
- s := `{
- "in_stock_products" : {
- "doc_count" : 100,
- "avg_price" : { "value" : 56.3 }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Filter("in_stock_products")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 100 {
- t.Fatalf("expected aggregation DocCount = %d; got: %d", 100, agg.DocCount)
- }
- // Sub-aggregation
- subAgg, found := agg.Avg("avg_price")
- if !found {
- t.Fatalf("expected sub-aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
- }
- if subAgg.Value == nil {
- t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
- }
- if *subAgg.Value != float64(56.3) {
- t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(56.3), *subAgg.Value)
- }
- }
- func TestAggsBucketFiltersWithBuckets(t *testing.T) {
- s := `{
- "messages" : {
- "buckets" : [
- {
- "doc_count" : 34,
- "monthly" : {
- "buckets" : []
- }
- },
- {
- "doc_count" : 439,
- "monthly" : {
- "buckets" : []
- }
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Filters("messages")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != %v; got: %v", nil, agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Fatalf("expected %d buckets; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].DocCount != 34 {
- t.Fatalf("expected DocCount = %d; got: %d", 34, agg.Buckets[0].DocCount)
- }
- subAgg, found := agg.Buckets[0].Histogram("monthly")
- if !found {
- t.Fatalf("expected sub aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
- }
- if agg.Buckets[1].DocCount != 439 {
- t.Fatalf("expected DocCount = %d; got: %d", 439, agg.Buckets[1].DocCount)
- }
- subAgg, found = agg.Buckets[1].Histogram("monthly")
- if !found {
- t.Fatalf("expected sub aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
- }
- }
- func TestAggsBucketFiltersWithNamedBuckets(t *testing.T) {
- s := `{
- "messages" : {
- "buckets" : {
- "errors" : {
- "doc_count" : 34,
- "monthly" : {
- "buckets" : []
- }
- },
- "warnings" : {
- "doc_count" : 439,
- "monthly" : {
- "buckets" : []
- }
- }
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Filters("messages")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.NamedBuckets == nil {
- t.Fatalf("expected aggregation buckets != %v; got: %v", nil, agg.NamedBuckets)
- }
- if len(agg.NamedBuckets) != 2 {
- t.Fatalf("expected %d buckets; got: %d", 2, len(agg.NamedBuckets))
- }
- if agg.NamedBuckets["errors"].DocCount != 34 {
- t.Fatalf("expected DocCount = %d; got: %d", 34, agg.NamedBuckets["errors"].DocCount)
- }
- subAgg, found := agg.NamedBuckets["errors"].Histogram("monthly")
- if !found {
- t.Fatalf("expected sub aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
- }
- if agg.NamedBuckets["warnings"].DocCount != 439 {
- t.Fatalf("expected DocCount = %d; got: %d", 439, agg.NamedBuckets["warnings"].DocCount)
- }
- subAgg, found = agg.NamedBuckets["warnings"].Histogram("monthly")
- if !found {
- t.Fatalf("expected sub aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub aggregation != %v; got: %v", nil, subAgg)
- }
- }
- func TestAggsBucketMissing(t *testing.T) {
- s := `{
- "products_without_a_price" : {
- "doc_count" : 10
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Missing("products_without_a_price")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 10 {
- t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
- }
- }
- func TestAggsBucketNested(t *testing.T) {
- s := `{
- "resellers": {
- "min_price": {
- "value" : 350
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Nested("resellers")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 0 {
- t.Fatalf("expected aggregation DocCount = %d; got: %d", 0, agg.DocCount)
- }
- // Sub-aggregation
- subAgg, found := agg.Avg("min_price")
- if !found {
- t.Fatalf("expected sub-aggregation to be found; got: %v", found)
- }
- if subAgg == nil {
- t.Fatalf("expected sub-aggregation != nil; got: %v", subAgg)
- }
- if subAgg.Value == nil {
- t.Fatalf("expected sub-aggregation value != nil; got: %v", subAgg.Value)
- }
- if *subAgg.Value != float64(350) {
- t.Fatalf("expected sub-aggregation value = %v; got: %v", float64(350), *subAgg.Value)
- }
- }
- func TestAggsBucketReverseNested(t *testing.T) {
- s := `{
- "comment_to_issue": {
- "doc_count" : 10
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.ReverseNested("comment_to_issue")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 10 {
- t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
- }
- }
- func TestAggsBucketChildren(t *testing.T) {
- s := `{
- "to-answers": {
- "doc_count" : 10
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Children("to-answers")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 10 {
- t.Fatalf("expected aggregation DocCount = %d; got: %d", 10, agg.DocCount)
- }
- }
- func TestAggsBucketTerms(t *testing.T) {
- s := `{
- "users" : {
- "doc_count_error_upper_bound" : 1,
- "sum_other_doc_count" : 2,
- "buckets" : [ {
- "key" : "olivere",
- "doc_count" : 2
- }, {
- "key" : "sandrae",
- "doc_count" : 1
- } ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Terms("users")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != "olivere" {
- t.Errorf("expected key %q; got: %q", "olivere", agg.Buckets[0].Key)
- }
- if agg.Buckets[0].DocCount != 2 {
- t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].Key != "sandrae" {
- t.Errorf("expected key %q; got: %q", "sandrae", agg.Buckets[1].Key)
- }
- if agg.Buckets[1].DocCount != 1 {
- t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
- }
- }
- func TestAggsBucketTermsWithNumericKeys(t *testing.T) {
- s := `{
- "users" : {
- "doc_count_error_upper_bound" : 1,
- "sum_other_doc_count" : 2,
- "buckets" : [ {
- "key" : 17,
- "doc_count" : 2
- }, {
- "key" : 21,
- "doc_count" : 1
- } ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Terms("users")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != float64(17) {
- t.Errorf("expected key %v; got: %v", 17, agg.Buckets[0].Key)
- }
- if got, err := agg.Buckets[0].KeyNumber.Int64(); err != nil {
- t.Errorf("expected to convert key to int64; got: %v", err)
- } else if got != 17 {
- t.Errorf("expected key %v; got: %v", 17, agg.Buckets[0].Key)
- }
- if agg.Buckets[0].DocCount != 2 {
- t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].Key != float64(21) {
- t.Errorf("expected key %v; got: %v", 21, agg.Buckets[1].Key)
- }
- if got, err := agg.Buckets[1].KeyNumber.Int64(); err != nil {
- t.Errorf("expected to convert key to int64; got: %v", err)
- } else if got != 21 {
- t.Errorf("expected key %v; got: %v", 21, agg.Buckets[1].Key)
- }
- if agg.Buckets[1].DocCount != 1 {
- t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
- }
- }
- func TestAggsBucketTermsWithBoolKeys(t *testing.T) {
- s := `{
- "users" : {
- "doc_count_error_upper_bound" : 1,
- "sum_other_doc_count" : 2,
- "buckets" : [ {
- "key" : true,
- "doc_count" : 2
- }, {
- "key" : false,
- "doc_count" : 1
- } ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Terms("users")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != true {
- t.Errorf("expected key %v; got: %v", true, agg.Buckets[0].Key)
- }
- if agg.Buckets[0].DocCount != 2 {
- t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].Key != false {
- t.Errorf("expected key %v; got: %v", false, agg.Buckets[1].Key)
- }
- if agg.Buckets[1].DocCount != 1 {
- t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[1].DocCount)
- }
- }
- func TestAggsBucketSignificantTerms(t *testing.T) {
- s := `{
- "significantCrimeTypes" : {
- "doc_count": 47347,
- "buckets" : [
- {
- "key": "Bicycle theft",
- "doc_count": 3640,
- "score": 0.371235374214817,
- "bg_count": 66799
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.SignificantTerms("significantCrimeTypes")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 47347 {
- t.Fatalf("expected aggregation DocCount != %d; got: %d", 47347, agg.DocCount)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 1 {
- t.Errorf("expected %d bucket entries; got: %d", 1, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != "Bicycle theft" {
- t.Errorf("expected key = %q; got: %q", "Bicycle theft", agg.Buckets[0].Key)
- }
- if agg.Buckets[0].DocCount != 3640 {
- t.Errorf("expected doc count = %d; got: %d", 3640, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[0].Score != float64(0.371235374214817) {
- t.Errorf("expected score = %v; got: %v", float64(0.371235374214817), agg.Buckets[0].Score)
- }
- if agg.Buckets[0].BgCount != 66799 {
- t.Errorf("expected BgCount = %d; got: %d", 66799, agg.Buckets[0].BgCount)
- }
- }
- func TestAggsBucketSampler(t *testing.T) {
- s := `{
- "sample" : {
- "doc_count": 1000,
- "keywords": {
- "doc_count": 1000,
- "buckets" : [
- {
- "key": "bend",
- "doc_count": 58,
- "score": 37.982536582524276,
- "bg_count": 103
- }
- ]
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Sampler("sample")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.DocCount != 1000 {
- t.Fatalf("expected aggregation DocCount != %d; got: %d", 1000, agg.DocCount)
- }
- sub, found := agg.Aggregations["keywords"]
- if !found {
- t.Fatalf("expected sub aggregation %q", "keywords")
- }
- if sub == nil {
- t.Fatalf("expected sub aggregation %q; got: %v", "keywords", sub)
- }
- }
- func TestAggsBucketRange(t *testing.T) {
- s := `{
- "price_ranges" : {
- "buckets": [
- {
- "to": 50,
- "doc_count": 2
- },
- {
- "from": 50,
- "to": 100,
- "doc_count": 4
- },
- {
- "from": 100,
- "doc_count": 4
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Range("price_ranges")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 3 {
- t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
- }
- if agg.Buckets[0].From != nil {
- t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
- }
- if agg.Buckets[0].To == nil {
- t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
- }
- if *agg.Buckets[0].To != float64(50) {
- t.Errorf("expected To = %v; got: %v", float64(50), *agg.Buckets[0].To)
- }
- if agg.Buckets[0].DocCount != 2 {
- t.Errorf("expected DocCount = %d; got: %d", 2, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].From == nil {
- t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
- }
- if *agg.Buckets[1].From != float64(50) {
- t.Errorf("expected From = %v; got: %v", float64(50), *agg.Buckets[1].From)
- }
- if agg.Buckets[1].To == nil {
- t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[1].To)
- }
- if *agg.Buckets[1].To != float64(100) {
- t.Errorf("expected To = %v; got: %v", float64(100), *agg.Buckets[1].To)
- }
- if agg.Buckets[1].DocCount != 4 {
- t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[1].DocCount)
- }
- if agg.Buckets[2].From == nil {
- t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[2].From)
- }
- if *agg.Buckets[2].From != float64(100) {
- t.Errorf("expected From = %v; got: %v", float64(100), *agg.Buckets[2].From)
- }
- if agg.Buckets[2].To != nil {
- t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[2].To)
- }
- if agg.Buckets[2].DocCount != 4 {
- t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[2].DocCount)
- }
- }
- func TestAggsBucketDateRange(t *testing.T) {
- s := `{
- "range": {
- "buckets": [
- {
- "to": 1.3437792E+12,
- "to_as_string": "08-2012",
- "doc_count": 7
- },
- {
- "from": 1.3437792E+12,
- "from_as_string": "08-2012",
- "doc_count": 2
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.DateRange("range")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].From != nil {
- t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
- }
- if agg.Buckets[0].To == nil {
- t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
- }
- if *agg.Buckets[0].To != float64(1.3437792E+12) {
- t.Errorf("expected To = %v; got: %v", float64(1.3437792E+12), *agg.Buckets[0].To)
- }
- if agg.Buckets[0].ToAsString != "08-2012" {
- t.Errorf("expected ToAsString = %q; got: %q", "08-2012", agg.Buckets[0].ToAsString)
- }
- if agg.Buckets[0].DocCount != 7 {
- t.Errorf("expected DocCount = %d; got: %d", 7, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].From == nil {
- t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
- }
- if *agg.Buckets[1].From != float64(1.3437792E+12) {
- t.Errorf("expected From = %v; got: %v", float64(1.3437792E+12), *agg.Buckets[1].From)
- }
- if agg.Buckets[1].FromAsString != "08-2012" {
- t.Errorf("expected FromAsString = %q; got: %q", "08-2012", agg.Buckets[1].FromAsString)
- }
- if agg.Buckets[1].To != nil {
- t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[1].To)
- }
- if agg.Buckets[1].DocCount != 2 {
- t.Errorf("expected DocCount = %d; got: %d", 2, agg.Buckets[1].DocCount)
- }
- }
- func TestAggsBucketIPRange(t *testing.T) {
- s := `{
- "ip_ranges": {
- "buckets" : [
- {
- "to": 167772165,
- "to_as_string": "10.0.0.5",
- "doc_count": 4
- },
- {
- "from": 167772165,
- "from_as_string": "10.0.0.5",
- "doc_count": 6
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.IPRange("ip_ranges")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].From != nil {
- t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
- }
- if agg.Buckets[0].To == nil {
- t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
- }
- if *agg.Buckets[0].To != float64(167772165) {
- t.Errorf("expected To = %v; got: %v", float64(167772165), *agg.Buckets[0].To)
- }
- if agg.Buckets[0].ToAsString != "10.0.0.5" {
- t.Errorf("expected ToAsString = %q; got: %q", "10.0.0.5", agg.Buckets[0].ToAsString)
- }
- if agg.Buckets[0].DocCount != 4 {
- t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].From == nil {
- t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
- }
- if *agg.Buckets[1].From != float64(167772165) {
- t.Errorf("expected From = %v; got: %v", float64(167772165), *agg.Buckets[1].From)
- }
- if agg.Buckets[1].FromAsString != "10.0.0.5" {
- t.Errorf("expected FromAsString = %q; got: %q", "10.0.0.5", agg.Buckets[1].FromAsString)
- }
- if agg.Buckets[1].To != nil {
- t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[1].To)
- }
- if agg.Buckets[1].DocCount != 6 {
- t.Errorf("expected DocCount = %d; got: %d", 6, agg.Buckets[1].DocCount)
- }
- }
- func TestAggsBucketHistogram(t *testing.T) {
- s := `{
- "prices" : {
- "buckets": [
- {
- "key": 0,
- "doc_count": 2
- },
- {
- "key": 50,
- "doc_count": 4
- },
- {
- "key": 150,
- "doc_count": 3
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Histogram("prices")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 3 {
- t.Errorf("expected %d buckets; got: %d", 3, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != 0 {
- t.Errorf("expected key = %v; got: %v", 0, agg.Buckets[0].Key)
- }
- if agg.Buckets[0].KeyAsString != nil {
- t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[0].KeyAsString)
- }
- if agg.Buckets[0].DocCount != 2 {
- t.Errorf("expected doc count = %d; got: %d", 2, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].Key != 50 {
- t.Errorf("expected key = %v; got: %v", 50, agg.Buckets[1].Key)
- }
- if agg.Buckets[1].KeyAsString != nil {
- t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[1].KeyAsString)
- }
- if agg.Buckets[1].DocCount != 4 {
- t.Errorf("expected doc count = %d; got: %d", 4, agg.Buckets[1].DocCount)
- }
- if agg.Buckets[2].Key != 150 {
- t.Errorf("expected key = %v; got: %v", 150, agg.Buckets[2].Key)
- }
- if agg.Buckets[2].KeyAsString != nil {
- t.Fatalf("expected key_as_string = %v; got: %q", nil, *agg.Buckets[2].KeyAsString)
- }
- if agg.Buckets[2].DocCount != 3 {
- t.Errorf("expected doc count = %d; got: %d", 3, agg.Buckets[2].DocCount)
- }
- }
- func TestAggsBucketDateHistogram(t *testing.T) {
- s := `{
- "articles_over_time": {
- "buckets": [
- {
- "key_as_string": "2013-02-02",
- "key": 1328140800000,
- "doc_count": 1
- },
- {
- "key_as_string": "2013-03-02",
- "key": 1330646400000,
- "doc_count": 2
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.DateHistogram("articles_over_time")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != 1328140800000 {
- t.Errorf("expected key %v; got: %v", 1328140800000, agg.Buckets[0].Key)
- }
- if agg.Buckets[0].KeyAsString == nil {
- t.Fatalf("expected key_as_string != nil; got: %v", agg.Buckets[0].KeyAsString)
- }
- if *agg.Buckets[0].KeyAsString != "2013-02-02" {
- t.Errorf("expected key_as_string %q; got: %q", "2013-02-02", *agg.Buckets[0].KeyAsString)
- }
- if agg.Buckets[0].DocCount != 1 {
- t.Errorf("expected doc count %d; got: %d", 1, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].Key != 1330646400000 {
- t.Errorf("expected key %v; got: %v", 1330646400000, agg.Buckets[1].Key)
- }
- if agg.Buckets[1].KeyAsString == nil {
- t.Fatalf("expected key_as_string != nil; got: %v", agg.Buckets[1].KeyAsString)
- }
- if *agg.Buckets[1].KeyAsString != "2013-03-02" {
- t.Errorf("expected key_as_string %q; got: %q", "2013-03-02", *agg.Buckets[1].KeyAsString)
- }
- if agg.Buckets[1].DocCount != 2 {
- t.Errorf("expected doc count %d; got: %d", 2, agg.Buckets[1].DocCount)
- }
- }
- func TestAggsMetricsGeoBounds(t *testing.T) {
- s := `{
- "viewport": {
- "bounds": {
- "top_left": {
- "lat": 80.45,
- "lon": -160.22
- },
- "bottom_right": {
- "lat": 40.65,
- "lon": 42.57
- }
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.GeoBounds("viewport")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Bounds.TopLeft.Latitude != float64(80.45) {
- t.Fatalf("expected Bounds.TopLeft.Latitude != %v; got: %v", float64(80.45), agg.Bounds.TopLeft.Latitude)
- }
- if agg.Bounds.TopLeft.Longitude != float64(-160.22) {
- t.Fatalf("expected Bounds.TopLeft.Longitude != %v; got: %v", float64(-160.22), agg.Bounds.TopLeft.Longitude)
- }
- if agg.Bounds.BottomRight.Latitude != float64(40.65) {
- t.Fatalf("expected Bounds.BottomRight.Latitude != %v; got: %v", float64(40.65), agg.Bounds.BottomRight.Latitude)
- }
- if agg.Bounds.BottomRight.Longitude != float64(42.57) {
- t.Fatalf("expected Bounds.BottomRight.Longitude != %v; got: %v", float64(42.57), agg.Bounds.BottomRight.Longitude)
- }
- }
- func TestAggsBucketGeoHash(t *testing.T) {
- s := `{
- "myLarge-GrainGeoHashGrid": {
- "buckets": [
- {
- "key": "svz",
- "doc_count": 10964
- },
- {
- "key": "sv8",
- "doc_count": 3198
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.GeoHash("myLarge-GrainGeoHashGrid")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(agg.Buckets))
- }
- if agg.Buckets[0].Key != "svz" {
- t.Errorf("expected key %q; got: %q", "svz", agg.Buckets[0].Key)
- }
- if agg.Buckets[0].DocCount != 10964 {
- t.Errorf("expected doc count %d; got: %d", 10964, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].Key != "sv8" {
- t.Errorf("expected key %q; got: %q", "sv8", agg.Buckets[1].Key)
- }
- if agg.Buckets[1].DocCount != 3198 {
- t.Errorf("expected doc count %d; got: %d", 3198, agg.Buckets[1].DocCount)
- }
- }
- func TestAggsMetricsGeoCentroid(t *testing.T) {
- s := `{
- "centroid": {
- "location": {
- "lat": 80.45,
- "lon": -160.22
- },
- "count": 6
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.GeoCentroid("centroid")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Location.Latitude != float64(80.45) {
- t.Fatalf("expected Location.Latitude != %v; got: %v", float64(80.45), agg.Location.Latitude)
- }
- if agg.Location.Longitude != float64(-160.22) {
- t.Fatalf("expected Location.Longitude != %v; got: %v", float64(-160.22), agg.Location.Longitude)
- }
- if agg.Count != int(6) {
- t.Fatalf("expected Count != %v; got: %v", int(6), agg.Count)
- }
- }
- func TestAggsBucketGeoDistance(t *testing.T) {
- s := `{
- "rings" : {
- "buckets": [
- {
- "unit": "km",
- "to": 100.0,
- "doc_count": 3
- },
- {
- "unit": "km",
- "from": 100.0,
- "to": 300.0,
- "doc_count": 1
- },
- {
- "unit": "km",
- "from": 300.0,
- "doc_count": 7
- }
- ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.GeoDistance("rings")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Buckets == nil {
- t.Fatalf("expected aggregation buckets != nil; got: %v", agg.Buckets)
- }
- if len(agg.Buckets) != 3 {
- t.Errorf("expected %d bucket entries; got: %d", 3, len(agg.Buckets))
- }
- if agg.Buckets[0].From != nil {
- t.Errorf("expected From = %v; got: %v", nil, agg.Buckets[0].From)
- }
- if agg.Buckets[0].To == nil {
- t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[0].To)
- }
- if *agg.Buckets[0].To != float64(100.0) {
- t.Errorf("expected To = %v; got: %v", float64(100.0), *agg.Buckets[0].To)
- }
- if agg.Buckets[0].DocCount != 3 {
- t.Errorf("expected DocCount = %d; got: %d", 4, agg.Buckets[0].DocCount)
- }
- if agg.Buckets[1].From == nil {
- t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[1].From)
- }
- if *agg.Buckets[1].From != float64(100.0) {
- t.Errorf("expected From = %v; got: %v", float64(100.0), *agg.Buckets[1].From)
- }
- if agg.Buckets[1].To == nil {
- t.Errorf("expected To != %v; got: %v", nil, agg.Buckets[1].To)
- }
- if *agg.Buckets[1].To != float64(300.0) {
- t.Errorf("expected From = %v; got: %v", float64(300.0), *agg.Buckets[1].To)
- }
- if agg.Buckets[1].DocCount != 1 {
- t.Errorf("expected DocCount = %d; got: %d", 1, agg.Buckets[1].DocCount)
- }
- if agg.Buckets[2].From == nil {
- t.Errorf("expected From != %v; got: %v", nil, agg.Buckets[2].From)
- }
- if *agg.Buckets[2].From != float64(300.0) {
- t.Errorf("expected From = %v; got: %v", float64(300.0), *agg.Buckets[2].From)
- }
- if agg.Buckets[2].To != nil {
- t.Errorf("expected To = %v; got: %v", nil, agg.Buckets[2].To)
- }
- if agg.Buckets[2].DocCount != 7 {
- t.Errorf("expected DocCount = %d; got: %d", 7, agg.Buckets[2].DocCount)
- }
- }
- func TestAggsSubAggregates(t *testing.T) {
- rs := `{
- "users" : {
- "doc_count_error_upper_bound" : 1,
- "sum_other_doc_count" : 2,
- "buckets" : [ {
- "key" : "olivere",
- "doc_count" : 2,
- "ts" : {
- "buckets" : [ {
- "key_as_string" : "2012-01-01T00:00:00.000Z",
- "key" : 1325376000000,
- "doc_count" : 2
- } ]
- }
- }, {
- "key" : "sandrae",
- "doc_count" : 1,
- "ts" : {
- "buckets" : [ {
- "key_as_string" : "2011-01-01T00:00:00.000Z",
- "key" : 1293840000000,
- "doc_count" : 1
- } ]
- }
- } ]
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(rs), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- // Access top-level aggregation
- users, found := aggs.Terms("users")
- if !found {
- t.Fatalf("expected users aggregation to be found; got: %v", found)
- }
- if users == nil {
- t.Fatalf("expected users aggregation; got: %v", users)
- }
- if users.Buckets == nil {
- t.Fatalf("expected users buckets; got: %v", users.Buckets)
- }
- if len(users.Buckets) != 2 {
- t.Errorf("expected %d bucket entries; got: %d", 2, len(users.Buckets))
- }
- if users.Buckets[0].Key != "olivere" {
- t.Errorf("expected key %q; got: %q", "olivere", users.Buckets[0].Key)
- }
- if users.Buckets[0].DocCount != 2 {
- t.Errorf("expected doc count %d; got: %d", 2, users.Buckets[0].DocCount)
- }
- if users.Buckets[1].Key != "sandrae" {
- t.Errorf("expected key %q; got: %q", "sandrae", users.Buckets[1].Key)
- }
- if users.Buckets[1].DocCount != 1 {
- t.Errorf("expected doc count %d; got: %d", 1, users.Buckets[1].DocCount)
- }
- // Access sub-aggregation
- ts, found := users.Buckets[0].DateHistogram("ts")
- if !found {
- t.Fatalf("expected ts aggregation to be found; got: %v", found)
- }
- if ts == nil {
- t.Fatalf("expected ts aggregation; got: %v", ts)
- }
- if ts.Buckets == nil {
- t.Fatalf("expected ts buckets; got: %v", ts.Buckets)
- }
- if len(ts.Buckets) != 1 {
- t.Errorf("expected %d bucket entries; got: %d", 1, len(ts.Buckets))
- }
- if ts.Buckets[0].Key != 1325376000000 {
- t.Errorf("expected key %v; got: %v", 1325376000000, ts.Buckets[0].Key)
- }
- if ts.Buckets[0].KeyAsString == nil {
- t.Fatalf("expected key_as_string != %v; got: %v", nil, ts.Buckets[0].KeyAsString)
- }
- if *ts.Buckets[0].KeyAsString != "2012-01-01T00:00:00.000Z" {
- t.Errorf("expected key_as_string %q; got: %q", "2012-01-01T00:00:00.000Z", *ts.Buckets[0].KeyAsString)
- }
- }
- func TestAggsPipelineAvgBucket(t *testing.T) {
- s := `{
- "avg_monthly_sales" : {
- "value" : 328.33333333333333
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.AvgBucket("avg_monthly_sales")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(328.33333333333333) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(328.33333333333333), *agg.Value)
- }
- }
- func TestAggsPipelineSumBucket(t *testing.T) {
- s := `{
- "sum_monthly_sales" : {
- "value" : 985
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.SumBucket("sum_monthly_sales")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(985) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(985), *agg.Value)
- }
- }
- func TestAggsPipelineMaxBucket(t *testing.T) {
- s := `{
- "max_monthly_sales" : {
- "keys": ["2015/01/01 00:00:00"],
- "value" : 550
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.MaxBucket("max_monthly_sales")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if len(agg.Keys) != 1 {
- t.Fatalf("expected 1 key; got: %d", len(agg.Keys))
- }
- if got, want := agg.Keys[0], "2015/01/01 00:00:00"; got != want {
- t.Fatalf("expected key %q; got: %v (%T)", want, got, got)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(550) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(550), *agg.Value)
- }
- }
- func TestAggsPipelineMinBucket(t *testing.T) {
- s := `{
- "min_monthly_sales" : {
- "keys": ["2015/02/01 00:00:00"],
- "value" : 60
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.MinBucket("min_monthly_sales")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if len(agg.Keys) != 1 {
- t.Fatalf("expected 1 key; got: %d", len(agg.Keys))
- }
- if got, want := agg.Keys[0], "2015/02/01 00:00:00"; got != want {
- t.Fatalf("expected key %q; got: %v (%T)", want, got, got)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(60) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(60), *agg.Value)
- }
- }
- func TestAggsPipelineMovAvg(t *testing.T) {
- s := `{
- "the_movavg" : {
- "value" : 12.0
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.MovAvg("the_movavg")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(12.0) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(12.0), *agg.Value)
- }
- }
- func TestAggsPipelineDerivative(t *testing.T) {
- s := `{
- "sales_deriv" : {
- "value" : 315
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.Derivative("sales_deriv")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(315) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(315), *agg.Value)
- }
- }
- func TestAggsPipelinePercentilesBucket(t *testing.T) {
- s := `{
- "sales_percentiles": {
- "values": {
- "25.0": 100,
- "50.0": 200,
- "75.0": 300
- }
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.PercentilesBucket("sales_percentiles")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if len(agg.Values) != 3 {
- t.Fatalf("expected aggregation map with three entries; got: %v", agg.Values)
- }
- }
- func TestAggsPipelineStatsBucket(t *testing.T) {
- s := `{
- "stats_monthly_sales": {
- "count": 3,
- "min": 60.0,
- "max": 550.0,
- "avg": 328.3333333333333,
- "sum": 985.0
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.StatsBucket("stats_monthly_sales")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Count != 3 {
- t.Fatalf("expected aggregation count = %v; got: %v", 3, agg.Count)
- }
- if agg.Min == nil {
- t.Fatalf("expected aggregation min != nil; got: %v", agg.Min)
- }
- if *agg.Min != float64(60.0) {
- t.Fatalf("expected aggregation min = %v; got: %v", float64(60.0), *agg.Min)
- }
- if agg.Max == nil {
- t.Fatalf("expected aggregation max != nil; got: %v", agg.Max)
- }
- if *agg.Max != float64(550.0) {
- t.Fatalf("expected aggregation max = %v; got: %v", float64(550.0), *agg.Max)
- }
- if agg.Avg == nil {
- t.Fatalf("expected aggregation avg != nil; got: %v", agg.Avg)
- }
- if *agg.Avg != float64(328.3333333333333) {
- t.Fatalf("expected aggregation average = %v; got: %v", float64(328.3333333333333), *agg.Avg)
- }
- if agg.Sum == nil {
- t.Fatalf("expected aggregation sum != nil; got: %v", agg.Sum)
- }
- if *agg.Sum != float64(985.0) {
- t.Fatalf("expected aggregation sum = %v; got: %v", float64(985.0), *agg.Sum)
- }
- }
- func TestAggsPipelineCumulativeSum(t *testing.T) {
- s := `{
- "cumulative_sales" : {
- "value" : 550
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.CumulativeSum("cumulative_sales")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(550) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(550), *agg.Value)
- }
- }
- func TestAggsPipelineBucketScript(t *testing.T) {
- s := `{
- "t-shirt-percentage" : {
- "value" : 20
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.BucketScript("t-shirt-percentage")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(20) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(20), *agg.Value)
- }
- }
- func TestAggsPipelineSerialDiff(t *testing.T) {
- s := `{
- "the_diff" : {
- "value" : -722.0
- }
- }`
- aggs := new(Aggregations)
- err := json.Unmarshal([]byte(s), &aggs)
- if err != nil {
- t.Fatalf("expected no error decoding; got: %v", err)
- }
- agg, found := aggs.SerialDiff("the_diff")
- if !found {
- t.Fatalf("expected aggregation to be found; got: %v", found)
- }
- if agg == nil {
- t.Fatalf("expected aggregation != nil; got: %v", agg)
- }
- if agg.Value == nil {
- t.Fatalf("expected aggregation value != nil; got: %v", agg.Value)
- }
- if *agg.Value != float64(-722.0) {
- t.Fatalf("expected aggregation value = %v; got: %v", float64(20), *agg.Value)
- }
- }
|