1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313 |
- // 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")
- dateHistoKeyedAgg := NewDateHistogramAggregation().Field("created").Interval("year").Keyed(true)
- 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("dateHistoKeyed", dateHistoKeyedAgg)
- 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)
- }
- // dateHistoKeyed
- {
- res, found := agg.KeyedDateHistogram("dateHistoKeyed")
- if !found {
- t.Errorf("expected %v; got: %v", true, found)
- }
- if res == nil {
- t.Fatalf("expected != nil; got: nil")
- }
- if len(res.Buckets) != 2 {
- t.Fatalf("expected %d; got: %d", 2, len(res.Buckets))
- }
- bucket, ok := res.Buckets["2011-01-01T00:00:00.000Z"]
- if !ok || bucket == nil {
- t.Fatalf("expected to have bucket with key %q", "2011-01-01T00:00:00.000Z")
- }
- if bucket.DocCount != 1 {
- t.Errorf("expected %d; got: %d", 1, bucket.DocCount)
- }
- if bucket.Key != 1.29384e+12 {
- t.Errorf("expected %v; got: %v", 1.29384e+12, bucket.Key)
- }
- if bucket.KeyAsString == nil {
- t.Fatalf("expected != nil; got: %v", bucket.KeyAsString)
- }
- if *bucket.KeyAsString != "2011-01-01T00:00:00.000Z" {
- t.Errorf("expected %q; got: %q", "2011-01-01T00:00:00.000Z", *bucket.KeyAsString)
- }
- bucket, ok = res.Buckets["2012-01-01T00:00:00.000Z"]
- if !ok || bucket == nil {
- t.Fatalf("expected to have bucket with key %q", "2012-01-01T00:00:00.000Z")
- }
- if bucket.DocCount != 2 {
- t.Errorf("expected %d; got: %d", 2, bucket.DocCount)
- }
- if bucket.Key != 1.325376e+12 {
- t.Errorf("expected %v; got: %v", 1.325376e+12, bucket.Key)
- }
- if bucket.KeyAsString == nil {
- t.Fatalf("expected != nil; got: %v", bucket.KeyAsString)
- }
- if *bucket.KeyAsString != "2012-01-01T00:00:00.000Z" {
- t.Errorf("expected %q; got: %q", "2012-01-01T00:00:00.000Z", *bucket.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)
- }
- }
|