search_queries_bool_test.go 1007 B

1234567891011121314151617181920212223242526272829
  1. // Copyright 2012-2015 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.Should(NewTermQuery("tag", "sometag"), NewTermQuery("tag", "sometagtag"))
  14. q = q.Boost(10)
  15. q = q.DisableCoord(true)
  16. q = q.QueryName("Test")
  17. data, err := json.Marshal(q.Source())
  18. if err != nil {
  19. t.Fatalf("marshaling to JSON failed: %v", err)
  20. }
  21. got := string(data)
  22. expected := `{"bool":{"_name":"Test","boost":10,"disable_coord":true,"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"}}]}}`
  23. if got != expected {
  24. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  25. }
  26. }