Skip to content

Commit

Permalink
Update elastic query DSL syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed May 22, 2024
1 parent d00a4d4 commit 44581e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
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}}}
}

// 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,
},
},
}
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
10 changes: 5 additions & 5 deletions elastic/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func TestQuery(t *testing.T) {
{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}}]}}`),
Expand Down

0 comments on commit 44581e5

Please sign in to comment.