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 (
|
2018-03-02 02:12:16 +01:00
|
|
|
"reflect"
|
2014-11-29 16:13:10 +01:00
|
|
|
"sort"
|
2014-08-11 17:03:29 +02:00
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2018-03-02 02:12:16 +01:00
|
|
|
"github.com/blevesearch/bleve/size"
|
2014-08-11 17:03:29 +02:00
|
|
|
)
|
|
|
|
|
2018-03-02 02:12:16 +01:00
|
|
|
var reflectStaticSizeTermsFacetBuilder int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var tfb TermsFacetBuilder
|
|
|
|
reflectStaticSizeTermsFacetBuilder = int(reflect.TypeOf(tfb).Size())
|
|
|
|
}
|
|
|
|
|
2014-08-11 17:03:29 +02:00
|
|
|
type TermsFacetBuilder struct {
|
|
|
|
size int
|
|
|
|
field string
|
|
|
|
termsCount map[string]int
|
|
|
|
total int
|
|
|
|
missing int
|
2017-03-02 22:19:08 +01:00
|
|
|
sawValue bool
|
2014-08-11 17:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTermsFacetBuilder(field string, size int) *TermsFacetBuilder {
|
|
|
|
return &TermsFacetBuilder{
|
|
|
|
size: size,
|
|
|
|
field: field,
|
|
|
|
termsCount: make(map[string]int),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 02:12:16 +01:00
|
|
|
func (fb *TermsFacetBuilder) Size() int {
|
|
|
|
sizeInBytes := reflectStaticSizeTermsFacetBuilder + size.SizeOfPtr +
|
|
|
|
len(fb.field)
|
|
|
|
|
|
|
|
for k, _ := range fb.termsCount {
|
|
|
|
sizeInBytes += size.SizeOfString + len(k) +
|
|
|
|
size.SizeOfInt
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizeInBytes
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:31:35 +02:00
|
|
|
func (fb *TermsFacetBuilder) Field() string {
|
|
|
|
return fb.field
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:19:08 +01:00
|
|
|
func (fb *TermsFacetBuilder) UpdateVisitor(field string, term []byte) {
|
|
|
|
if field == fb.field {
|
|
|
|
fb.sawValue = true
|
|
|
|
fb.termsCount[string(term)] = fb.termsCount[string(term)] + 1
|
|
|
|
fb.total++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb *TermsFacetBuilder) StartDoc() {
|
|
|
|
fb.sawValue = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb *TermsFacetBuilder) EndDoc() {
|
|
|
|
if !fb.sawValue {
|
2014-08-11 17:03:29 +02:00
|
|
|
fb.missing++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 14:22:11 +01:00
|
|
|
func (fb *TermsFacetBuilder) 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,
|
|
|
|
}
|
|
|
|
|
2014-12-01 18:59:30 +01:00
|
|
|
rv.Terms = make([]*search.TermFacet, 0, len(fb.termsCount))
|
2014-08-11 17:03:29 +02:00
|
|
|
|
|
|
|
for term, count := range fb.termsCount {
|
2014-09-01 17:15:38 +02:00
|
|
|
tf := &search.TermFacet{
|
2014-08-11 17:03:29 +02:00
|
|
|
Term: term,
|
|
|
|
Count: count,
|
|
|
|
}
|
|
|
|
|
2014-11-29 16:13:10 +01:00
|
|
|
rv.Terms = append(rv.Terms, tf)
|
2014-08-11 17:03:29 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 16:13:10 +01:00
|
|
|
sort.Sort(rv.Terms)
|
|
|
|
|
2014-08-11 17:03:29 +02:00
|
|
|
// we now have the list of the top N facets
|
2015-01-22 15:56:00 +01:00
|
|
|
trimTopN := fb.size
|
|
|
|
if trimTopN > len(rv.Terms) {
|
|
|
|
trimTopN = len(rv.Terms)
|
|
|
|
}
|
|
|
|
rv.Terms = rv.Terms[:trimTopN]
|
2014-11-29 16:13:10 +01:00
|
|
|
|
2014-08-11 17:03:29 +02:00
|
|
|
notOther := 0
|
2014-11-29 16:13:10 +01:00
|
|
|
for _, tf := range rv.Terms {
|
|
|
|
notOther += tf.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
|
|
|
}
|