Skip to content

Commit

Permalink
after BlugeWalkDocument set the Sortable property by sortFilters,and …
Browse files Browse the repository at this point in the history
…the sortFilters must start with value.
  • Loading branch information
hexun80149128 committed Feb 21, 2024
1 parent ed6e779 commit 5f06e2b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion server/runtime_go.go
Expand Up @@ -2598,7 +2598,7 @@ func (ri *RuntimeGoInitializer) RegisterSubscriptionNotificationGoogle(fn func(c
return nil
}

// @param sortFields strings with the keys of the storage object whose values are to be indexed and sorted,it must be in fields,and should be string or num.
// @param sortFields strings with the keys of the storage object whose values are to be indexed and sorted,it must be in fields and add "value.",and should be string or num
func (ri *RuntimeGoInitializer) RegisterStorageIndex(name, collection, key string, fields []string, sortFields []string, maxEntries int, indexOnly bool) error {
return ri.storageIndex.CreateIndex(context.Background(), name, collection, key, fields, sortFields, maxEntries, indexOnly)
}
Expand Down
38 changes: 15 additions & 23 deletions server/storage_index.go
Expand Up @@ -20,7 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
segment "github.com/blugelabs/bluge_segment_api"
"time"

"github.com/blugelabs/bluge"
Expand Down Expand Up @@ -485,28 +485,6 @@ func (si *LocalStorageIndex) mapIndexStorageFields(userID, collection, key, vers
rv.AddField(bluge.NewNumericField("read", float64(read)).StoreValue())
rv.AddField(bluge.NewNumericField("write", float64(write)).StoreValue())

if len(sortFilters) > 0 {
for _, sortKey := range sortFilters {
if v, found := mapValue[sortKey]; found {
val := reflect.ValueOf(v)
if !val.IsValid() {
continue
}
typ := val.Type()
switch typ.Kind() {
case reflect.String:
rv.AddField(bluge.NewKeywordField(sortKey, val.String()).Sortable())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
rv.AddField(bluge.NewNumericField(sortKey, float64(val.Int())).Sortable())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.AddField(bluge.NewNumericField(sortKey, float64(val.Uint())).Sortable())
case reflect.Float32, reflect.Float64:
rv.AddField(bluge.NewNumericField(sortKey, val.Float()).Sortable())
}
}
}
}

if !si.config.DisableIndexOnly && indexOnly {
json, err := json.Marshal(mapValue)
if err != nil {
Expand All @@ -517,6 +495,20 @@ func (si *LocalStorageIndex) mapIndexStorageFields(userID, collection, key, vers

BlugeWalkDocument(mapValue, []string{"value"}, rv)

if len(sortFilters) > 0 {
rv.EachField(func(field segment.Field) {
for _, sortKey := range sortFilters {
if field.Name() == sortKey {
term, ok := field.(*bluge.TermField)
if ok {
term.Sortable()
}
break
}
}
})
}

return rv, nil
}

Expand Down
28 changes: 19 additions & 9 deletions server/storage_index_test.go
Expand Up @@ -411,28 +411,38 @@ func TestLocalStorageIndex_List(t *testing.T) {
key := "key"
maxEntries := 10

type sortStruct struct {
SortValue int32
}

valueOneBytes, _ := json.Marshal(map[string]any{
"one": 1,
"sort": 1,
"one": 1,
"sort": sortStruct{
SortValue: 1,
},
})
valueOne := string(valueOneBytes)
valueTwoBytes, _ := json.Marshal(map[string]any{
"two": 2,
"sort": 2,
"two": 2,
"sort": sortStruct{
SortValue: 2,
},
})
valueTwo := string(valueTwoBytes)
valueThreeBytes, _ := json.Marshal(map[string]any{
"three": 3,
"sort": 3,
"sort": sortStruct{
SortValue: 3,
},
})
valueThree := string(valueThreeBytes)

storageIdx, err := NewLocalStorageIndex(logger, db, &StorageConfig{}, metrics)
if err != nil {
t.Fatal(err.Error())
}
if err := storageIdx.CreateIndex(ctx, indexName, collection, key, []string{"one", "two", "three", "sort"}, []string{"sort"}, maxEntries, true); err != nil {

if err := storageIdx.CreateIndex(ctx, indexName, collection, key, []string{"one", "two", "three", "sort"}, []string{"value.sort.SortValue"}, maxEntries, true); err != nil {
t.Fatal(err.Error())
}

Expand Down Expand Up @@ -476,15 +486,15 @@ func TestLocalStorageIndex_List(t *testing.T) {
assert.Equal(t, valueOne, entries.Objects[0].Value, "expected value retrieved from db did not match")
assert.Equal(t, valueThree, entries.Objects[1].Value, "expected value retrieved from db did not match")

sortEntries, err := storageIdx.List(ctx, uuid.Nil, indexName, "value.one:1 value.three:3", 10, []string{"value.sort"})
sortEntries, err := storageIdx.List(ctx, uuid.Nil, indexName, "value.one:1 value.three:3", 10, []string{"value.sort.SortValue"})
if err != nil {
t.Fatal(err.Error())
}
assert.Len(t, sortEntries.Objects, 2, "indexed results did not match query params")
assert.Equal(t, valueOne, sortEntries.Objects[0].Value, "expected value retrieved from db did not match")
assert.Equal(t, valueThree, sortEntries.Objects[1].Value, "expected value retrieved from db did not match")

sortDescEntries, err := storageIdx.List(ctx, uuid.Nil, indexName, "value.one:1 value.three:3", 10, []string{"-value.sort"})
sortDescEntries, err := storageIdx.List(ctx, uuid.Nil, indexName, "value.one:1 value.three:3", 10, []string{"-value.sort.SortValue"})
if err != nil {
t.Fatal(err.Error())
}
Expand Down

0 comments on commit 5f06e2b

Please sign in to comment.