| 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 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestBoolQuery(t *testing.T) { |
| 13 |
q := NewBoolQuery() |
| 14 |
q = q.Must(NewTermQuery("tag", "wow")) |
| 15 |
q = q.MustNot(NewRangeQuery("age").From(10).To(20)) |
| 16 |
q = q.Filter(NewTermQuery("account", "1")) |
| 17 |
q = q.Should(NewTermQuery("tag", "sometag"), NewTermQuery("tag", "sometagtag")) |
| 18 |
q = q.Boost(10) |
| 19 |
q = q.DisableCoord(true) |
| 20 |
q = q.QueryName("Test") |
| 21 |
src, err := q.Source() |
| 22 |
if err != nil { |
| 23 |
t.Fatal(err) |
| 24 |
} |
| 25 |
data, err := json.Marshal(src) |
| 26 |
if err != nil { |
| 27 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 28 |
} |
| 29 |
got := string(data) |
| 30 |
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"}}]}}` |
| 31 |
if got != expected { |
| 32 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 33 |
} |
| 34 |
} |