explain_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 TestExplain(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  12. // Add a document
  13. indexResult, err := client.Index().
  14. Index(testIndexName).
  15. Type("tweet").
  16. Id("1").
  17. BodyJson(&tweet1).
  18. Refresh("true").
  19. Do(context.TODO())
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if indexResult == nil {
  24. t.Errorf("expected result to be != nil; got: %v", indexResult)
  25. }
  26. // Explain
  27. query := NewTermQuery("user", "olivere")
  28. expl, err := client.Explain(testIndexName, "tweet", "1").Query(query).Do(context.TODO())
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if expl == nil {
  33. t.Fatal("expected to return an explanation")
  34. }
  35. if !expl.Matched {
  36. t.Errorf("expected matched to be %v; got: %v", true, expl.Matched)
  37. }
  38. }