2014-08-11 17:03:29 +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-09-02 16:54:50 +02:00
|
|
|
|
2016-10-02 16:29:39 +02:00
|
|
|
package facet
|
2014-08-11 17:03:29 +02:00
|
|
|
|
|
|
|
import (
|
2016-03-14 17:02:07 +01:00
|
|
|
"sort"
|
2014-08-11 17:03:29 +02:00
|
|
|
"time"
|
|
|
|
|
2016-09-30 17:35:22 +02:00
|
|
|
"github.com/blevesearch/bleve/numeric"
|
2014-09-01 17:15:38 +02:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2014-08-11 17:03:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type dateTimeRange struct {
|
|
|
|
start time.Time
|
|
|
|
end time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type DateTimeFacetBuilder struct {
|
|
|
|
size int
|
|
|
|
field string
|
|
|
|
termsCount map[string]int
|
|
|
|
total int
|
|
|
|
missing int
|
|
|
|
ranges map[string]*dateTimeRange
|
2017-03-02 22:19:08 +01:00
|
|
|
sawValue bool
|
2014-08-11 17:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDateTimeFacetBuilder(field string, size int) *DateTimeFacetBuilder {
|
|
|
|
return &DateTimeFacetBuilder{
|
|
|
|
size: size,
|
|
|
|
field: field,
|
|
|
|
termsCount: make(map[string]int),
|
|
|
|
ranges: make(map[string]*dateTimeRange, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb *DateTimeFacetBuilder) AddRange(name string, start, end time.Time) {
|
|
|
|
r := dateTimeRange{
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
}
|
|
|
|
fb.ranges[name] = &r
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:31:35 +02:00
|
|
|
func (fb *DateTimeFacetBuilder) Field() string {
|
|
|
|
return fb.field
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:19:08 +01:00
|
|
|
func (fb *DateTimeFacetBuilder) UpdateVisitor(field string, term []byte) {
|
|
|
|
if field == fb.field {
|
|
|
|
fb.sawValue = true
|
|
|
|
// only consider the values which are shifted 0
|
|
|
|
prefixCoded := numeric.PrefixCoded(term)
|
|
|
|
shift, err := prefixCoded.Shift()
|
|
|
|
if err == nil && shift == 0 {
|
|
|
|
i64, err := prefixCoded.Int64()
|
|
|
|
if err == nil {
|
|
|
|
t := time.Unix(0, i64)
|
|
|
|
|
|
|
|
// look at each of the ranges for a match
|
|
|
|
for rangeName, r := range fb.ranges {
|
|
|
|
if (r.start.IsZero() || t.After(r.start) || t.Equal(r.start)) && (r.end.IsZero() || t.Before(r.end)) {
|
|
|
|
fb.termsCount[rangeName] = fb.termsCount[rangeName] + 1
|
|
|
|
fb.total++
|
2014-08-11 17:03:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 22:19:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb *DateTimeFacetBuilder) StartDoc() {
|
|
|
|
fb.sawValue = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb *DateTimeFacetBuilder) EndDoc() {
|
|
|
|
if !fb.sawValue {
|
2014-08-11 17:03:29 +02:00
|
|
|
fb.missing++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 14:22:11 +01:00
|
|
|
func (fb *DateTimeFacetBuilder) Result() *search.FacetResult {
|
2014-09-01 17:15:38 +02:00
|
|
|
rv := search.FacetResult{
|
2014-08-11 17:03:29 +02:00
|
|
|
Field: fb.field,
|
|
|
|
Total: fb.total,
|
|
|
|
Missing: fb.missing,
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:02:07 +01:00
|
|
|
rv.DateRanges = make([]*search.DateRangeFacet, 0, len(fb.termsCount))
|
2014-08-11 17:03:29 +02:00
|
|
|
|
|
|
|
for term, count := range fb.termsCount {
|
|
|
|
dateRange := fb.ranges[term]
|
2014-09-01 17:15:38 +02:00
|
|
|
tf := &search.DateRangeFacet{
|
2014-08-11 17:03:29 +02:00
|
|
|
Name: term,
|
|
|
|
Count: count,
|
|
|
|
}
|
|
|
|
if !dateRange.start.IsZero() {
|
|
|
|
start := dateRange.start.Format(time.RFC3339Nano)
|
|
|
|
tf.Start = &start
|
|
|
|
}
|
|
|
|
if !dateRange.end.IsZero() {
|
|
|
|
end := dateRange.end.Format(time.RFC3339Nano)
|
|
|
|
tf.End = &end
|
|
|
|
}
|
2016-03-14 17:02:07 +01:00
|
|
|
rv.DateRanges = append(rv.DateRanges, tf)
|
|
|
|
}
|
2014-08-11 17:03:29 +02:00
|
|
|
|
2016-03-14 17:02:07 +01:00
|
|
|
sort.Sort(rv.DateRanges)
|
2014-08-11 17:03:29 +02:00
|
|
|
|
2016-03-14 17:02:07 +01:00
|
|
|
// we now have the list of the top N facets
|
|
|
|
if fb.size < len(rv.DateRanges) {
|
|
|
|
rv.DateRanges = rv.DateRanges[:fb.size]
|
2014-08-11 17:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
notOther := 0
|
2016-03-14 17:02:07 +01:00
|
|
|
for _, nr := range rv.DateRanges {
|
|
|
|
notOther += nr.Count
|
2014-08-11 17:03:29 +02:00
|
|
|
}
|
|
|
|
rv.Other = fb.total - notOther
|
|
|
|
|
2014-10-29 14:22:11 +01:00
|
|
|
return &rv
|
2014-08-11 17:03:29 +02:00
|
|
|
}
|