Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update elastic query DSL syntax #129

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions elastic/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// Exists is a shortcut for an exists query
Expand All @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
},
}
Expand All @@ -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,
},
},
}
Expand All @@ -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,
},
},
}
Expand All @@ -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,
},
},
}
Expand All @@ -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,
},
},
}
Expand Down
22 changes: 11 additions & 11 deletions elastic/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@ 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")),
[]byte(`{"bool": {"must_not": {"ids": {"values": ["235"]}}}}`),
},
{
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")}),
[]byte(`{"bool": {"must_not": [{"exists": {"field": "age"}}]}}`),
},
{
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 {
Expand Down
2 changes: 1 addition & 1 deletion elastic/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}`),
},
}

Expand Down
Loading