Skip to content

Commit

Permalink
Revert "after BlugeWalkDocument set the Sortable property by sortFilt…
Browse files Browse the repository at this point in the history
…ers,and the sortFilters must start with value."

This reverts commit 5f06e2b.
  • Loading branch information
ftkg committed Mar 6, 2024
1 parent 8a7b7aa commit 5062a8d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 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 add "value.",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 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: 23 additions & 15 deletions server/storage_index.go
Expand Up @@ -20,7 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
segment "github.com/blugelabs/bluge_segment_api"
"reflect"
"time"

"github.com/blugelabs/bluge"
Expand Down Expand Up @@ -485,6 +485,28 @@ 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 @@ -495,20 +517,6 @@ 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: 9 additions & 19 deletions server/storage_index_test.go
Expand Up @@ -411,38 +411,28 @@ func TestLocalStorageIndex_List(t *testing.T) {
key := "key"
maxEntries := 10

type sortStruct struct {
SortValue int32
}

valueOneBytes, _ := json.Marshal(map[string]any{
"one": 1,
"sort": sortStruct{
SortValue: 1,
},
"one": 1,
"sort": 1,
})
valueOne := string(valueOneBytes)
valueTwoBytes, _ := json.Marshal(map[string]any{
"two": 2,
"sort": sortStruct{
SortValue: 2,
},
"two": 2,
"sort": 2,
})
valueTwo := string(valueTwoBytes)
valueThreeBytes, _ := json.Marshal(map[string]any{
"three": 3,
"sort": sortStruct{
SortValue: 3,
},
"sort": 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{"value.sort.SortValue"}, maxEntries, true); err != nil {

Check failure on line 434 in server/storage_index_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed (gofmt)
if err := storageIdx.CreateIndex(ctx, indexName, collection, key, []string{"one", "two", "three", "sort"}, []string{"sort"}, maxEntries, true); err != nil {
t.Fatal(err.Error())
}

Expand Down Expand Up @@ -486,15 +476,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.SortValue"})
sortEntries, err := storageIdx.List(ctx, uuid.Nil, indexName, "value.one:1 value.three:3", 10, []string{"value.sort"})
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.SortValue"})
sortDescEntries, err := storageIdx.List(ctx, uuid.Nil, indexName, "value.one:1 value.three:3", 10, []string{"-value.sort"})
if err != nil {
t.Fatal(err.Error())
}
Expand Down

0 comments on commit 5062a8d

Please sign in to comment.