Skip to content

Commit 2fea10b

Browse files
MB-64766: Adding decoded sort values to new array (#2198)
- Added a new field to document match to store the decoded sort values - Added implementations for decoding numeric, datetime and geo sort values - Added appropriate test cases for the same Also, upgrade zapx/v16 for fix: * 4e38ae4 Likith B | MB-59633: Support list of geo shapes for a single field --------- Co-authored-by: Abhinav Dangeti <[email protected]>
1 parent 98577e1 commit 2fea10b

File tree

6 files changed

+317
-22
lines changed

6 files changed

+317
-22
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/blevesearch/zapx/v13 v13.4.2
2727
github.com/blevesearch/zapx/v14 v14.4.2
2828
github.com/blevesearch/zapx/v15 v15.4.2
29-
github.com/blevesearch/zapx/v16 v16.2.3
29+
github.com/blevesearch/zapx/v16 v16.2.4
3030
github.com/couchbase/moss v0.2.0
3131
github.com/golang/protobuf v1.3.2
3232
github.com/spf13/cobra v1.8.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/blevesearch/zapx/v14 v14.4.2 h1:2SGHakVKd+TrtEqpfeq8X+So5PShQ5nW6GNxT
4444
github.com/blevesearch/zapx/v14 v14.4.2/go.mod h1:rz0XNb/OZSMjNorufDGSpFpjoFKhXmppH9Hi7a877D8=
4545
github.com/blevesearch/zapx/v15 v15.4.2 h1:sWxpDE0QQOTjyxYbAVjt3+0ieu8NCE0fDRaFxEsp31k=
4646
github.com/blevesearch/zapx/v15 v15.4.2/go.mod h1:1pssev/59FsuWcgSnTa0OeEpOzmhtmr/0/11H0Z8+Nw=
47-
github.com/blevesearch/zapx/v16 v16.2.3 h1:7Y0r+a3diEvlazsncexq1qoFOcBd64xwMS7aDm4lo1s=
48-
github.com/blevesearch/zapx/v16 v16.2.3/go.mod h1:wVJ+GtURAaRG9KQAMNYyklq0egV+XJlGcXNCE0OFjjA=
47+
github.com/blevesearch/zapx/v16 v16.2.4 h1:tGgfvleXTAkwsD5mEzgM3zCS/7pgocTCnO1oyAUjlww=
48+
github.com/blevesearch/zapx/v16 v16.2.4/go.mod h1:Rti/REtuuMmzwsI8/C/qIzRaEoSK/wiFYw5e5ctUKKs=
4949
github.com/couchbase/ghistogram v0.1.0 h1:b95QcQTCzjTUocDXp/uMgSNQi8oj1tGwnJ4bODWZnps=
5050
github.com/couchbase/ghistogram v0.1.0/go.mod h1:s1Jhy76zqfEecpNWJfWUiKZookAFaiGOEoyzgHt9i7k=
5151
github.com/couchbase/moss v0.2.0 h1:VCYrMzFwEryyhRSeI+/b3tRBSeTpi/8gn5Kf6dxqn+o=

search/collector/topn.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"strconv"
2121
"time"
2222

23-
"github.com/blevesearch/bleve/v2/numeric"
2423
"github.com/blevesearch/bleve/v2/search"
2524
"github.com/blevesearch/bleve/v2/size"
2625
index "github.com/blevesearch/bleve_index_api"
@@ -501,23 +500,7 @@ func (hc *TopNCollector) finalizeResults(r index.IndexReader) error {
501500
doc.Complete(nil)
502501
return nil
503502
})
504-
if err != nil {
505-
return err
506-
}
507503

508-
// Decode geo sort keys back to its distance values
509-
for i, so := range hc.sort {
510-
if _, ok := so.(*search.SortGeoDistance); ok {
511-
for _, dm := range hc.results {
512-
// The string is a int64 bit representation of a float64 distance
513-
distInt, err := numeric.PrefixCoded(dm.Sort[i]).Int64()
514-
if err != nil {
515-
return err
516-
}
517-
dm.Sort[i] = strconv.FormatFloat(numeric.Int64ToFloat64(distInt), 'f', -1, 64)
518-
}
519-
}
520-
}
521504
return err
522505
}
523506

