0
0
Fork 0

numeric range facet merging compare range values not pointers

fix #492
This commit is contained in:
Marty Schoch 2016-11-03 15:48:46 -04:00
parent 81c76f2a4a
commit e5ec831250
2 changed files with 33 additions and 5 deletions

View File

@ -105,11 +105,34 @@ type NumericRangeFacet struct {
Count int `json:"count"`
}
func (nrf *NumericRangeFacet) Same(other *NumericRangeFacet) bool {
if nrf.Min == nil && other.Min != nil {
return false
}
if nrf.Min != nil && other.Min == nil {
return false
}
if nrf.Min != nil && other.Min != nil && *nrf.Min != *other.Min {
return false
}
if nrf.Max == nil && other.Max != nil {
return false
}
if nrf.Max != nil && other.Max == nil {
return false
}
if nrf.Max != nil && other.Max != nil && *nrf.Max != *other.Max {
return false
}
return true
}
type NumericRangeFacets []*NumericRangeFacet
func (nrf NumericRangeFacets) Add(numericRangeFacet *NumericRangeFacet) NumericRangeFacets {
for _, existingNr := range nrf {
if numericRangeFacet.Min == existingNr.Min && numericRangeFacet.Max == existingNr.Max {
if numericRangeFacet.Same(existingNr) {
existingNr.Count += numericRangeFacet.Count
return nrf
}

View File

@ -124,6 +124,11 @@ func TestNumericFacetResultsMerge(t *testing.T) {
medhi := 6.0
hihigher := 9.0
// why second copy? the pointers may be different, but values the same
lowmed2 := 3.0
medhi2 := 6.0
hihigher2 := 9.0
fr1 := &FacetResult{
Field: "rating",
Total: 100,
@ -161,18 +166,18 @@ func TestNumericFacetResultsMerge(t *testing.T) {
NumericRanges: []*NumericRangeFacet{
{
Name: "low",
Max: &lowmed,
Max: &lowmed2,
Count: 25,
},
{
Name: "med",
Max: &lowmed,
Min: &medhi,
Max: &lowmed2,
Min: &medhi2,
Count: 22,
},
{
Name: "highest",
Min: &hihigher,
Min: &hihigher2,
Count: 3,
},
},