12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370 |
- // 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"
- "reflect"
- "testing"
- "time"
- )
- func TestSearchMatchAll(t *testing.T) {
- //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
- client := setupTestClientAndCreateIndexAndAddDocs(t)
- // Match all should return all documents
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(NewMatchAllQuery()).
- Size(100).
- Pretty(true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if got, want := searchResult.Hits.TotalHits, int64(12); got != want {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
- }
- if got, want := len(searchResult.Hits.Hits), 12; got != want {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
- }
- for _, hit := range searchResult.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
- }
- item := make(map[string]interface{})
- err := json.Unmarshal(*hit.Source, &item)
- if err != nil {
- t.Fatal(err)
- }
- }
- }
- func TestSearchMatchAllWithRequestCacheDisabled(t *testing.T) {
- //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
- client := setupTestClientAndCreateIndexAndAddDocs(t)
- // Match all should return all documents, with request cache disabled
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(NewMatchAllQuery()).
- Size(100).
- Pretty(true).
- RequestCache(false).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if got, want := searchResult.Hits.TotalHits, int64(12); got != want {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
- }
- if got, want := len(searchResult.Hits.Hits), 12; got != want {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
- }
- }
- func BenchmarkSearchMatchAll(b *testing.B) {
- client := setupTestClientAndCreateIndexAndAddDocs(b)
- for n := 0; n < b.N; n++ {
- // Match all should return all documents
- all := NewMatchAllQuery()
- searchResult, err := client.Search().Index(testIndexName).Query(all).Do(context.TODO())
- if err != nil {
- b.Fatal(err)
- }
- if searchResult.Hits == nil {
- b.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits == 0 {
- b.Errorf("expected SearchResult.Hits.TotalHits > %d; got %d", 0, searchResult.Hits.TotalHits)
- }
- }
- }
- func TestSearchResultTotalHits(t *testing.T) {
- client := setupTestClientAndCreateIndexAndAddDocs(t)
- count, err := client.Count(testIndexName).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- all := NewMatchAllQuery()
- searchResult, err := client.Search().Index(testIndexName).Query(all).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- got := searchResult.TotalHits()
- if got != count {
- t.Fatalf("expected %d hits; got: %d", count, got)
- }
- // No hits
- searchResult = &SearchResult{}
- got = searchResult.TotalHits()
- if got != 0 {
- t.Errorf("expected %d hits; got: %d", 0, got)
- }
- }
- func TestSearchResultWithProfiling(t *testing.T) {
- client := setupTestClientAndCreateIndexAndAddDocs(t)
- all := NewMatchAllQuery()
- searchResult, err := client.Search().Index(testIndexName).Query(all).Profile(true).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Profile == nil {
- t.Fatal("Profiled MatchAll query did not return profiling data with results")
- }
- }
- func TestSearchResultEach(t *testing.T) {
- client := setupTestClientAndCreateIndexAndAddDocs(t)
- all := NewMatchAllQuery()
- searchResult, err := client.Search().Index(testIndexName).Query(all).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- // Iterate over non-ptr type
- var aTweet tweet
- count := 0
- for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
- count++
- _, ok := item.(tweet)
- if !ok {
- t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
- }
- }
- if count == 0 {
- t.Errorf("expected to find some hits; got: %d", count)
- }
- // Iterate over ptr-type
- count = 0
- var aTweetPtr *tweet
- for _, item := range searchResult.Each(reflect.TypeOf(aTweetPtr)) {
- count++
- tw, ok := item.(*tweet)
- if !ok {
- t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
- }
- if tw == nil {
- t.Fatal("expected hit to not be nil")
- }
- }
- if count == 0 {
- t.Errorf("expected to find some hits; got: %d", count)
- }
- // Does not iterate when no hits are found
- searchResult = &SearchResult{Hits: nil}
- count = 0
- for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
- count++
- _ = item
- }
- if count != 0 {
- t.Errorf("expected to not find any hits; got: %d", count)
- }
- searchResult = &SearchResult{Hits: &SearchHits{Hits: make([]*SearchHit, 0)}}
- count = 0
- for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
- count++
- _ = item
- }
- if count != 0 {
- t.Errorf("expected to not find any hits; got: %d", count)
- }
- }
- func TestSearchResultEachNoSource(t *testing.T) {
- client := setupTestClientAndCreateIndexAndAddDocsNoSource(t)
- all := NewMatchAllQuery()
- searchResult, err := client.Search().Index(testIndexName).Type("tweet-nosource").Query(all).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- // Iterate over non-ptr type
- var aTweet tweet
- count := 0
- for _, item := range searchResult.Each(reflect.TypeOf(aTweet)) {
- count++
- tw, ok := item.(tweet)
- if !ok {
- t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
- }
- if tw.User != "" {
- t.Fatalf("expected no _source hit to be empty tweet; got: %v", reflect.ValueOf(item))
- }
- }
- if count != 2 {
- t.Errorf("expected to find 2 hits; got: %d", count)
- }
- // Iterate over ptr-type
- count = 0
- var aTweetPtr *tweet
- for _, item := range searchResult.Each(reflect.TypeOf(aTweetPtr)) {
- count++
- tw, ok := item.(*tweet)
- if !ok {
- t.Fatalf("expected hit to be serialized as tweet; got: %v", reflect.ValueOf(item))
- }
- if tw != nil {
- t.Fatal("expected hit to be nil")
- }
- }
- if count != 2 {
- t.Errorf("expected to find 2 hits; got: %d", count)
- }
- }
- func TestSearchSorting(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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()
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(all).
- Sort("created", false).
- Timeout("1s").
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 3 {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
- }
- for _, hit := range searchResult.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
- }
- item := make(map[string]interface{})
- err := json.Unmarshal(*hit.Source, &item)
- if err != nil {
- t.Fatal(err)
- }
- }
- }
- func TestSearchSortingBySorters(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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()
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(all).
- SortBy(NewFieldSort("created").Desc(), NewScoreSort()).
- Timeout("1s").
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 3 {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
- }
- for _, hit := range searchResult.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
- }
- item := make(map[string]interface{})
- err := json.Unmarshal(*hit.Source, &item)
- if err != nil {
- t.Fatal(err)
- }
- }
- }
- func TestSearchSpecificFields(t *testing.T) {
- // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
- tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
- tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
- // 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()
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(all).
- StoredFields("message").
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 3 {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
- }
- for _, hit := range searchResult.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
- }
- if hit.Source != nil {
- t.Fatalf("expected SearchResult.Hits.Hit.Source to be nil; got: %v", hit.Source)
- }
- if hit.Fields == nil {
- t.Fatal("expected SearchResult.Hits.Hit.Fields to be != nil")
- }
- field, found := hit.Fields["message"]
- if !found {
- t.Errorf("expected SearchResult.Hits.Hit.Fields[%s] to be found", "message")
- }
- fields, ok := field.([]interface{})
- if !ok {
- t.Errorf("expected []interface{}; got: %v", reflect.TypeOf(fields))
- }
- if len(fields) != 1 {
- t.Errorf("expected a field with 1 entry; got: %d", len(fields))
- }
- message, ok := fields[0].(string)
- if !ok {
- t.Errorf("expected a string; got: %v", reflect.TypeOf(fields[0]))
- }
- if message == "" {
- t.Errorf("expected a message; got: %q", message)
- }
- }
- }
- func TestSearchExplain(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", 0)))
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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()
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(all).
- Explain(true).
- Timeout("1s").
- // Pretty(true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 3 {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
- }
- for _, hit := range searchResult.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
- }
- if hit.Explanation == nil {
- t.Fatal("expected search explanation")
- }
- if hit.Explanation.Value <= 0.0 {
- t.Errorf("expected explanation value to be > 0.0; got: %v", hit.Explanation.Value)
- }
- if hit.Explanation.Description == "" {
- t.Errorf("expected explanation description != %q; got: %q", "", hit.Explanation.Description)
- }
- }
- }
- func TestSearchSource(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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)
- }
- // Set up the request JSON manually to pass to the search service via Source()
- source := map[string]interface{}{
- "query": map[string]interface{}{
- "match_all": map[string]interface{}{},
- },
- }
- searchResult, err := client.Search().
- Index(testIndexName).
- Source(source). // sets the JSON request
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- }
- func TestSearchSourceWithString(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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)
- }
- searchResult, err := client.Search().
- Index(testIndexName).
- Source(`{"query":{"match_all":{}}}`). // sets the JSON request
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- }
- func TestSearchRawString(t *testing.T) {
- // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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)
- }
- query := RawStringQuery(`{"match_all":{}}`)
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(query).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- }
- func TestSearchSearchSource(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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)
- }
- // Set up the search source manually and pass it to the search service via SearchSource()
- ss := NewSearchSource().Query(NewMatchAllQuery()).From(0).Size(2)
- // One can use ss.Source() to get to the raw interface{} that will be used
- // as the search request JSON by the SearchService.
- searchResult, err := client.Search().
- Index(testIndexName).
- SearchSource(ss). // sets the SearchSource
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 2 {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 2, len(searchResult.Hits.Hits))
- }
- }
- func TestSearchInnerHitsOnHasChild(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- // Check for valid ES version
- esversion, err := client.ElasticsearchVersion(DefaultURL)
- if err != nil {
- t.Fatal(err)
- }
- if esversion < "1.5.0" {
- t.Skip("InnerHits feature is only available for Elasticsearch 1.5+")
- return
- }
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- comment2a := comment{User: "sandrae", Comment: "What does that even mean?"}
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
- }
- comment3a := comment{User: "nico", Comment: "You bet."}
- comment3b := comment{User: "olivere", Comment: "It sure is."}
- // Add all documents
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("t1").BodyJson(&tweet1).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("t2").BodyJson(&tweet2).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("comment").Id("c2a").Parent("t2").BodyJson(&comment2a).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("t3").BodyJson(&tweet3).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("comment").Id("c3a").Parent("t3").BodyJson(&comment3a).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("comment").Id("c3b").Parent("t3").BodyJson(&comment3b).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Flush().Index(testIndexName).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- bq := NewBoolQuery()
- bq = bq.Must(NewMatchAllQuery())
- bq = bq.Filter(NewHasChildQuery("comment", NewMatchAllQuery()).
- InnerHit(NewInnerHit().Name("comments")))
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(bq).
- Pretty(true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 2 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 2, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 2 {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 2, len(searchResult.Hits.Hits))
- }
- hit := searchResult.Hits.Hits[0]
- if hit.Id != "t2" {
- t.Fatalf("expected tweet %q; got: %q", "t2", hit.Id)
- }
- if hit.InnerHits == nil {
- t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
- }
- if len(hit.InnerHits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
- }
- innerHits, found := hit.InnerHits["comments"]
- if !found {
- t.Fatalf("expected inner hits for name %q", "comments")
- }
- if innerHits == nil || innerHits.Hits == nil {
- t.Fatal("expected inner hits != nil")
- }
- if len(innerHits.Hits.Hits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
- }
- if innerHits.Hits.Hits[0].Id != "c2a" {
- t.Fatalf("expected inner hit with id %q; got: %q", "c2a", innerHits.Hits.Hits[0].Id)
- }
- hit = searchResult.Hits.Hits[1]
- if hit.Id != "t3" {
- t.Fatalf("expected tweet %q; got: %q", "t3", hit.Id)
- }
- if hit.InnerHits == nil {
- t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
- }
- if len(hit.InnerHits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
- }
- innerHits, found = hit.InnerHits["comments"]
- if !found {
- t.Fatalf("expected inner hits for name %q", "comments")
- }
- if innerHits == nil || innerHits.Hits == nil {
- t.Fatal("expected inner hits != nil")
- }
- if len(innerHits.Hits.Hits) != 2 {
- t.Fatalf("expected %d inner hits; got: %d", 2, len(innerHits.Hits.Hits))
- }
- if innerHits.Hits.Hits[0].Id != "c3a" {
- t.Fatalf("expected inner hit with id %q; got: %q", "c3a", innerHits.Hits.Hits[0].Id)
- }
- if innerHits.Hits.Hits[1].Id != "c3b" {
- t.Fatalf("expected inner hit with id %q; got: %q", "c3b", innerHits.Hits.Hits[1].Id)
- }
- }
- func TestSearchInnerHitsOnHasParent(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- // Check for valid ES version
- esversion, err := client.ElasticsearchVersion(DefaultURL)
- if err != nil {
- t.Fatal(err)
- }
- if esversion < "1.5.0" {
- t.Skip("InnerHits feature is only available for Elasticsearch 1.5+")
- return
- }
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- comment2a := comment{User: "sandrae", Comment: "What does that even mean?"}
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- Created: time.Date(2011, 11, 11, 10, 58, 12, 0, time.UTC),
- }
- comment3a := comment{User: "nico", Comment: "You bet."}
- comment3b := comment{User: "olivere", Comment: "It sure is."}
- // Add all documents
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("t1").BodyJson(&tweet1).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("t2").BodyJson(&tweet2).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("comment").Id("c2a").Parent("t2").BodyJson(&comment2a).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("tweet").Id("t3").BodyJson(&tweet3).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("comment").Id("c3a").Parent("t3").BodyJson(&comment3a).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Index().Index(testIndexName).Type("comment").Id("c3b").Parent("t3").BodyJson(&comment3b).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- _, err = client.Flush().Index(testIndexName).Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- bq := NewBoolQuery()
- bq = bq.Must(NewMatchAllQuery())
- bq = bq.Filter(NewHasParentQuery("tweet", NewMatchAllQuery()).
- InnerHit(NewInnerHit().Name("tweets")))
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(bq).
- Pretty(true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- if len(searchResult.Hits.Hits) != 3 {
- t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", 3, len(searchResult.Hits.Hits))
- }
- hit := searchResult.Hits.Hits[0]
- if hit.Id != "c2a" {
- t.Fatalf("expected tweet %q; got: %q", "c2a", hit.Id)
- }
- if hit.InnerHits == nil {
- t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
- }
- if len(hit.InnerHits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
- }
- innerHits, found := hit.InnerHits["tweets"]
- if !found {
- t.Fatalf("expected inner hits for name %q", "tweets")
- }
- if innerHits == nil || innerHits.Hits == nil {
- t.Fatal("expected inner hits != nil")
- }
- if len(innerHits.Hits.Hits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
- }
- if innerHits.Hits.Hits[0].Id != "t2" {
- t.Fatalf("expected inner hit with id %q; got: %q", "t2", innerHits.Hits.Hits[0].Id)
- }
- hit = searchResult.Hits.Hits[1]
- if hit.Id != "c3a" {
- t.Fatalf("expected tweet %q; got: %q", "c3a", hit.Id)
- }
- if hit.InnerHits == nil {
- t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
- }
- if len(hit.InnerHits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
- }
- innerHits, found = hit.InnerHits["tweets"]
- if !found {
- t.Fatalf("expected inner hits for name %q", "tweets")
- }
- if innerHits == nil || innerHits.Hits == nil {
- t.Fatal("expected inner hits != nil")
- }
- if len(innerHits.Hits.Hits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
- }
- if innerHits.Hits.Hits[0].Id != "t3" {
- t.Fatalf("expected inner hit with id %q; got: %q", "t3", innerHits.Hits.Hits[0].Id)
- }
- hit = searchResult.Hits.Hits[2]
- if hit.Id != "c3b" {
- t.Fatalf("expected tweet %q; got: %q", "c3b", hit.Id)
- }
- if hit.InnerHits == nil {
- t.Fatalf("expected inner hits; got: %v", hit.InnerHits)
- }
- if len(hit.InnerHits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(hit.InnerHits))
- }
- innerHits, found = hit.InnerHits["tweets"]
- if !found {
- t.Fatalf("expected inner hits for name %q", "tweets")
- }
- if innerHits == nil || innerHits.Hits == nil {
- t.Fatal("expected inner hits != nil")
- }
- if len(innerHits.Hits.Hits) != 1 {
- t.Fatalf("expected %d inner hits; got: %d", 1, len(innerHits.Hits.Hits))
- }
- if innerHits.Hits.Hits[0].Id != "t3" {
- t.Fatalf("expected inner hit with id %q; got: %q", "t3", innerHits.Hits.Hits[0].Id)
- }
- }
- func TestSearchBuildURL(t *testing.T) {
- client := setupTestClient(t)
- tests := []struct {
- Indices []string
- Types []string
- Expected string
- }{
- {
- []string{},
- []string{},
- "/_search",
- },
- {
- []string{"index1"},
- []string{},
- "/index1/_search",
- },
- {
- []string{"index1", "index2"},
- []string{},
- "/index1%2Cindex2/_search",
- },
- {
- []string{},
- []string{"type1"},
- "/_all/type1/_search",
- },
- {
- []string{"index1"},
- []string{"type1"},
- "/index1/type1/_search",
- },
- {
- []string{"index1", "index2"},
- []string{"type1", "type2"},
- "/index1%2Cindex2/type1%2Ctype2/_search",
- },
- {
- []string{},
- []string{"type1", "type2"},
- "/_all/type1%2Ctype2/_search",
- },
- }
- for i, test := range tests {
- path, _, err := client.Search().Index(test.Indices...).Type(test.Types...).buildURL()
- if err != nil {
- t.Errorf("case #%d: %v", i+1, err)
- continue
- }
- if path != test.Expected {
- t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
- }
- }
- }
- func TestSearchFilterPath(t *testing.T) {
- // client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
- client := setupTestClientAndCreateIndexAndAddDocs(t)
- // Match all should return all documents
- all := NewMatchAllQuery()
- searchResult, err := client.Search().
- Index(testIndexName).
- Type("tweet").
- Query(all).
- FilterPath(
- "took",
- "hits.hits._id",
- "hits.hits._source.user",
- "hits.hits._source.message",
- ).
- Timeout("1s").
- Pretty(true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Fatalf("expected SearchResult.Hits != nil; got nil")
- }
- // 0 because it was filtered out
- if want, got := int64(0), searchResult.Hits.TotalHits; want != got {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
- }
- if want, got := 3, len(searchResult.Hits.Hits); want != got {
- t.Fatalf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
- }
- for _, hit := range searchResult.Hits.Hits {
- if want, got := "", hit.Index; want != got {
- t.Fatalf("expected index %q, got %q", want, got)
- }
- item := make(map[string]interface{})
- err := json.Unmarshal(*hit.Source, &item)
- if err != nil {
- t.Fatal(err)
- }
- // user field
- v, found := item["user"]
- if !found {
- t.Fatalf("expected SearchResult.Hits.Hit[%q] to be found", "user")
- }
- if v == "" {
- t.Fatalf("expected user field, got %v (%T)", v, v)
- }
- // No retweets field
- v, found = item["retweets"]
- if found {
- t.Fatalf("expected SearchResult.Hits.Hit[%q] to not be found, got %v", "retweets", v)
- }
- if v == "" {
- t.Fatalf("expected user field, got %v (%T)", v, v)
- }
- }
- }
- func TestSearchAfter(t *testing.T) {
- // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere", Retweets: 108,
- Message: "Welcome to Golang and Elasticsearch.",
- Created: time.Date(2012, 12, 12, 17, 38, 34, 0, time.UTC),
- }
- tweet2 := tweet{
- User: "olivere", Retweets: 0,
- Message: "Another unrelated topic.",
- Created: time.Date(2012, 10, 10, 8, 12, 03, 0, time.UTC),
- }
- tweet3 := tweet{
- User: "sandrae", Retweets: 12,
- Message: "Cycling is fun.",
- 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)
- }
- searchResult, err := client.Search().
- Index(testIndexName).
- Query(NewMatchAllQuery()).
- SearchAfter("olivere").
- Sort("user", true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Errorf("expected SearchResult.Hits != nil; got nil")
- }
- if searchResult.Hits.TotalHits != 3 {
- t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult.Hits.TotalHits)
- }
- if want, got := 1, len(searchResult.Hits.Hits); want != got {
- t.Fatalf("expected len(SearchResult.Hits.Hits) = %d; got: %d", want, got)
- }
- hit := searchResult.Hits.Hits[0]
- if want, got := "3", hit.Id; want != got {
- t.Fatalf("expected tweet %q; got: %q", want, got)
- }
- }
- func TestSearchResultWithFieldCollapsing(t *testing.T) {
- client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
- searchResult, err := client.Search().
- Index(testIndexName).
- Type("tweet").
- Query(NewMatchAllQuery()).
- Collapse(NewCollapseBuilder("user")).
- Pretty(true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Fatalf("expected SearchResult.Hits != nil; got nil")
- }
- if got := searchResult.Hits.TotalHits; got == 0 {
- t.Fatalf("expected SearchResult.Hits.TotalHits > 0; got %d", got)
- }
- for _, hit := range searchResult.Hits.Hits {
- if hit.Index != testIndexName {
- t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
- }
- item := make(map[string]interface{})
- err := json.Unmarshal(*hit.Source, &item)
- if err != nil {
- t.Fatal(err)
- }
- if len(hit.Fields) == 0 {
- t.Fatal("expected fields in SearchResult")
- }
- usersVal, ok := hit.Fields["user"]
- if !ok {
- t.Fatalf("expected %q field in fields of SearchResult", "user")
- }
- users, ok := usersVal.([]interface{})
- if !ok {
- t.Fatalf("expected slice of strings in field of SearchResult, got %T", usersVal)
- }
- if len(users) != 1 {
- t.Fatalf("expected 1 entry in users slice, got %d", len(users))
- }
- }
- }
- func TestSearchResultWithFieldCollapsingAndInnerHits(t *testing.T) {
- client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
- searchResult, err := client.Search().
- Index(testIndexName).
- Type("tweet").
- Query(NewMatchAllQuery()).
- Collapse(
- NewCollapseBuilder("user").
- InnerHit(
- NewInnerHit().Name("last_tweets").Size(5).Sort("created", true),
- ).
- MaxConcurrentGroupRequests(4)).
- Pretty(true).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Hits == nil {
- t.Fatalf("expected SearchResult.Hits != nil; got nil")
- }
- if got := searchResult.Hits.TotalHits; got == 0 {
- t.Fatalf("expected SearchResult.Hits.TotalHits > 0; got %d", got)
- }
- for _, hit := range searchResult.Hits.Hits {
- if hit.Index != testIndexName {
- t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
- }
- item := make(map[string]interface{})
- err := json.Unmarshal(*hit.Source, &item)
- if err != nil {
- t.Fatal(err)
- }
- if len(hit.Fields) == 0 {
- t.Fatal("expected fields in SearchResult")
- }
- usersVal, ok := hit.Fields["user"]
- if !ok {
- t.Fatalf("expected %q field in fields of SearchResult", "user")
- }
- users, ok := usersVal.([]interface{})
- if !ok {
- t.Fatalf("expected slice of strings in field of SearchResult, got %T", usersVal)
- }
- if len(users) != 1 {
- t.Fatalf("expected 1 entry in users slice, got %d", len(users))
- }
- lastTweets, ok := hit.InnerHits["last_tweets"]
- if !ok {
- t.Fatalf("expected inner_hits named %q in SearchResult", "last_tweets")
- }
- if lastTweets == nil {
- t.Fatal("expected inner_hits in SearchResult")
- }
- }
- }
|