From e5ec8312500a5df0ab999884ffd854a16eb844e3 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Thu, 3 Nov 2016 15:48:46 -0400 Subject: [PATCH] numeric range facet merging compare range values not pointers fix #492 --- search/facets_builder.go | 25 ++++++++++++++++++++++++- search/facets_builder_test.go | 13 +++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/search/facets_builder.go b/search/facets_builder.go index 702e2ec2..2cb6e5c8 100644 --- a/search/facets_builder.go +++ b/search/facets_builder.go @@ -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 } diff --git a/search/facets_builder_test.go b/search/facets_builder_test.go index 8dfcebb4..4d7dd085 100644 --- a/search/facets_builder_test.go +++ b/search/facets_builder_test.go @@ -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, }, },