diff --git a/elastic/query.go b/elastic/query.go index ded4f51..419c67c 100644 --- a/elastic/query.go +++ b/elastic/query.go @@ -9,7 +9,7 @@ func Ids(values ...string) Query { // Term is a shortcut for a term query func Term(field string, value any) Query { - return Query{"term": map[string]any{field: value}} + return Query{"term": map[string]any{field: map[string]any{"value": value}}} } // Exists is a shortcut for an exists query @@ -32,10 +32,7 @@ func GreaterThan(field string, value any) Query { return Query{ "range": map[string]any{ field: map[string]any{ - "from": value, - "include_lower": false, - "include_upper": true, - "to": nil, + "gt": value, }, }, } @@ -46,10 +43,7 @@ func GreaterThanOrEqual(field string, value any) Query { return Query{ "range": map[string]any{ field: map[string]any{ - "from": value, - "include_lower": true, - "include_upper": true, - "to": nil, + "gte": value, }, }, } @@ -60,10 +54,7 @@ func LessThan(field string, value any) Query { return Query{ "range": map[string]any{ field: map[string]any{ - "from": nil, - "include_lower": true, - "include_upper": false, - "to": value, + "lt": value, }, }, } @@ -74,10 +65,7 @@ func LessThanOrEqual(field string, value any) Query { return Query{ "range": map[string]any{ field: map[string]any{ - "from": nil, - "include_lower": true, - "include_upper": true, - "to": value, + "lte": value, }, }, } @@ -88,10 +76,8 @@ func Between(field string, from, to any) Query { return Query{ "range": map[string]any{ field: map[string]any{ - "from": from, - "include_lower": true, - "include_upper": false, - "to": to, + "gte": from, + "lt": to, }, }, } diff --git a/elastic/query_test.go b/elastic/query_test.go index 1da3fbf..02f2968 100644 --- a/elastic/query_test.go +++ b/elastic/query_test.go @@ -14,22 +14,22 @@ func TestQuery(t *testing.T) { json []byte }{ {elastic.Ids("235", "465", "787"), []byte(`{"ids": {"values": ["235", "465", "787"]}}`)}, - {elastic.Term("age", 42), []byte(`{"term": {"age": 42}}`)}, + {elastic.Term("age", 42), []byte(`{"term": {"age": {"value":42}}}`)}, {elastic.Exists("age"), []byte(`{"exists": {"field": "age"}}`)}, {elastic.Match("name", "Bob"), []byte(`{"match": {"name": {"query": "Bob"}}}`)}, {elastic.MatchPhrase("name", "Bob"), []byte(`{"match_phrase": {"name": {"query": "Bob"}}}`)}, - {elastic.GreaterThan("age", 45), []byte(`{"range": {"age": {"from": 45, "include_lower": false, "include_upper": true, "to": null}}}`)}, - {elastic.GreaterThanOrEqual("age", 45), []byte(`{"range": {"age": {"from": 45, "include_lower": true, "include_upper": true, "to": null}}}`)}, - {elastic.LessThan("age", 45), []byte(`{"range": {"age": {"from": null, "include_lower": true, "include_upper": false, "to": 45}}}`)}, - {elastic.LessThanOrEqual("age", 45), []byte(`{"range": {"age": {"from": null, "include_lower": true, "include_upper": true, "to": 45}}}`)}, - {elastic.Between("age", 20, 45), []byte(`{"range": {"age": {"from": 20, "include_lower": true, "include_upper": false, "to": 45}}}`)}, + {elastic.GreaterThan("age", 45), []byte(`{"range": {"age": {"gt": 45}}}`)}, + {elastic.GreaterThanOrEqual("age", 45), []byte(`{"range": {"age": {"gte": 45}}}`)}, + {elastic.LessThan("age", 45), []byte(`{"range": {"age": {"lt": 45}}}`)}, + {elastic.LessThanOrEqual("age", 45), []byte(`{"range": {"age": {"lte": 45}}}`)}, + {elastic.Between("age", 20, 45), []byte(`{"range": {"age": {"gte": 20, "lt": 45}}}`)}, { elastic.Any(elastic.Ids("235"), elastic.Term("age", 42)), - []byte(`{"bool": {"should": [{"ids": {"values": ["235"]}}, {"term": {"age": 42}}]}}`), + []byte(`{"bool": {"should": [{"ids": {"values": ["235"]}}, {"term": {"age": {"value":42}}}]}}`), }, { elastic.All(elastic.Ids("235"), elastic.Term("age", 42)), - []byte(`{"bool": {"must": [{"ids": {"values": ["235"]}}, {"term": {"age": 42}}]}}`), + []byte(`{"bool": {"must": [{"ids": {"values": ["235"]}}, {"term": {"age": {"value":42}}}]}}`), }, { elastic.Not(elastic.Ids("235")), @@ -37,7 +37,7 @@ func TestQuery(t *testing.T) { }, { elastic.Bool([]elastic.Query{elastic.Ids("235"), elastic.Term("age", 42)}, []elastic.Query{elastic.Exists("age")}), - []byte(`{"bool": {"must": [{"ids": {"values": ["235"]}}, {"term": {"age": 42}}], "must_not": [{"exists": {"field": "age"}}]}}`), + []byte(`{"bool": {"must": [{"ids": {"values": ["235"]}}, {"term": {"age": {"value":42}}}], "must_not": [{"exists": {"field": "age"}}]}}`), }, { elastic.Bool([]elastic.Query{}, []elastic.Query{elastic.Exists("age")}), @@ -45,9 +45,9 @@ func TestQuery(t *testing.T) { }, { elastic.Bool([]elastic.Query{elastic.Ids("235"), elastic.Term("age", 42)}, []elastic.Query{}), - []byte(`{"bool": {"must": [{"ids": {"values": ["235"]}}, {"term": {"age": 42}}]}}`), + []byte(`{"bool": {"must": [{"ids": {"values": ["235"]}}, {"term": {"age": {"value":42}}}]}}`), }, - {elastic.Nested("group", elastic.Term("group.id", 10)), []byte(`{"nested": {"path": "group", "query": {"term": {"group.id": 10}}}}`)}, + {elastic.Nested("group", elastic.Term("group.id", 10)), []byte(`{"nested": {"path": "group", "query": {"term": {"group.id": {"value":10}}}}}`)}, } for i, tc := range tcs { diff --git a/elastic/sort_test.go b/elastic/sort_test.go index 86df842..45e2c16 100644 --- a/elastic/sort_test.go +++ b/elastic/sort_test.go @@ -17,7 +17,7 @@ func TestSort(t *testing.T) { {elastic.SortBy("name", false), []byte(`{"name": {"order": "desc"}}`)}, { elastic.SortNested("age", elastic.Term("fields.field", "1234"), "fields", true), - []byte(`{"age": {"nested": {"filter": {"term": {"fields.field": "1234"}}, "path": "fields"}, "order":"asc"}}`), + []byte(`{"age": {"nested": {"filter": {"term": {"fields.field": {"value":"1234"}}}, "path": "fields"}, "order":"asc"}}`), }, }