2014-08-03 23:19:04 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
|
|
|
// 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 23:19:04 +02:00
|
|
|
|
|
|
|
import (
|
2016-09-23 03:24:43 +02:00
|
|
|
"encoding/json"
|
2014-08-03 23:19:04 +02:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2016-09-23 03:24:43 +02:00
|
|
|
"time"
|
2014-08-03 23:19:04 +02:00
|
|
|
|
2016-09-30 17:18:39 +02:00
|
|
|
"github.com/blevesearch/bleve/analysis/datetime/optional"
|
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"
|
2016-09-30 17:35:22 +02:00
|
|
|
"github.com/blevesearch/bleve/numeric"
|
2016-09-20 03:12:36 +02:00
|
|
|
"github.com/blevesearch/bleve/registry"
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2014-09-01 17:15:38 +02:00
|
|
|
"github.com/blevesearch/bleve/search/searchers"
|
2014-08-03 23:19:04 +02:00
|
|
|
)
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
// QueryDateTimeParser controls the default query date time parser
|
2016-09-30 17:18:39 +02:00
|
|
|
var QueryDateTimeParser = optional.Name
|
2016-09-20 03:12:36 +02:00
|
|
|
|
2016-09-23 03:24:43 +02:00
|
|
|
// QueryDateTimeFormat controls the format when Marshaling to JSON
|
|
|
|
var QueryDateTimeFormat = time.RFC3339
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
var cache = registry.NewCache()
|
|
|
|
|
2016-09-23 03:24:43 +02:00
|
|
|
type BleveQueryTime struct {
|
|
|
|
time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func QueryTimeFromString(t string) (time.Time, error) {
|
|
|
|
dateTimeParser, err := cache.DateTimeParserNamed(QueryDateTimeParser)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
rv, err := dateTimeParser.ParseDateTime(t)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
return rv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BleveQueryTime) MarshalJSON() ([]byte, error) {
|
|
|
|
tt := time.Time(t.Time)
|
|
|
|
return []byte(tt.Format(QueryDateTimeFormat)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BleveQueryTime) UnmarshalJSON(data []byte) error {
|
|
|
|
var timeString string
|
|
|
|
err := json.Unmarshal(data, &timeString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dateTimeParser, err := cache.DateTimeParserNamed(QueryDateTimeParser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Time, err = dateTimeParser.ParseDateTime(timeString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
type DateRangeQuery struct {
|
2016-09-23 03:24:43 +02:00
|
|
|
Start BleveQueryTime `json:"start,omitempty"`
|
|
|
|
End BleveQueryTime `json:"end,omitempty"`
|
|
|
|
InclusiveStart *bool `json:"inclusive_start,omitempty"`
|
|
|
|
InclusiveEnd *bool `json:"inclusive_end,omitempty"`
|
|
|
|
Field string `json:"field,omitempty"`
|
|
|
|
Boost *Boost `json:"boost,omitempty"`
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 16:55:22 +02:00
|
|
|
// NewDateRangeQuery creates a new Query for ranges
|
|
|
|
// of date values.
|
2016-06-26 16:25:09 +02:00
|
|
|
// Date strings are parsed using the DateTimeParser configured in the
|
|
|
|
// top-level config.QueryDateTimeParser
|
2014-08-31 16:55:22 +02:00
|
|
|
// Either, but not both endpoints can be nil.
|
2016-09-23 03:24:43 +02:00
|
|
|
func NewDateRangeQuery(start, end time.Time) *DateRangeQuery {
|
2014-08-25 23:47:27 +02:00
|
|
|
return NewDateRangeInclusiveQuery(start, end, nil, nil)
|
|
|
|
}
|
|
|
|
|
2014-09-02 23:40:46 +02:00
|
|
|
// NewDateRangeInclusiveQuery creates a new Query for ranges
|
|
|
|
// of date values.
|
2016-06-26 16:25:09 +02:00
|
|
|
// Date strings are parsed using the DateTimeParser configured in the
|
|
|
|
// top-level config.QueryDateTimeParser
|
2014-09-02 23:40:46 +02:00
|
|
|
// Either, but not both endpoints can be nil.
|
|
|
|
// startInclusive and endInclusive control inclusion of the endpoints.
|
2016-09-23 03:24:43 +02:00
|
|
|
func NewDateRangeInclusiveQuery(start, end time.Time, startInclusive, endInclusive *bool) *DateRangeQuery {
|
2016-09-20 03:12:36 +02:00
|
|
|
return &DateRangeQuery{
|
2016-09-23 03:24:43 +02:00
|
|
|
Start: BleveQueryTime{start},
|
|
|
|
End: BleveQueryTime{end},
|
2014-08-25 23:47:27 +02:00
|
|
|
InclusiveStart: startInclusive,
|
|
|
|
InclusiveEnd: endInclusive,
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
func (q *DateRangeQuery) SetBoost(b float64) {
|
|
|
|
boost := Boost(b)
|
|
|
|
q.Boost = &boost
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
func (q *DateRangeQuery) SetField(f string) {
|
|
|
|
q.Field = f
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *DateRangeQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, explain bool) (search.Searcher, error) {
|
2016-04-22 23:12:10 +02:00
|
|
|
min, max, err := q.parseEndpoints()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
|
|
|
|
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-09-23 01:49:31 +02:00
|
|
|
return searchers.NewNumericRangeSearcher(i, min, max, q.InclusiveStart, q.InclusiveEnd, field, q.Boost.Value(), explain)
|
2016-04-22 23:12:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *DateRangeQuery) parseEndpoints() (*float64, *float64, error) {
|
2014-08-03 23:19:04 +02:00
|
|
|
min := math.Inf(-1)
|
|
|
|
max := math.Inf(1)
|
2016-09-23 03:24:43 +02:00
|
|
|
if !q.Start.IsZero() {
|
2016-09-30 17:35:22 +02:00
|
|
|
min = numeric.Int64ToFloat64(q.Start.UnixNano())
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
2016-09-23 03:24:43 +02:00
|
|
|
if !q.End.IsZero() {
|
2016-09-30 17:35:22 +02:00
|
|
|
max = numeric.Int64ToFloat64(q.End.UnixNano())
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 23:12:10 +02:00
|
|
|
return &min, &max, nil
|
2014-08-03 23:19:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *DateRangeQuery) Validate() error {
|
2016-09-23 03:24:43 +02:00
|
|
|
if q.Start.IsZero() && q.End.IsZero() {
|
2014-08-03 23:19:04 +02:00
|
|
|
return fmt.Errorf("must specify start or end")
|
|
|
|
}
|
2016-04-22 23:12:10 +02:00
|
|
|
_, _, err := q.parseEndpoints()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-03 23:19:04 +02:00
|
|
|
return nil
|
|
|
|
}
|