From 78db88897c0e20a5e1a6d84ca56491814b8f1e7a Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Mon, 25 Aug 2014 17:47:27 -0400 Subject: [PATCH] added control over inclusive/exclusive numeric/date ranges --- examples/beer-search/static/js/search.js | 8 +++++++ .../static/partials/search/date_range.html | 12 ++++++++++ .../static/partials/search/numeric_range.html | 12 ++++++++++ query_date_range.go | 16 +++++++++---- query_numeric_range.go | 24 ++++++++++++------- search/search_numeric_range.go | 16 ++++++++++++- 6 files changed, 75 insertions(+), 13 deletions(-) diff --git a/examples/beer-search/static/js/search.js b/examples/beer-search/static/js/search.js index ffaf4b99..959886e8 100644 --- a/examples/beer-search/static/js/search.js +++ b/examples/beer-search/static/js/search.js @@ -1,5 +1,9 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) { + $scope.inclusiveMin = true; + $scope.inclusiveMax = false; + $scope.inclusiveStart = true; + $scope.inclusiveEnd = false; $scope.fieldNames = []; $scope.minShouldOptions = []; @@ -79,6 +83,8 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) { "query": { "min": parseFloat($scope.min), "max": parseFloat($scope.max), + "inclusive_min": $scope.inclusiveMin, + "inclusive_max": $scope.inclusiveMax, "field": $scope.field, } }). @@ -98,6 +104,8 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) { "query": { "start": $scope.startDate, "end": $scope.endDate, + "inclusive_start": $scope.inclusiveStart, + "inclusive_end": $scope.inclusiveEnd, "field": $scope.field, } }). diff --git a/examples/beer-search/static/partials/search/date_range.html b/examples/beer-search/static/partials/search/date_range.html index d61b54ba..23e40c7c 100644 --- a/examples/beer-search/static/partials/search/date_range.html +++ b/examples/beer-search/static/partials/search/date_range.html @@ -15,12 +15,24 @@ +
+ +
+ +
+
+
+ +
+ +
+
diff --git a/examples/beer-search/static/partials/search/numeric_range.html b/examples/beer-search/static/partials/search/numeric_range.html index 1d899e61..e1684f96 100644 --- a/examples/beer-search/static/partials/search/numeric_range.html +++ b/examples/beer-search/static/partials/search/numeric_range.html @@ -15,12 +15,24 @@
+
+ +
+ +
+
+
+ +
+ +
+
diff --git a/query_date_range.go b/query_date_range.go index 2fd4683b..6df24e13 100644 --- a/query_date_range.go +++ b/query_date_range.go @@ -19,16 +19,24 @@ import ( type DateRangeQuery struct { Start *string `json:"start,omitempty"` End *string `json:"end,omitempty"` + InclusiveStart *bool `json:"inclusive_start,omitempty"` + InclusiveEnd *bool `json:"inclusive_end,omitempty"` FieldVal string `json:"field,omitempty"` BoostVal float64 `json:"boost,omitempty"` DateTimeParser *string `json:"datetime_parser,omitempty"` } func NewDateRangeQuery(start, end *string) *DateRangeQuery { + return NewDateRangeInclusiveQuery(start, end, nil, nil) +} + +func NewDateRangeInclusiveQuery(start, end *string, startInclusive, endInclusive *bool) *DateRangeQuery { return &DateRangeQuery{ - Start: start, - End: end, - BoostVal: 1.0, + Start: start, + End: end, + InclusiveStart: startInclusive, + InclusiveEnd: endInclusive, + BoostVal: 1.0, } } @@ -86,7 +94,7 @@ func (q *DateRangeQuery) Searcher(i *indexImpl, explain bool) (search.Searcher, max = numeric_util.Int64ToFloat64(endTime.UnixNano()) } - return search.NewNumericRangeSearcher(i.i, &min, &max, field, q.BoostVal, explain) + return search.NewNumericRangeSearcher(i.i, &min, &max, q.InclusiveStart, q.InclusiveEnd, field, q.BoostVal, explain) } func (q *DateRangeQuery) Validate() error { diff --git a/query_numeric_range.go b/query_numeric_range.go index 6ab446c2..98c15d74 100644 --- a/query_numeric_range.go +++ b/query_numeric_range.go @@ -13,17 +13,25 @@ import ( ) type NumericRangeQuery struct { - Min *float64 `json:"min,omitempty"` - Max *float64 `json:"max,omitempty"` - FieldVal string `json:"field,omitempty"` - BoostVal float64 `json:"boost,omitempty"` + Min *float64 `json:"min,omitempty"` + Max *float64 `json:"max,omitempty"` + InclusiveMin *bool `json:"inclusive_min,omitempty"` + InclusiveMax *bool `json:"inclusive_max,omitempty"` + FieldVal string `json:"field,omitempty"` + BoostVal float64 `json:"boost,omitempty"` } func NewNumericRangeQuery(min, max *float64) *NumericRangeQuery { + return NewNumericRangeInclusiveQuery(min, max, nil, nil) +} + +func NewNumericRangeInclusiveQuery(min, max *float64, minInclusive, maxInclusive *bool) *NumericRangeQuery { return &NumericRangeQuery{ - Min: min, - Max: max, - BoostVal: 1.0, + Min: min, + Max: max, + InclusiveMin: minInclusive, + InclusiveMax: maxInclusive, + BoostVal: 1.0, } } @@ -50,7 +58,7 @@ func (q *NumericRangeQuery) Searcher(i *indexImpl, explain bool) (search.Searche if q.FieldVal == "" { field = i.m.DefaultField } - return search.NewNumericRangeSearcher(i.i, q.Min, q.Max, field, q.BoostVal, explain) + return search.NewNumericRangeSearcher(i.i, q.Min, q.Max, q.InclusiveMin, q.InclusiveMax, field, q.BoostVal, explain) } func (q *NumericRangeQuery) Validate() error { diff --git a/search/search_numeric_range.go b/search/search_numeric_range.go index 2e02026d..7cb54618 100644 --- a/search/search_numeric_range.go +++ b/search/search_numeric_range.go @@ -25,7 +25,7 @@ type NumericRangeSearcher struct { searcher *DisjunctionSearcher } -func NewNumericRangeSearcher(index index.Index, min *float64, max *float64, field string, boost float64, explain bool) (*NumericRangeSearcher, error) { +func NewNumericRangeSearcher(index index.Index, min *float64, max *float64, inclusiveMin, inclusiveMax *bool, field string, boost float64, explain bool) (*NumericRangeSearcher, error) { // account for unbounded edges if min == nil { negInf := math.Inf(-1) @@ -35,9 +35,23 @@ func NewNumericRangeSearcher(index index.Index, min *float64, max *float64, fiel Inf := math.Inf(1) max = &Inf } + if inclusiveMin == nil { + defaultInclusiveMin := true + inclusiveMin = &defaultInclusiveMin + } + if inclusiveMax == nil { + defaultInclusiveMax := false + inclusiveMax = &defaultInclusiveMax + } // find all the ranges minInt64 := numeric_util.Float64ToInt64(*min) + if !*inclusiveMin && minInt64 != math.MaxInt64 { + minInt64 += 1 + } maxInt64 := numeric_util.Float64ToInt64(*max) + if !*inclusiveMax && maxInt64 != math.MinInt64 { + maxInt64 -= 1 + } // FIXME hard-coded precion, should match field declaration termRanges := splitInt64Range(minInt64, maxInt64, 4) terms := termRanges.Enumerate()