search_queries_bool_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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 TestBoolQuery(t *testing.T) {
  10. q := NewBoolQuery()
  11. q = q.Must(NewTermQuery("tag", "wow"))
  12. q = q.MustNot(NewRangeQuery("age").From(10).To(20))
  13. q = q.Filter(NewTermQuery("account", "1"))
  14. q = q.Should(NewTermQuery("tag", "sometag"), NewTermQuery("tag", "sometagtag"))
  15. q = q.Boost(10)
  16. q = q.DisableCoord(true)
  17. q = q.QueryName("Test")
  18. src, err := q.Source()
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. data, err := json.Marshal(src)
  23. if err != nil {
  24. t.Fatalf("marshaling to JSON failed: %v", err)
  25. }
  26. got := string(data)
  27. expected := `{"bool":{"_name":"Test","boost":10,"disable_coord":true,"filter":{"term":{"account":"1"}},"must":{"term":{"tag":"wow"}},"must_not":{"range":{"age":{"from":10,"include_lower":true,"include_upper":true,"to":20}}},"should":[{"term":{"tag":"sometag"}},{"term":{"tag":"sometagtag"}}]}}`
  28. if got != expected {
  29. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  30. }
  31. }