Merge pull request #134 from Shugyousha/numfacet
Add a benchmark for the numeric facet builder and use sort.Sort in it (just like for the terms one)
This commit is contained in:
commit
eaccd74c93
|
@ -10,7 +10,7 @@
|
||||||
package facets
|
package facets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"sort"
|
||||||
|
|
||||||
"github.com/blevesearch/bleve/index"
|
"github.com/blevesearch/bleve/index"
|
||||||
"github.com/blevesearch/bleve/numeric_util"
|
"github.com/blevesearch/bleve/numeric_util"
|
||||||
|
@ -89,11 +89,8 @@ func (fb *NumericFacetBuilder) Result() *search.FacetResult {
|
||||||
Missing: fb.missing,
|
Missing: fb.missing,
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME better implementation needed here this is quick and dirty
|
rv.NumericRanges = make([]*search.NumericRangeFacet, 0, len(fb.termsCount))
|
||||||
topN := list.New()
|
|
||||||
|
|
||||||
// walk entries and find top N
|
|
||||||
OUTER:
|
|
||||||
for term, count := range fb.termsCount {
|
for term, count := range fb.termsCount {
|
||||||
numericRange := fb.ranges[term]
|
numericRange := fb.ranges[term]
|
||||||
tf := &search.NumericRangeFacet{
|
tf := &search.NumericRangeFacet{
|
||||||
|
@ -103,36 +100,19 @@ OUTER:
|
||||||
Max: numericRange.max,
|
Max: numericRange.max,
|
||||||
}
|
}
|
||||||
|
|
||||||
for e := topN.Front(); e != nil; e = e.Next() {
|
rv.NumericRanges = append(rv.NumericRanges, tf)
|
||||||
curr := e.Value.(*search.NumericRangeFacet)
|
|
||||||
if tf.Count < curr.Count {
|
|
||||||
|
|
||||||
topN.InsertBefore(tf, e)
|
|
||||||
// if we just made the list too long
|
|
||||||
if topN.Len() > fb.size {
|
|
||||||
// remove the head
|
|
||||||
topN.Remove(topN.Front())
|
|
||||||
}
|
|
||||||
continue OUTER
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if we got to the end, we still have to add it
|
|
||||||
topN.PushBack(tf)
|
|
||||||
if topN.Len() > fb.size {
|
|
||||||
// remove the head
|
|
||||||
topN.Remove(topN.Front())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
sort.Sort(rv.NumericRanges)
|
||||||
|
|
||||||
// we now have the list of the top N facets
|
// we now have the list of the top N facets
|
||||||
rv.NumericRanges = make([]*search.NumericRangeFacet, topN.Len())
|
if fb.size < len(rv.NumericRanges) {
|
||||||
i := 0
|
rv.NumericRanges = rv.NumericRanges[:fb.size]
|
||||||
|
}
|
||||||
|
|
||||||
notOther := 0
|
notOther := 0
|
||||||
for e := topN.Back(); e != nil; e = e.Prev() {
|
for _, nr := range rv.NumericRanges {
|
||||||
rv.NumericRanges[i] = e.Value.(*search.NumericRangeFacet)
|
notOther += nr.Count
|
||||||
i++
|
|
||||||
notOther += e.Value.(*search.NumericRangeFacet).Count
|
|
||||||
}
|
}
|
||||||
rv.Other = fb.total - notOther
|
rv.Other = fb.total - notOther
|
||||||
|
|
||||||
|
|
49
search/facets/facet_builder_numeric_test.go
Normal file
49
search/facets/facet_builder_numeric_test.go
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package facets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/blevesearch/bleve/index"
|
||||||
|
nu "github.com/blevesearch/bleve/numeric_util"
|
||||||
|
)
|
||||||
|
|
||||||
|
var pcodedvalues []nu.PrefixCoded
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
pcodedvalues = []nu.PrefixCoded{nu.PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}, nu.PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f}, nu.PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7a, 0x1d, 0xa}, nu.PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x16, 0x9, 0x4a, 0x7b}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNumericFacet10(b *testing.B) {
|
||||||
|
numericFacetN(b, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNumericFacet100(b *testing.B) {
|
||||||
|
numericFacetN(b, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNumericFacet1000(b *testing.B) {
|
||||||
|
numericFacetN(b, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func numericFacetN(b *testing.B, numTerms int) {
|
||||||
|
field := "test"
|
||||||
|
nfb := NewNumericFacetBuilder(field, numTerms)
|
||||||
|
min, max := 0.0, 9999999998.0
|
||||||
|
|
||||||
|
for i := 0; i <= numTerms; i++ {
|
||||||
|
max++
|
||||||
|
min--
|
||||||
|
|
||||||
|
nfb.AddRange("rangename"+strconv.Itoa(i), &min, &max)
|
||||||
|
|
||||||
|
for _, pv := range pcodedvalues {
|
||||||
|
nfb.Update(index.FieldTerms{field: []string{string(pv)}})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
nfb.Result()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user