2014-08-03 01:05:58 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
2016-10-02 16:13:14 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-08-29 20:18:36 +02:00
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
package query
|
2014-08-03 01:05:58 +02:00
|
|
|
|
|
|
|
import (
|
2016-09-20 03:12:36 +02:00
|
|
|
"fmt"
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
"github.com/blevesearch/bleve/index"
|
2016-09-18 15:33:18 +02:00
|
|
|
"github.com/blevesearch/bleve/mapping"
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2016-10-01 23:20:59 +02:00
|
|
|
"github.com/blevesearch/bleve/search/searcher"
|
2014-08-03 01:05:58 +02:00
|
|
|
)
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
type NumericRangeQuery struct {
|
2014-08-25 23:47:27 +02:00
|
|
|
Min *float64 `json:"min,omitempty"`
|
|
|
|
Max *float64 `json:"max,omitempty"`
|
|
|
|
InclusiveMin *bool `json:"inclusive_min,omitempty"`
|
|
|
|
InclusiveMax *bool `json:"inclusive_max,omitempty"`
|
2016-09-23 01:49:31 +02:00
|
|
|
Field string `json:"field,omitempty"`
|
|
|
|
Boost *Boost `json:"boost,omitempty"`
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 16:55:22 +02:00
|
|
|
// NewNumericRangeQuery creates a new Query for ranges
|
2015-11-12 22:16:10 +01:00
|
|
|
// of numeric values.
|
2014-08-31 16:55:22 +02:00
|
|
|
// Either, but not both endpoints can be nil.
|
|
|
|
// The minimum value is inclusive.
|
|
|
|
// The maximum value is exclusive.
|
2016-09-20 03:12:36 +02:00
|
|
|
func NewNumericRangeQuery(min, max *float64) *NumericRangeQuery {
|
2014-08-25 23:47:27 +02:00
|
|
|
return NewNumericRangeInclusiveQuery(min, max, nil, nil)
|
|
|
|
}
|
|
|
|
|
2014-09-02 23:40:46 +02:00
|
|
|
// NewNumericRangeInclusiveQuery creates a new Query for ranges
|
2015-11-12 22:16:10 +01:00
|
|
|
// of numeric values.
|
2014-08-31 16:55:22 +02:00
|
|
|
// Either, but not both endpoints can be nil.
|
|
|
|
// Control endpoint inclusion with inclusiveMin, inclusiveMax.
|
2016-09-20 03:12:36 +02:00
|
|
|
func NewNumericRangeInclusiveQuery(min, max *float64, minInclusive, maxInclusive *bool) *NumericRangeQuery {
|
|
|
|
return &NumericRangeQuery{
|
2014-08-25 23:47:27 +02:00
|
|
|
Min: min,
|
|
|
|
Max: max,
|
|
|
|
InclusiveMin: minInclusive,
|
|
|
|
InclusiveMax: maxInclusive,
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
func (q *NumericRangeQuery) SetBoost(b float64) {
|
|
|
|
boost := Boost(b)
|
|
|
|
q.Boost = &boost
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
func (q *NumericRangeQuery) SetField(f string) {
|
|
|
|
q.Field = f
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *NumericRangeQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, explain bool) (search.Searcher, error) {
|
2016-09-23 01:49:31 +02:00
|
|
|
field := q.Field
|
|
|
|
if q.Field == "" {
|
2016-09-18 15:33:18 +02:00
|
|
|
field = m.DefaultSearchField()
|
2014-08-06 14:23:29 +02:00
|
|
|
}
|
2016-10-02 16:29:39 +02:00
|
|
|
return searcher.NewNumericRangeSearcher(i, q.Min, q.Max, q.InclusiveMin, q.InclusiveMax, field, q.Boost.Value(), explain)
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *NumericRangeQuery) Validate() error {
|
2014-08-03 01:05:58 +02:00
|
|
|
if q.Min == nil && q.Min == q.Max {
|
2016-09-20 03:12:36 +02:00
|
|
|
return fmt.Errorf("numeric range query must specify min or max")
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|