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

RangeQuery gt, gte, lt, lte with backward compatibility #1453

Closed
Closed
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
6 changes: 3 additions & 3 deletions search_aggs_bucket_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestFilterAggregation(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"filter":{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}}}`
expected := `{"filter":{"range":{"stock":{"gt":0}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand All @@ -41,7 +41,7 @@ func TestFilterAggregationWithSubAggregation(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"aggregations":{"avg_price":{"avg":{"field":"price"}}},"filter":{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}}}`
expected := `{"aggregations":{"avg_price":{"avg":{"field":"price"}}},"filter":{"range":{"stock":{"gt":0}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand All @@ -59,7 +59,7 @@ func TestFilterAggregationWithMeta(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"filter":{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}},"meta":{"name":"Oliver"}}`
expected := `{"filter":{"range":{"stock":{"gt":0}}},"meta":{"name":"Oliver"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down
8 changes: 4 additions & 4 deletions search_aggs_bucket_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFiltersAggregationFilters(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"filters":{"filters":[{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}},{"term":{"symbol":"GOOG"}}]}}`
expected := `{"filters":{"filters":[{"range":{"stock":{"gt":0}}},{"term":{"symbol":"GOOG"}}]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand All @@ -43,7 +43,7 @@ func TestFiltersAggregationFilterWithName(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"filters":{"filters":{"f1":{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}},"f2":{"term":{"symbol":"GOOG"}}}}}`
expected := `{"filters":{"filters":{"f1":{"range":{"stock":{"gt":0}}},"f2":{"term":{"symbol":"GOOG"}}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestFiltersAggregationWithSubAggregation(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"aggregations":{"avg_price":{"avg":{"field":"price"}}},"filters":{"filters":[{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}},{"term":{"symbol":"GOOG"}}]}}`
expected := `{"aggregations":{"avg_price":{"avg":{"field":"price"}}},"filters":{"filters":[{"range":{"stock":{"gt":0}}},{"term":{"symbol":"GOOG"}}]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand All @@ -113,7 +113,7 @@ func TestFiltersAggregationWithMetaData(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"filters":{"filters":[{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}},{"term":{"symbol":"GOOG"}}]},"meta":{"name":"Oliver"}}`
expected := `{"filters":{"filters":[{"range":{"stock":{"gt":0}}},{"term":{"symbol":"GOOG"}}]},"meta":{"name":"Oliver"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down
2 changes: 1 addition & 1 deletion search_queries_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBoolQuery(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"bool":{"_name":"Test","boost":10,"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"}}]}}`
expected := `{"bool":{"_name":"Test","boost":10,"filter":{"term":{"account":"1"}},"must":{"term":{"tag":"wow"}},"must_not":{"range":{"age":{"gte":10,"lte":20}}},"should":[{"term":{"tag":"sometag"}},{"term":{"tag":"sometagtag"}}]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down
2 changes: 1 addition & 1 deletion search_queries_boosting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestBoostingQuery(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"boosting":{"negative":{"range":{"age":{"from":10,"include_lower":true,"include_upper":true,"to":20}}},"negative_boost":0.2,"positive":{"term":{"tag":"wow"}}}}`
expected := `{"boosting":{"negative":{"range":{"age":{"gte":10,"lte":20}}},"negative_boost":0.2,"positive":{"term":{"tag":"wow"}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down
4 changes: 2 additions & 2 deletions search_queries_nested_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNestedQuery(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"nested":{"_name":"qname","path":"obj1","query":{"bool":{"must":[{"term":{"obj1.name":"blue"}},{"range":{"obj1.count":{"from":5,"include_lower":false,"include_upper":true,"to":null}}}]}}}}`
expected := `{"nested":{"_name":"qname","path":"obj1","query":{"bool":{"must":[{"term":{"obj1.name":"blue"}},{"range":{"obj1.count":{"gt":5}}}]}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand All @@ -45,7 +45,7 @@ func TestNestedQueryWithInnerHit(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"nested":{"_name":"qname","inner_hits":{"name":"comments","query":{"term":{"user":"olivere"}}},"path":"obj1","query":{"bool":{"must":[{"term":{"obj1.name":"blue"}},{"range":{"obj1.count":{"from":5,"include_lower":false,"include_upper":true,"to":null}}}]}}}}`
expected := `{"nested":{"_name":"qname","inner_hits":{"name":"comments","query":{"term":{"user":"olivere"}}},"path":"obj1","query":{"bool":{"must":[{"term":{"obj1.name":"blue"}},{"range":{"obj1.count":{"gt":5}}}]}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down
138 changes: 89 additions & 49 deletions search_queries_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

package elastic

// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-range-query.html#querying-range-fields
const (
// (Default) Matches documents with a range field value that intersects the query’s range.
RelationIntersects string = "INTERSECTS"
// Matches documents with a range field value that entirely contains the query’s range.
RelationContains string = "CONTAINS"
// Matches documents with a range field value entirely within the query’s range.
RelationWithin string = "WITHIN"
)

// RangeQuery matches documents with fields that have terms within a certain range.
//
// For details, see
// For details, see Elastic Documentation (6.8):
// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-range-query.html
type RangeQuery struct {
name string
from interface{}
to interface{}
gt interface{}
gte interface{}
lt interface{}
lte interface{}
timeZone string
includeLower bool
includeUpper bool
Expand All @@ -26,63 +38,31 @@ func NewRangeQuery(name string) *RangeQuery {
return &RangeQuery{name: name, includeLower: true, includeUpper: true}
}

// From indicates the from part of the RangeQuery.
// Use nil to indicate an unbounded from part.
func (q *RangeQuery) From(from interface{}) *RangeQuery {
q.from = from
return q
}

// Gt indicates a greater-than value for the from part.
// Use nil to indicate an unbounded from part.
func (q *RangeQuery) Gt(from interface{}) *RangeQuery {
q.from = from
q.includeLower = false
func (q *RangeQuery) Gt(gt interface{}) *RangeQuery {
q.gt = gt
return q
}

// Gte indicates a greater-than-or-equal value for the from part.
// Use nil to indicate an unbounded from part.
func (q *RangeQuery) Gte(from interface{}) *RangeQuery {
q.from = from
q.includeLower = true
return q
}

// To indicates the to part of the RangeQuery.
// Use nil to indicate an unbounded to part.
func (q *RangeQuery) To(to interface{}) *RangeQuery {
q.to = to
func (q *RangeQuery) Gte(gte interface{}) *RangeQuery {
q.gte = gte
return q
}

// Lt indicates a less-than value for the to part.
// Use nil to indicate an unbounded to part.
func (q *RangeQuery) Lt(to interface{}) *RangeQuery {
q.to = to
q.includeUpper = false
func (q *RangeQuery) Lt(lt interface{}) *RangeQuery {
q.lt = lt
return q
}

// Lte indicates a less-than-or-equal value for the to part.
// Use nil to indicate an unbounded to part.
func (q *RangeQuery) Lte(to interface{}) *RangeQuery {
q.to = to
q.includeUpper = true
return q
}

// IncludeLower indicates whether the lower bound should be included or not.
// Defaults to true.
func (q *RangeQuery) IncludeLower(includeLower bool) *RangeQuery {
q.includeLower = includeLower
return q
}

// IncludeUpper indicates whether the upper bound should be included or not.
// Defaults to true.
func (q *RangeQuery) IncludeUpper(includeUpper bool) *RangeQuery {
q.includeUpper = includeUpper
func (q *RangeQuery) Lte(lte interface{}) *RangeQuery {
q.lte = lte
return q
}

Expand Down Expand Up @@ -114,12 +94,65 @@ func (q *RangeQuery) Format(format string) *RangeQuery {
}

// Relation is used for range fields. which can be one of
// "within", "contains", "intersects" (default) and "disjoint".
// "within", "contains" and "intersects" (default).
func (q *RangeQuery) Relation(relation string) *RangeQuery {
q.relation = relation
return q
}

// From Deprecated use Gt or Gte
func (q *RangeQuery) From(from interface{}) *RangeQuery {
if q.includeLower {
q.gte = from
q.gt = nil
return q
}
q.gte = nil
q.gt = from
return q
}

// To Deprecated use Lt or Lte
func (q *RangeQuery) To(to interface{}) *RangeQuery {
if q.includeUpper {
q.lte = to
q.lt = nil
return q
}
q.lte = nil
q.lt = to
return q
}

// IncludeLower Deprecated use Gt or Gte
func (q *RangeQuery) IncludeLower(includeLower bool) *RangeQuery {
if includeLower && q.gt != nil {
q.gte = q.gt
q.gt = nil
}
if !includeLower && q.gte != nil {
q.gt = q.gte
q.gte = nil
}
q.includeLower = includeLower
return q
}

// IncludeUpper Deprecated use Lt or Lte
func (q *RangeQuery) IncludeUpper(includeUpper bool) *RangeQuery {
if includeUpper && q.lt != nil {
q.lte = q.lt
q.lt = nil
}
if !includeUpper && q.lte != nil {
q.lt = q.lte
q.lte = nil
}

q.includeUpper = includeUpper
return q
}

// Source returns JSON for the query.
func (q *RangeQuery) Source() (interface{}, error) {
source := make(map[string]interface{})
Expand All @@ -130,8 +163,18 @@ func (q *RangeQuery) Source() (interface{}, error) {
params := make(map[string]interface{})
rangeQ[q.name] = params

params["from"] = q.from
params["to"] = q.to
if q.gt != nil {
params["gt"] = q.gt
}
if q.gte != nil {
params["gte"] = q.gte
}
if q.lt != nil {
params["lt"] = q.lt
}
if q.lte != nil {
params["lte"] = q.lte
}
if q.timeZone != "" {
params["time_zone"] = q.timeZone
}
Expand All @@ -144,9 +187,6 @@ func (q *RangeQuery) Source() (interface{}, error) {
if q.boost != nil {
params["boost"] = *q.boost
}
params["include_lower"] = q.includeLower
params["include_upper"] = q.includeUpper

if q.queryName != "" {
rangeQ["_name"] = q.queryName
}
Expand Down
Loading