123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- // 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"
- _ "net/http"
- "testing"
- )
- func TestMultiSearch(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere",
- Message: "Welcome to Golang and Elasticsearch.",
- Tags: []string{"golang", "elasticsearch"},
- }
- tweet2 := tweet{
- User: "olivere",
- Message: "Another unrelated topic.",
- Tags: []string{"golang"},
- }
- tweet3 := tweet{
- User: "sandrae",
- Message: "Cycling is fun.",
- Tags: []string{"sports", "cycling"},
- }
- // 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)
- }
- // Spawn two search queries with one roundtrip
- q1 := NewMatchAllQuery()
- q2 := NewTermQuery("tags", "golang")
- sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2).
- Source(NewSearchSource().Query(q1).Size(10))
- sreq2 := NewSearchRequest().Index(testIndexName).Type("tweet").
- Source(NewSearchSource().Query(q2))
- searchResult, err := client.MultiSearch().
- Add(sreq1, sreq2).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Responses == nil {
- t.Fatal("expected responses != nil; got nil")
- }
- if len(searchResult.Responses) != 2 {
- t.Fatalf("expected 2 responses; got %d", len(searchResult.Responses))
- }
- sres := searchResult.Responses[0]
- if sres.Hits == nil {
- t.Errorf("expected Hits != nil; got nil")
- }
- if sres.Hits.TotalHits != 3 {
- t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits)
- }
- if len(sres.Hits.Hits) != 3 {
- t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits))
- }
- for _, hit := range sres.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected 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)
- }
- }
- sres = searchResult.Responses[1]
- if sres.Hits == nil {
- t.Errorf("expected Hits != nil; got nil")
- }
- if sres.Hits.TotalHits != 2 {
- t.Errorf("expected Hits.TotalHits = %d; got %d", 2, sres.Hits.TotalHits)
- }
- if len(sres.Hits.Hits) != 2 {
- t.Errorf("expected len(Hits.Hits) = %d; got %d", 2, len(sres.Hits.Hits))
- }
- for _, hit := range sres.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected 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 TestMultiSearchWithStrings(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- // client := setupTestClientAndCreateIndexAndLog(t)
- tweet1 := tweet{
- User: "olivere",
- Message: "Welcome to Golang and Elasticsearch.",
- Tags: []string{"golang", "elasticsearch"},
- }
- tweet2 := tweet{
- User: "olivere",
- Message: "Another unrelated topic.",
- Tags: []string{"golang"},
- }
- tweet3 := tweet{
- User: "sandrae",
- Message: "Cycling is fun.",
- Tags: []string{"sports", "cycling"},
- }
- // 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)
- }
- // Spawn two search queries with one roundtrip
- sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2).
- Source(`{"query":{"match_all":{}}}`)
- sreq2 := NewSearchRequest().Index(testIndexName).Type("tweet").
- Source(`{"query":{"term":{"tags":"golang"}}}`)
- searchResult, err := client.MultiSearch().
- Add(sreq1, sreq2).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Responses == nil {
- t.Fatal("expected responses != nil; got nil")
- }
- if len(searchResult.Responses) != 2 {
- t.Fatalf("expected 2 responses; got %d", len(searchResult.Responses))
- }
- sres := searchResult.Responses[0]
- if sres.Hits == nil {
- t.Errorf("expected Hits != nil; got nil")
- }
- if sres.Hits.TotalHits != 3 {
- t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits)
- }
- if len(sres.Hits.Hits) != 3 {
- t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits))
- }
- for _, hit := range sres.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected 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)
- }
- }
- sres = searchResult.Responses[1]
- if sres.Hits == nil {
- t.Errorf("expected Hits != nil; got nil")
- }
- if sres.Hits.TotalHits != 2 {
- t.Errorf("expected Hits.TotalHits = %d; got %d", 2, sres.Hits.TotalHits)
- }
- if len(sres.Hits.Hits) != 2 {
- t.Errorf("expected len(Hits.Hits) = %d; got %d", 2, len(sres.Hits.Hits))
- }
- for _, hit := range sres.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected 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 TestMultiSearchWithOneRequest(t *testing.T) {
- client := setupTestClientAndCreateIndex(t)
- tweet1 := tweet{
- User: "olivere",
- Message: "Welcome to Golang and Elasticsearch.",
- Tags: []string{"golang", "elasticsearch"},
- }
- tweet2 := tweet{
- User: "olivere",
- Message: "Another unrelated topic.",
- Tags: []string{"golang"},
- }
- tweet3 := tweet{
- User: "sandrae",
- Message: "Cycling is fun.",
- Tags: []string{"sports", "cycling"},
- }
- // 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)
- }
- // Spawn two search queries with one roundtrip
- query := NewMatchAllQuery()
- source := NewSearchSource().Query(query).Size(10)
- sreq := NewSearchRequest().Source(source)
- searchResult, err := client.MultiSearch().
- Index(testIndexName).
- Add(sreq).
- Do(context.TODO())
- if err != nil {
- t.Fatal(err)
- }
- if searchResult.Responses == nil {
- t.Fatal("expected responses != nil; got nil")
- }
- if len(searchResult.Responses) != 1 {
- t.Fatalf("expected 1 responses; got %d", len(searchResult.Responses))
- }
- sres := searchResult.Responses[0]
- if sres.Hits == nil {
- t.Errorf("expected Hits != nil; got nil")
- }
- if sres.Hits.TotalHits != 3 {
- t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits)
- }
- if len(sres.Hits.Hits) != 3 {
- t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits))
- }
- for _, hit := range sres.Hits.Hits {
- if hit.Index != testIndexName {
- t.Errorf("expected 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)
- }
- }
- }
|