search/search.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ type DocumentMatch struct {
154154
Locations FieldTermLocationMap `json:"locations,omitempty"`
155155
Fragments FieldFragmentMap `json:"fragments,omitempty"`
156156
Sort []string `json:"sort,omitempty"`
157+
DecodedSort []string `json:"decoded_sort,omitempty"`
157158

158159
// Fields contains the values for document fields listed in
159160
// SearchRequest.Fields. Text fields are returned as strings, numeric
@@ -224,6 +225,7 @@ func (dm *DocumentMatch) Reset() *DocumentMatch {
224225
dm.IndexInternalID = indexInternalID[:0]
225226
// reuse the []interface{} already allocated (and reset len to 0)
226227
dm.Sort = sort[:0]
228+
dm.DecodedSort = dm.DecodedSort[:0]
227229
// reuse the FieldTermLocations already allocated (and reset len to 0)
228230
dm.FieldTermLocations = ftls[:0]
229231
return dm
@@ -263,6 +265,10 @@ func (dm *DocumentMatch) Size() int {
263265
sizeInBytes += size.SizeOfString + len(entry)
264266
}
265267

268+
for _, entry := range dm.DecodedSort {
269+
sizeInBytes += size.SizeOfString + len(entry)
270+
}
271+
266272
for k := range dm.Fields {
267273
sizeInBytes += size.SizeOfString + len(k) +
268274
size.SizeOfPtr

search/sort.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"fmt"
2121
"math"
2222
"sort"
23+
"strconv"
2324
"strings"
25+
"time"
2426
"unicode/utf8"
2527

2628
"github.com/blevesearch/bleve/v2/geo"
@@ -36,6 +38,7 @@ var (
3638
type SearchSort interface {
3739
UpdateVisitor(field string, term []byte)
3840
Value(a *DocumentMatch) string
41+
DecodeValue(value string) string
3942
Descending() bool
4043

4144
RequiresDocID() bool
@@ -212,7 +215,9 @@ type SortOrder []SearchSort
212215

213216
func (so SortOrder) Value(doc *DocumentMatch) {
214217
for _, soi := range so {
215-
doc.Sort = append(doc.Sort, soi.Value(doc))
218+
value := soi.Value(doc)
219+
doc.Sort = append(doc.Sort, value)
220+
doc.DecodedSort = append(doc.DecodedSort, soi.DecodeValue(value))
216221
}
217222
}
218223

@@ -390,6 +395,25 @@ func (s *SortField) Value(i *DocumentMatch) string {
390395
return iTerm
391396
}
392397

398+
func (s *SortField) DecodeValue(value string) string {
399+
switch s.Type {
400+
case SortFieldAsNumber:
401+
i64, err := numeric.PrefixCoded(value).Int64()
402+
if err != nil {
403+
return value
404+
}
405+
return strconv.FormatFloat(numeric.Int64ToFloat64(i64), 'f', -1, 64)
406+
case SortFieldAsDate:
407+
i64, err := numeric.PrefixCoded(value).Int64()
408+
if err != nil {
409+
return value
410+
}
411+
return time.Unix(0, i64).UTC().String()
412+
default:
413+
return value
414+
}
415+
}
416+
393417
// Descending determines the order of the sort
394418
func (s *SortField) Descending() bool {
395419
return s.Desc
@@ -545,6 +569,10 @@ func (s *SortDocID) Value(i *DocumentMatch) string {
545569
return i.ID
546570
}
547571

572+
func (s *SortDocID) DecodeValue(value string) string {
573+
return value
574+
}
575+
548576
// Descending determines the order of the sort
549577
func (s *SortDocID) Descending() bool {
550578
return s.Desc
@@ -590,6 +618,10 @@ func (s *SortScore) Value(i *DocumentMatch) string {
590618
return "_score"
591619
}
592620

621+
func (s *SortScore) DecodeValue(value string) string {
622+
return value
623+
}
624+
593625
// Descending determines the order of the sort
594626
func (s *SortScore) Descending() bool {
595627
return s.Desc
@@ -694,6 +726,14 @@ func (s *SortGeoDistance) Value(i *DocumentMatch) string {
694726
return string(numeric.MustNewPrefixCodedInt64(distInt64, 0))
695727
}
696728

729+
func (s *SortGeoDistance) DecodeValue(value string) string {
730+
distInt, err := numeric.PrefixCoded(value).Int64()
731+
if err != nil {
732+
return ""
733+
}
734+
return strconv.FormatFloat(numeric.Int64ToFloat64(distInt), 'f', -1, 64)
735+
}
736+
697737
// Descending determines the order of the sort
698738
func (s *SortGeoDistance) Descending() bool {
699739
return s.Desc

0 commit comments

Comments
 (0)