0
0
Fork 0

MB-20793: Validation for min/max/start/end params for numeric/date range facets

Improved the validations for date and numeric range queries for facets
This commit is contained in:
Sreekanth Sivasankaran 2017-02-01 14:48:47 +05:30
parent 12a7257b5f
commit f514ac7867
1 changed files with 44 additions and 15 deletions

View File

@ -20,10 +20,16 @@ import (
"time"
"github.com/blevesearch/bleve/analysis"
"github.com/blevesearch/bleve/analysis/datetime/optional"
"github.com/blevesearch/bleve/registry"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/query"
)
var cache = registry.NewCache()
const defaultDateTimeParser = optional.Name
type numericRange struct {
Name string `json:"name,omitempty"`
Min *float64 `json:"min,omitempty"`
@ -105,26 +111,49 @@ type FacetRequest struct {
}
func (fr *FacetRequest) Validate() error {
if len(fr.NumericRanges) > 0 && len(fr.DateTimeRanges) > 0 {
nrCount := len(fr.NumericRanges)
drCount := len(fr.DateTimeRanges)
if nrCount > 0 && drCount > 0 {
return fmt.Errorf("facet can only conain numeric ranges or date ranges, not both")
}
nrNames := map[string]interface{}{}
for _, nr := range fr.NumericRanges {
if _, ok := nrNames[nr.Name]; ok {
return fmt.Errorf("numeric ranges contains duplicate name '%s'", nr.Name)
rangeSet := false
if nrCount > 0 {
nrNames := map[string]interface{}{}
for _, nr := range fr.NumericRanges {
if _, ok := nrNames[nr.Name]; ok {
return fmt.Errorf("numeric ranges contains duplicate name '%s'", nr.Name)
}
nrNames[nr.Name] = struct{}{}
if !rangeSet && (nr.Min != nil || nr.Max != nil) {
rangeSet = true
}
}
nrNames[nr.Name] = struct{}{}
}
drNames := map[string]interface{}{}
for _, dr := range fr.DateTimeRanges {
if _, ok := drNames[dr.Name]; ok {
return fmt.Errorf("date ranges contains duplicate name '%s'", dr.Name)
if !rangeSet {
return fmt.Errorf("numeric range query must specify either min, max or both")
}
drNames[dr.Name] = struct{}{}
}
} else {
dateTimeParser, err := cache.DateTimeParserNamed(defaultDateTimeParser)
if err != nil {
return err
}
drNames := map[string]interface{}{}
for _, dr := range fr.DateTimeRanges {
if _, ok := drNames[dr.Name]; ok {
return fmt.Errorf("date ranges contains duplicate name '%s'", dr.Name)
}
drNames[dr.Name] = struct{}{}
if !rangeSet {
start, end := dr.ParseDates(dateTimeParser)
if !start.IsZero() || !end.IsZero() {
rangeSet = true
}
}
}
if !rangeSet {
return fmt.Errorf("date range query must specify either start, end or both")
}
}
return nil
}