percolate_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "testing"
  8. )
  9. func TestPercolate(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
  11. // Add a document
  12. _, err := client.Index().
  13. Index(testIndexName).
  14. Type("queries").
  15. Id("1").
  16. BodyJson(`{"query":{"match":{"message":"bonsai tree"}}}`).
  17. Refresh("wait_for").
  18. Do(context.TODO())
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. // Percolate should return our registered query
  23. pq := NewPercolatorQuery().
  24. Field("query").
  25. DocumentType("doctype").
  26. Document(doctype{Message: "A new bonsai tree in the office"})
  27. res, err := client.Search(testIndexName).Query(pq).Do(context.TODO())
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if res == nil {
  32. t.Fatal("expected results != nil; got nil")
  33. }
  34. if res.Hits == nil {
  35. t.Fatal("expected SearchResult.Hits != nil; got nil")
  36. }
  37. if got, want := res.Hits.TotalHits, int64(1); got != want {
  38. t.Fatalf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
  39. }
  40. if got, want := len(res.Hits.Hits), 1; got != want {
  41. t.Fatalf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
  42. }
  43. hit := res.Hits.Hits[0]
  44. if hit.Index != testIndexName {
  45. t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
  46. }
  47. got := string(*hit.Source)
  48. expected := `{"query":{"match":{"message":"bonsai tree"}}}`
  49. if got != expected {
  50. t.Fatalf("expected\n%s\n,got:\n%s", expected, got)
  51. }
  52. }