search_queries_indices_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. "encoding/json"
  7. "testing"
  8. )
  9. func TestIndicesQuery(t *testing.T) {
  10. q := NewIndicesQuery(NewTermQuery("tag", "wow"), "index1", "index2")
  11. q = q.NoMatchQuery(NewTermQuery("tag", "kow"))
  12. src, err := q.Source()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. data, err := json.Marshal(src)
  17. if err != nil {
  18. t.Fatalf("marshaling to JSON failed: %v", err)
  19. }
  20. got := string(data)
  21. expected := `{"indices":{"indices":["index1","index2"],"no_match_query":{"term":{"tag":"kow"}},"query":{"term":{"tag":"wow"}}}}`
  22. if got != expected {
  23. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  24. }
  25. }
  26. func TestIndicesQueryWithNoMatchQueryType(t *testing.T) {
  27. q := NewIndicesQuery(NewTermQuery("tag", "wow"), "index1", "index2")
  28. q = q.NoMatchQueryType("all")
  29. src, err := q.Source()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. data, err := json.Marshal(src)
  34. if err != nil {
  35. t.Fatalf("marshaling to JSON failed: %v", err)
  36. }
  37. got := string(data)
  38. expected := `{"indices":{"indices":["index1","index2"],"no_match_query":"all","query":{"term":{"tag":"wow"}}}}`
  39. if got != expected {
  40. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  41. }
  42. }