From 795761c2adbc8bfe8dfdf484b724aaa77c09fc0b Mon Sep 17 00:00:00 2001 From: zhenghaoz Date: Wed, 6 Nov 2024 22:20:49 +0800 Subject: [PATCH] refactor: rename Document to Score (#883) --- master/rest.go | 2 +- master/rest_test.go | 20 ++--- master/tasks.go | 28 +++--- master/tasks_test.go | 84 +++++++++--------- server/bench_test.go | 16 ++-- server/rest.go | 82 +++++++++--------- server/rest_test.go | 118 ++++++++++++------------- storage/cache/database.go | 30 +++---- storage/cache/database_test.go | 90 +++++++++---------- storage/cache/mongodb.go | 12 +-- storage/cache/no_database.go | 8 +- storage/cache/no_database_test.go | 8 +- storage/cache/redis.go | 12 +-- storage/cache/redis_test.go | 16 ++-- storage/cache/sql.go | 18 ++-- worker/worker.go | 68 +++++++-------- worker/worker_test.go | 138 +++++++++++++++--------------- 17 files changed, 375 insertions(+), 375 deletions(-) diff --git a/master/rest.go b/master/rest.go index 77e4b95c8..8cc351d40 100644 --- a/master/rest.go +++ b/master/rest.go @@ -881,7 +881,7 @@ func (m *Master) searchDocuments(collection, subset, category string, request *r return } // Get the popular list - scores, err := m.CacheClient.SearchDocuments(ctx, collection, subset, []string{category}, offset, m.Config.Recommend.CacheSize) + scores, err := m.CacheClient.SearchScores(ctx, collection, subset, []string{category}, offset, m.Config.Recommend.CacheSize) if err != nil { server.InternalServerError(response, err) return diff --git a/master/rest_test.go b/master/rest_test.go index cb23f7656..bb5e4d843 100644 --- a/master/rest_test.go +++ b/master/rest_test.go @@ -648,14 +648,14 @@ func TestServer_SearchDocumentsOfItems(t *testing.T) { for i, operator := range operators { t.Run(operator.Name, func(t *testing.T) { // Put scores - scores := []cache.Document{ + scores := []cache.Score{ {Id: strconv.Itoa(i) + "0", Score: 100, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "1", Score: 99, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "2", Score: 98, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "3", Score: 97, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "4", Score: 96, Categories: []string{operator.Category}}, } - err := s.CacheClient.AddDocuments(ctx, operator.Collection, operator.Subset, scores) + err := s.CacheClient.AddScores(ctx, operator.Collection, operator.Subset, scores) assert.NoError(t, err) items := make([]ScoredItem, 0) for _, score := range scores { @@ -699,14 +699,14 @@ func TestServer_SearchDocumentsOfUsers(t *testing.T) { for _, operator := range operators { t.Logf("test RESTful API: %v", operator.Get) // Put scores - scores := []cache.Document{ + scores := []cache.Score{ {Id: "0", Score: 100, Categories: []string{""}}, {Id: "1", Score: 99, Categories: []string{""}}, {Id: "2", Score: 98, Categories: []string{""}}, {Id: "3", Score: 97, Categories: []string{""}}, {Id: "4", Score: 96, Categories: []string{""}}, } - err := s.CacheClient.AddDocuments(ctx, operator.Prefix, operator.Label, scores) + err := s.CacheClient.AddScores(ctx, operator.Prefix, operator.Label, scores) assert.NoError(t, err) users := make([]ScoreUser, 0) for _, score := range scores { @@ -758,7 +758,7 @@ func TestServer_GetRecommends(t *testing.T) { s, cookie := newMockServer(t) defer s.Close(t) // inset recommendation - itemIds := []cache.Document{ + itemIds := []cache.Score{ {Id: "1", Score: 99, Categories: []string{""}}, {Id: "2", Score: 98, Categories: []string{""}}, {Id: "3", Score: 97, Categories: []string{""}}, @@ -769,7 +769,7 @@ func TestServer_GetRecommends(t *testing.T) { {Id: "8", Score: 92, Categories: []string{""}}, } ctx := context.Background() - err := s.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", itemIds) + err := s.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", itemIds) assert.NoError(t, err) // insert feedback feedback := []data.Feedback{ @@ -825,14 +825,14 @@ func TestMaster_Purge(t *testing.T) { assert.NoError(t, err) assert.ElementsMatch(t, []string{"a", "b", "c"}, set) - err = s.CacheClient.AddDocuments(ctx, "sorted", "", []cache.Document{ + err = s.CacheClient.AddScores(ctx, "sorted", "", []cache.Score{ {Id: "a", Score: 1, Categories: []string{""}}, {Id: "b", Score: 2, Categories: []string{""}}, {Id: "c", Score: 3, Categories: []string{""}}}) assert.NoError(t, err) - z, err := s.CacheClient.SearchDocuments(ctx, "sorted", "", []string{""}, 0, -1) + z, err := s.CacheClient.SearchScores(ctx, "sorted", "", []string{""}, 0, -1) assert.NoError(t, err) - assert.ElementsMatch(t, []cache.Document{ + assert.ElementsMatch(t, []cache.Score{ {Id: "a", Score: 1, Categories: []string{""}}, {Id: "b", Score: 2, Categories: []string{""}}, {Id: "c", Score: 3, Categories: []string{""}}}, z) @@ -869,7 +869,7 @@ func TestMaster_Purge(t *testing.T) { set, err = s.CacheClient.GetSet(ctx, "set") assert.NoError(t, err) assert.Empty(t, set) - z, err = s.CacheClient.SearchDocuments(ctx, "sorted", "", []string{""}, 0, -1) + z, err = s.CacheClient.SearchScores(ctx, "sorted", "", []string{""}, 0, -1) assert.NoError(t, err) assert.Empty(t, z) diff --git a/master/tasks.go b/master/tasks.go index e8ba09203..7116d8196 100644 --- a/master/tasks.go +++ b/master/tasks.go @@ -91,10 +91,10 @@ func (m *Master) runLoadDatasetTask() error { } // save popular items to cache - if err = m.CacheClient.AddDocuments(ctx, cache.PopularItems, "", popularItems.ToSlice()); err != nil { + if err = m.CacheClient.AddScores(ctx, cache.PopularItems, "", popularItems.ToSlice()); err != nil { log.Logger().Error("failed to cache popular items", zap.Error(err)) } - if err = m.CacheClient.DeleteDocuments(ctx, []string{cache.PopularItems}, cache.DocumentCondition{Before: &popularItems.Timestamp}); err != nil { + if err = m.CacheClient.DeleteScores(ctx, []string{cache.PopularItems}, cache.ScoreCondition{Before: &popularItems.Timestamp}); err != nil { log.Logger().Error("failed to reclaim outdated items", zap.Error(err)) } if err = m.CacheClient.Set(ctx, cache.Time(cache.Key(cache.GlobalMeta, cache.LastUpdatePopularItemsTime), time.Now())); err != nil { @@ -102,10 +102,10 @@ func (m *Master) runLoadDatasetTask() error { } // save the latest items to cache - if err = m.CacheClient.AddDocuments(ctx, cache.LatestItems, "", latestItems.ToSlice()); err != nil { + if err = m.CacheClient.AddScores(ctx, cache.LatestItems, "", latestItems.ToSlice()); err != nil { log.Logger().Error("failed to cache latest items", zap.Error(err)) } - if err = m.CacheClient.DeleteDocuments(ctx, []string{cache.LatestItems}, cache.DocumentCondition{Before: &latestItems.Timestamp}); err != nil { + if err = m.CacheClient.DeleteScores(ctx, []string{cache.LatestItems}, cache.ScoreCondition{Before: &latestItems.Timestamp}); err != nil { log.Logger().Error("failed to reclaim outdated items", zap.Error(err)) } if err = m.CacheClient.Set(ctx, cache.Time(cache.Key(cache.GlobalMeta, cache.LastUpdateLatestItemsTime), time.Now())); err != nil { @@ -397,10 +397,10 @@ func (m *Master) findItemNeighborsBruteForce(dataset *ranking.DataSet, labeledIt } aggregator.Add(category, recommends, scores) } - if err := m.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, itemId, aggregator.ToSlice()); err != nil { + if err := m.CacheClient.AddScores(ctx, cache.ItemNeighbors, itemId, aggregator.ToSlice()); err != nil { return errors.Trace(err) } - if err := m.CacheClient.DeleteDocuments(ctx, []string{cache.ItemNeighbors}, cache.DocumentCondition{ + if err := m.CacheClient.DeleteScores(ctx, []string{cache.ItemNeighbors}, cache.ScoreCondition{ Subset: proto.String(itemId), Before: &aggregator.Timestamp, }); err != nil { @@ -502,10 +502,10 @@ func (m *Master) findItemNeighborsIVF(dataset *ranking.DataSet, labelIDF, userID aggregator.Add(category, resultValues, resultScores) } } - if err := m.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, itemId, aggregator.ToSlice()); err != nil { + if err := m.CacheClient.AddScores(ctx, cache.ItemNeighbors, itemId, aggregator.ToSlice()); err != nil { return errors.Trace(err) } - if err := m.CacheClient.DeleteDocuments(ctx, []string{cache.ItemNeighbors}, cache.DocumentCondition{ + if err := m.CacheClient.DeleteScores(ctx, []string{cache.ItemNeighbors}, cache.ScoreCondition{ Subset: proto.String(itemId), Before: &aggregator.Timestamp, }); err != nil { @@ -716,10 +716,10 @@ func (m *Master) findUserNeighborsBruteForce(ctx context.Context, dataset *ranki } aggregator := cache.NewDocumentAggregator(startSearchTime) aggregator.Add("", recommends, scores) - if err := m.CacheClient.AddDocuments(ctx, cache.UserNeighbors, userId, aggregator.ToSlice()); err != nil { + if err := m.CacheClient.AddScores(ctx, cache.UserNeighbors, userId, aggregator.ToSlice()); err != nil { return errors.Trace(err) } - if err := m.CacheClient.DeleteDocuments(ctx, []string{cache.UserNeighbors}, cache.DocumentCondition{ + if err := m.CacheClient.DeleteScores(ctx, []string{cache.UserNeighbors}, cache.ScoreCondition{ Subset: proto.String(userId), Before: &aggregator.Timestamp, }); err != nil { @@ -808,10 +808,10 @@ func (m *Master) findUserNeighborsIVF(ctx context.Context, dataset *ranking.Data } aggregator := cache.NewDocumentAggregator(startSearchTime) aggregator.Add("", resultValues, resultScores) - if err := m.CacheClient.AddDocuments(ctx, cache.UserNeighbors, userId, aggregator.ToSlice()); err != nil { + if err := m.CacheClient.AddScores(ctx, cache.UserNeighbors, userId, aggregator.ToSlice()); err != nil { return errors.Trace(err) } - if err := m.CacheClient.DeleteDocuments(ctx, []string{cache.UserNeighbors}, cache.DocumentCondition{ + if err := m.CacheClient.DeleteScores(ctx, []string{cache.UserNeighbors}, cache.ScoreCondition{ Subset: proto.String(userId), Before: &aggregator.Timestamp, }); err != nil { @@ -872,7 +872,7 @@ func (m *Master) checkUserNeighborCacheTimeout(userId string) bool { ) ctx := context.Background() // check cache - if items, err := m.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, userId, []string{""}, 0, -1); err != nil { + if items, err := m.CacheClient.SearchScores(ctx, cache.UserNeighbors, userId, []string{""}, 0, -1); err != nil { log.Logger().Error("failed to load user neighbors", zap.String("user_id", userId), zap.Error(err)) return true } else if len(items) == 0 { @@ -927,7 +927,7 @@ func (m *Master) checkItemNeighborCacheTimeout(itemId string, categories []strin // check cache for _, category := range append([]string{""}, categories...) { - items, err := m.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, itemId, []string{category}, 0, -1) + items, err := m.CacheClient.SearchScores(ctx, cache.ItemNeighbors, itemId, []string{category}, 0, -1) if err != nil { log.Logger().Error("failed to load item neighbors", zap.String("item_id", itemId), zap.Error(err)) return true diff --git a/master/tasks_test.go b/master/tasks_test.go index 6750c43d0..8fa037047 100644 --- a/master/tasks_test.go +++ b/master/tasks_test.go @@ -89,11 +89,11 @@ func (s *MasterTestSuite) TestFindItemNeighborsBruteForce() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeRelated neighborTask := NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err := s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) + similar, err := s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) // similar items in category (common users) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "9", []string{"*"}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "9", []string{"*"}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "1"}, cache.ConvertDocumentsToValues(similar)) @@ -103,11 +103,11 @@ func (s *MasterTestSuite) TestFindItemNeighborsBruteForce() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeSimilar neighborTask = NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) // similar items in category (common labels) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "8", []string{"*"}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "8", []string{"*"}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "6"}, cache.ConvertDocumentsToValues(similar)) @@ -119,10 +119,10 @@ func (s *MasterTestSuite) TestFindItemNeighborsBruteForce() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeAuto neighborTask = NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) } @@ -194,11 +194,11 @@ func (s *MasterTestSuite) TestFindItemNeighborsIVF() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeRelated neighborTask := NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err := s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) + similar, err := s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) // similar items in category (common users) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "9", []string{"*"}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "9", []string{"*"}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "1"}, cache.ConvertDocumentsToValues(similar)) @@ -208,11 +208,11 @@ func (s *MasterTestSuite) TestFindItemNeighborsIVF() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeSimilar neighborTask = NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) // similar items in category (common labels) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "8", []string{"*"}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "8", []string{"*"}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "6"}, cache.ConvertDocumentsToValues(similar)) @@ -224,10 +224,10 @@ func (s *MasterTestSuite) TestFindItemNeighborsIVF() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeAuto neighborTask = NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) } @@ -261,7 +261,7 @@ func (s *MasterTestSuite) TestFindItemNeighborsIVF_ZeroIDF() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeRelated neighborTask := NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err := s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "0", []string{""}, 0, 100) + similar, err := s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "0", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"1"}, cache.ConvertDocumentsToValues(similar)) @@ -269,7 +269,7 @@ func (s *MasterTestSuite) TestFindItemNeighborsIVF_ZeroIDF() { s.Config.Recommend.ItemNeighbors.NeighborType = config.NeighborTypeSimilar neighborTask = NewFindItemNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, "0", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, "0", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"1"}, cache.ConvertDocumentsToValues(similar)) } @@ -321,7 +321,7 @@ func (s *MasterTestSuite) TestFindUserNeighborsBruteForce() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeRelated neighborTask := NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err := s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) + similar, err := s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) @@ -331,7 +331,7 @@ func (s *MasterTestSuite) TestFindUserNeighborsBruteForce() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeSimilar neighborTask = NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) @@ -343,10 +343,10 @@ func (s *MasterTestSuite) TestFindUserNeighborsBruteForce() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeAuto neighborTask = NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) } @@ -401,7 +401,7 @@ func (s *MasterTestSuite) TestFindUserNeighborsIVF() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeRelated neighborTask := NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err := s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) + similar, err := s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) @@ -411,7 +411,7 @@ func (s *MasterTestSuite) TestFindUserNeighborsIVF() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeSimilar neighborTask = NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) @@ -423,10 +423,10 @@ func (s *MasterTestSuite) TestFindUserNeighborsIVF() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeAuto neighborTask = NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "8", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"0", "2", "4"}, cache.ConvertDocumentsToValues(similar)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "9", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"7", "5", "3"}, cache.ConvertDocumentsToValues(similar)) } @@ -460,7 +460,7 @@ func (s *MasterTestSuite) TestFindUserNeighborsIVF_ZeroIDF() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeRelated neighborTask := NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err := s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "0", []string{""}, 0, 100) + similar, err := s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "0", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"1"}, cache.ConvertDocumentsToValues(similar)) @@ -468,7 +468,7 @@ func (s *MasterTestSuite) TestFindUserNeighborsIVF_ZeroIDF() { s.Config.Recommend.UserNeighbors.NeighborType = config.NeighborTypeSimilar neighborTask = NewFindUserNeighborsTask(&s.Master) s.NoError(neighborTask.run(context.Background(), nil)) - similar, err = s.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, "0", []string{""}, 0, 100) + similar, err = s.CacheClient.SearchScores(ctx, cache.UserNeighbors, "0", []string{""}, 0, 100) s.NoError(err) s.Equal([]string{"1"}, cache.ConvertDocumentsToValues(similar)) } @@ -568,43 +568,43 @@ func (s *MasterTestSuite) TestLoadDataFromDatabase() { s.Equal(45, s.clickTrainSet.NegativeCount+s.clickTestSet.NegativeCount) // check latest items - latest, err := s.CacheClient.SearchDocuments(ctx, cache.LatestItems, "", []string{""}, 0, 100) + latest, err := s.CacheClient.SearchScores(ctx, cache.LatestItems, "", []string{""}, 0, 100) s.NoError(err) - s.Equal([]cache.Document{ + s.Equal([]cache.Score{ {Id: items[8].ItemId, Score: float64(items[8].Timestamp.Unix())}, {Id: items[7].ItemId, Score: float64(items[7].Timestamp.Unix())}, {Id: items[6].ItemId, Score: float64(items[6].Timestamp.Unix())}, - }, lo.Map(latest, func(document cache.Document, _ int) cache.Document { - return cache.Document{Id: document.Id, Score: document.Score} + }, lo.Map(latest, func(document cache.Score, _ int) cache.Score { + return cache.Score{Id: document.Id, Score: document.Score} })) - latest, err = s.CacheClient.SearchDocuments(ctx, cache.LatestItems, "", []string{"2"}, 0, 100) + latest, err = s.CacheClient.SearchScores(ctx, cache.LatestItems, "", []string{"2"}, 0, 100) s.NoError(err) - s.Equal([]cache.Document{ + s.Equal([]cache.Score{ {Id: items[8].ItemId, Score: float64(items[8].Timestamp.Unix())}, {Id: items[5].ItemId, Score: float64(items[5].Timestamp.Unix())}, {Id: items[2].ItemId, Score: float64(items[2].Timestamp.Unix())}, - }, lo.Map(latest, func(document cache.Document, _ int) cache.Document { - return cache.Document{Id: document.Id, Score: document.Score} + }, lo.Map(latest, func(document cache.Score, _ int) cache.Score { + return cache.Score{Id: document.Id, Score: document.Score} })) // check popular items - popular, err := s.CacheClient.SearchDocuments(ctx, cache.PopularItems, "", []string{""}, 0, 3) + popular, err := s.CacheClient.SearchScores(ctx, cache.PopularItems, "", []string{""}, 0, 3) s.NoError(err) - s.Equal([]cache.Document{ + s.Equal([]cache.Score{ {Id: items[8].ItemId, Score: 9}, {Id: items[7].ItemId, Score: 8}, {Id: items[6].ItemId, Score: 7}, - }, lo.Map(popular, func(document cache.Document, _ int) cache.Document { - return cache.Document{Id: document.Id, Score: document.Score} + }, lo.Map(popular, func(document cache.Score, _ int) cache.Score { + return cache.Score{Id: document.Id, Score: document.Score} })) - popular, err = s.CacheClient.SearchDocuments(ctx, cache.PopularItems, "", []string{"2"}, 0, 3) + popular, err = s.CacheClient.SearchScores(ctx, cache.PopularItems, "", []string{"2"}, 0, 3) s.NoError(err) - s.Equal([]cache.Document{ + s.Equal([]cache.Score{ {Id: items[8].ItemId, Score: 9}, {Id: items[5].ItemId, Score: 6}, {Id: items[2].ItemId, Score: 3}, - }, lo.Map(popular, func(document cache.Document, _ int) cache.Document { - return cache.Document{Id: document.Id, Score: document.Score} + }, lo.Map(popular, func(document cache.Score, _ int) cache.Score { + return cache.Score{Id: document.Id, Score: document.Score} })) // check categories @@ -619,7 +619,7 @@ func (s *MasterTestSuite) TestCheckItemNeighborCacheTimeout() { // empty cache s.True(s.checkItemNeighborCacheTimeout("1", nil)) - err := s.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "1", []cache.Document{ + err := s.CacheClient.AddScores(ctx, cache.ItemNeighbors, "1", []cache.Score{ {Id: "2", Score: 1, Categories: []string{""}}, {Id: "3", Score: 2, Categories: []string{""}}, {Id: "4", Score: 3, Categories: []string{""}}, @@ -654,7 +654,7 @@ func (s *MasterTestSuite) TestCheckUserNeighborCacheTimeout() { // empty cache s.True(s.checkUserNeighborCacheTimeout("1")) - err := s.CacheClient.AddDocuments(ctx, cache.UserNeighbors, "1", []cache.Document{ + err := s.CacheClient.AddScores(ctx, cache.UserNeighbors, "1", []cache.Score{ {Id: "1", Score: 1, Categories: []string{""}}, {Id: "2", Score: 2, Categories: []string{""}}, {Id: "3", Score: 3, Categories: []string{""}}, diff --git a/server/bench_test.go b/server/bench_test.go index 417345aa7..53afe0493 100644 --- a/server/bench_test.go +++ b/server/bench_test.go @@ -797,14 +797,14 @@ func BenchmarkGetRecommendCache(b *testing.B) { ctx := context.Background() for batchSize := 10; batchSize <= 1000; batchSize *= 10 { b.Run(strconv.Itoa(batchSize), func(b *testing.B) { - documents := make([]cache.Document, batchSize) + documents := make([]cache.Score, batchSize) for i := range documents { documents[i].Id = strconv.Itoa(i) documents[i].Score = float64(i) documents[i].Categories = []string{""} } lo.Reverse(documents) - err := s.CacheClient.AddDocuments(ctx, cache.PopularItems, "", documents) + err := s.CacheClient.AddScores(ctx, cache.PopularItems, "", documents) require.NoError(b, err) s.Config.Recommend.CacheSize = len(documents) @@ -835,7 +835,7 @@ func BenchmarkRecommendFromOfflineCache(b *testing.B) { ctx := context.Background() for batchSize := 10; batchSize <= 1000; batchSize *= 10 { b.Run(strconv.Itoa(batchSize), func(b *testing.B) { - documents := make([]cache.Document, batchSize*2) + documents := make([]cache.Score, batchSize*2) expects := make([]string, batchSize) feedbacks := make([]data.Feedback, batchSize) for i := range documents { @@ -852,7 +852,7 @@ func BenchmarkRecommendFromOfflineCache(b *testing.B) { } lo.Reverse(documents) lo.Reverse(expects) - err := s.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "init_user_1", documents) + err := s.CacheClient.AddScores(ctx, cache.OfflineRecommend, "init_user_1", documents) require.NoError(b, err) err = s.DataClient.BatchInsertFeedback(ctx, feedbacks, true, true, true) require.NoError(b, err) @@ -886,7 +886,7 @@ func BenchmarkRecommendFromLatest(b *testing.B) { for batchSize := 10; batchSize <= 1000; batchSize *= 10 { b.Run(strconv.Itoa(batchSize), func(b *testing.B) { - documents := make([]cache.Document, batchSize*2) + documents := make([]cache.Score, batchSize*2) expects := make([]string, batchSize) feedbacks := make([]data.Feedback, batchSize) for i := range documents { @@ -903,7 +903,7 @@ func BenchmarkRecommendFromLatest(b *testing.B) { } lo.Reverse(documents) lo.Reverse(expects) - err := s.CacheClient.AddDocuments(ctx, cache.LatestItems, "", documents) + err := s.CacheClient.AddScores(ctx, cache.LatestItems, "", documents) require.NoError(b, err) err = s.DataClient.BatchInsertFeedback(ctx, feedbacks, true, true, true) require.NoError(b, err) @@ -938,7 +938,7 @@ func BenchmarkRecommendFromItemBased(b *testing.B) { for batchSize := 10; batchSize <= 1000; batchSize *= 10 { b.Run(strconv.Itoa(batchSize), func(b *testing.B) { // insert user feedbacks - documents := make([]cache.Document, batchSize*2) + documents := make([]cache.Score, batchSize*2) for i := range documents { documents[i].Id = fmt.Sprintf("init_item_%d", i) documents[i].Score = float64(i) @@ -958,7 +958,7 @@ func BenchmarkRecommendFromItemBased(b *testing.B) { // insert user neighbors for i := 0; i < s.Config.Recommend.Online.NumFeedbackFallbackItemBased; i++ { - err := s.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, fmt.Sprintf("init_item_%d", i), documents) + err := s.CacheClient.AddScores(ctx, cache.ItemNeighbors, fmt.Sprintf("init_item_%d", i), documents) require.NoError(b, err) } diff --git a/server/rest.go b/server/rest.go index 98ae7c40c..c0bab8ebb 100644 --- a/server/rest.go +++ b/server/rest.go @@ -418,8 +418,8 @@ func (s *RestServer) CreateWebService() { Param(ws.PathParameter("user-id", "ID of the user to get recommendation").DataType("string")). Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.GET("/intermediate/recommend/{user-id}/{category}").To(s.getCollaborative). Doc("Get the collaborative filtering recommendation for a user"). Metadata(restfulspec.KeyOpenAPITags, []string{DetractedAPITag}). @@ -428,8 +428,8 @@ func (s *RestServer) CreateWebService() { Param(ws.PathParameter("category", "Category of returned items.").DataType("string")). Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) // Get popular items ws.Route(ws.GET("/popular").To(s.getPopular). @@ -439,8 +439,8 @@ func (s *RestServer) CreateWebService() { Param(ws.QueryParameter("n", "Number of returned recommendations").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned recommendations").DataType("integer")). Param(ws.QueryParameter("user-id", "Remove read items of a user").DataType("string")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.GET("/popular/{category}").To(s.getPopular). Doc("Get popular items in category."). Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}). @@ -449,8 +449,8 @@ func (s *RestServer) CreateWebService() { Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). Param(ws.QueryParameter("user-id", "Remove read items of a user").DataType("string")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) // Get latest items ws.Route(ws.GET("/latest").To(s.getLatest). Doc("Get the latest items."). @@ -459,8 +459,8 @@ func (s *RestServer) CreateWebService() { Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). Param(ws.QueryParameter("user-id", "Remove read items of a user").DataType("string")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.GET("/latest/{category}").To(s.getLatest). Doc("Get the latest items in category."). Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}). @@ -469,8 +469,8 @@ func (s *RestServer) CreateWebService() { Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). Param(ws.QueryParameter("user-id", "Remove read items of a user").DataType("string")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) // Get neighbors ws.Route(ws.GET("/item/{item-id}/neighbors/").To(s.getItemNeighbors). Doc("Get neighbors of a item"). @@ -479,8 +479,8 @@ func (s *RestServer) CreateWebService() { Param(ws.PathParameter("item-id", "ID of the item to get neighbors").DataType("string")). Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.GET("/item/{item-id}/neighbors/{category}").To(s.getItemNeighbors). Doc("Get neighbors of a item in category."). Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}). @@ -489,8 +489,8 @@ func (s *RestServer) CreateWebService() { Param(ws.PathParameter("category", "Category of returned items").DataType("string")). Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.GET("/user/{user-id}/neighbors/").To(s.getUserNeighbors). Doc("Get neighbors of a user."). Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}). @@ -498,8 +498,8 @@ func (s *RestServer) CreateWebService() { Param(ws.PathParameter("user-id", "ID of the user to get neighbors").DataType("string")). Param(ws.QueryParameter("n", "Number of returned users").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned users").DataType("integer")). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.GET("/recommend/{user-id}").To(s.getRecommend). Doc("Get recommendation for user."). Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}). @@ -531,8 +531,8 @@ func (s *RestServer) CreateWebService() { Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). Reads([]Feedback{}). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.POST("/session/recommend/{category}").To(s.sessionRecommend). Doc("Get recommendation for session."). Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}). @@ -541,8 +541,8 @@ func (s *RestServer) CreateWebService() { Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). Reads([]Feedback{}). - Returns(http.StatusOK, "OK", []cache.Document{}). - Writes([]cache.Document{})) + Returns(http.StatusOK, "OK", []cache.Score{}). + Writes([]cache.Score{})) ws.Route(ws.GET("/measurements/{name}").To(s.getMeasurements). Doc("Get measurements."). @@ -612,7 +612,7 @@ func (s *RestServer) searchDocuments(collection, subset, category string, isItem } // Get the sorted list - items, err := s.CacheClient.SearchDocuments(ctx, collection, subset, []string{category}, offset, end) + items, err := s.CacheClient.SearchScores(ctx, collection, subset, []string{category}, offset, end) if err != nil { InternalServerError(response, err) return @@ -620,7 +620,7 @@ func (s *RestServer) searchDocuments(collection, subset, category string, isItem // Remove read items if userId != "" { - prunedItems := make([]cache.Document, 0, len(items)) + prunedItems := make([]cache.Score, 0, len(items)) for _, item := range items { if !readItems.Contains(item.Id) { prunedItems = append(prunedItems, item) @@ -799,7 +799,7 @@ type Recommender func(ctx *recommendContext) error func (s *RestServer) RecommendOffline(ctx *recommendContext) error { if len(ctx.results) < ctx.n { start := time.Now() - recommendation, err := s.CacheClient.SearchDocuments(ctx.context, cache.OfflineRecommend, ctx.userId, ctx.categories, 0, s.Config.Recommend.CacheSize) + recommendation, err := s.CacheClient.SearchScores(ctx.context, cache.OfflineRecommend, ctx.userId, ctx.categories, 0, s.Config.Recommend.CacheSize) if err != nil { return errors.Trace(err) } @@ -819,7 +819,7 @@ func (s *RestServer) RecommendOffline(ctx *recommendContext) error { func (s *RestServer) RecommendCollaborative(ctx *recommendContext) error { if len(ctx.results) < ctx.n { start := time.Now() - collaborativeRecommendation, err := s.CacheClient.SearchDocuments(ctx.context, cache.CollaborativeRecommend, ctx.userId, ctx.categories, 0, s.Config.Recommend.CacheSize) + collaborativeRecommendation, err := s.CacheClient.SearchScores(ctx.context, cache.CollaborativeRecommend, ctx.userId, ctx.categories, 0, s.Config.Recommend.CacheSize) if err != nil { return errors.Trace(err) } @@ -841,7 +841,7 @@ func (s *RestServer) RecommendUserBased(ctx *recommendContext) error { start := time.Now() candidates := make(map[string]float64) // load similar users - similarUsers, err := s.CacheClient.SearchDocuments(ctx.context, cache.UserNeighbors, ctx.userId, []string{""}, 0, s.Config.Recommend.CacheSize) + similarUsers, err := s.CacheClient.SearchScores(ctx.context, cache.UserNeighbors, ctx.userId, []string{""}, 0, s.Config.Recommend.CacheSize) if err != nil { return errors.Trace(err) } @@ -898,7 +898,7 @@ func (s *RestServer) RecommendItemBased(ctx *recommendContext) error { candidates := make(map[string]float64) for _, feedback := range userFeedback { // load similar items - similarItems, err := s.CacheClient.SearchDocuments(ctx.context, cache.ItemNeighbors, feedback.ItemId, ctx.categories, 0, s.Config.Recommend.CacheSize) + similarItems, err := s.CacheClient.SearchScores(ctx.context, cache.ItemNeighbors, feedback.ItemId, ctx.categories, 0, s.Config.Recommend.CacheSize) if err != nil { return errors.Trace(err) } @@ -928,7 +928,7 @@ func (s *RestServer) RecommendItemBased(ctx *recommendContext) error { func (s *RestServer) RecommendLatest(ctx *recommendContext) error { if len(ctx.results) < ctx.n { start := time.Now() - items, err := s.CacheClient.SearchDocuments(ctx.context, cache.LatestItems, "", ctx.categories, 0, s.Config.Recommend.CacheSize) + items, err := s.CacheClient.SearchScores(ctx.context, cache.LatestItems, "", ctx.categories, 0, s.Config.Recommend.CacheSize) if err != nil { return errors.Trace(err) } @@ -948,7 +948,7 @@ func (s *RestServer) RecommendLatest(ctx *recommendContext) error { func (s *RestServer) RecommendPopular(ctx *recommendContext) error { if len(ctx.results) < ctx.n { start := time.Now() - items, err := s.CacheClient.SearchDocuments(ctx.context, cache.PopularItems, "", ctx.categories, 0, s.Config.Recommend.CacheSize) + items, err := s.CacheClient.SearchScores(ctx.context, cache.PopularItems, "", ctx.categories, 0, s.Config.Recommend.CacheSize) if err != nil { return errors.Trace(err) } @@ -1090,7 +1090,7 @@ func (s *RestServer) sessionRecommend(request *restful.Request, response *restfu usedFeedbackCount := 0 for _, feedback := range userFeedback { // load similar items - similarItems, err := s.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, feedback.ItemId, []string{category}, 0, s.Config.Recommend.CacheSize) + similarItems, err := s.CacheClient.SearchScores(ctx, cache.ItemNeighbors, feedback.ItemId, []string{category}, 0, s.Config.Recommend.CacheSize) if err != nil { BadRequest(response, err) return @@ -1116,8 +1116,8 @@ func (s *RestServer) sessionRecommend(request *restful.Request, response *restfu filter.Push(id, score) } names, scores := filter.PopAll() - result := lo.Map(names, func(_ string, i int) cache.Document { - return cache.Document{ + result := lo.Map(names, func(_ string, i int) cache.Score { + return cache.Score{ Id: names[i], Score: scores[i], } @@ -1378,7 +1378,7 @@ func (s *RestServer) batchInsertItems(ctx context.Context, response *restful.Res Comment: item.Comment, }) // insert to latest items cache - if err = s.CacheClient.AddDocuments(ctx, cache.LatestItems, "", []cache.Document{{ + if err = s.CacheClient.AddScores(ctx, cache.LatestItems, "", []cache.Score{{ Id: item.ItemId, Score: float64(timestamp.Unix()), Categories: withWildCard(item.Categories), @@ -1388,7 +1388,7 @@ func (s *RestServer) batchInsertItems(ctx context.Context, response *restful.Res return } // update items cache - if err = s.CacheClient.UpdateDocuments(ctx, cache.ItemCache, item.ItemId, cache.DocumentPatch{ + if err = s.CacheClient.UpdateScores(ctx, cache.ItemCache, item.ItemId, cache.ScorePatch{ Categories: withWildCard(item.Categories), IsHidden: &item.IsHidden, }); err != nil { @@ -1492,21 +1492,21 @@ func (s *RestServer) modifyItem(request *restful.Request, response *restful.Resp } // remove hidden item from cache if patch.IsHidden != nil { - if err := s.CacheClient.UpdateDocuments(ctx, cache.ItemCache, itemId, cache.DocumentPatch{IsHidden: patch.IsHidden}); err != nil { + if err := s.CacheClient.UpdateScores(ctx, cache.ItemCache, itemId, cache.ScorePatch{IsHidden: patch.IsHidden}); err != nil { InternalServerError(response, err) return } } // add item to latest items cache if patch.Timestamp != nil { - if err := s.CacheClient.UpdateDocuments(ctx, []string{cache.LatestItems}, itemId, cache.DocumentPatch{Score: proto.Float64(float64(patch.Timestamp.Unix()))}); err != nil { + if err := s.CacheClient.UpdateScores(ctx, []string{cache.LatestItems}, itemId, cache.ScorePatch{Score: proto.Float64(float64(patch.Timestamp.Unix()))}); err != nil { InternalServerError(response, err) return } } // update categories in cache if patch.Categories != nil { - if err := s.CacheClient.UpdateDocuments(ctx, cache.ItemCache, itemId, cache.DocumentPatch{Categories: withWildCard(patch.Categories)}); err != nil { + if err := s.CacheClient.UpdateScores(ctx, cache.ItemCache, itemId, cache.ScorePatch{Categories: withWildCard(patch.Categories)}); err != nil { InternalServerError(response, err) return } @@ -1580,7 +1580,7 @@ func (s *RestServer) deleteItem(request *restful.Request, response *restful.Resp return } // delete item from cache - if err := s.CacheClient.DeleteDocuments(ctx, cache.ItemCache, cache.DocumentCondition{Id: &itemId}); err != nil { + if err := s.CacheClient.DeleteScores(ctx, cache.ItemCache, cache.ScoreCondition{Id: &itemId}); err != nil { InternalServerError(response, err) return } @@ -1610,7 +1610,7 @@ func (s *RestServer) insertItemCategory(request *restful.Request, response *rest return } // insert category to cache - if err = s.CacheClient.UpdateDocuments(ctx, cache.ItemCache, itemId, cache.DocumentPatch{Categories: withWildCard(item.Categories)}); err != nil { + if err = s.CacheClient.UpdateScores(ctx, cache.ItemCache, itemId, cache.ScorePatch{Categories: withWildCard(item.Categories)}); err != nil { InternalServerError(response, err) return } @@ -1639,7 +1639,7 @@ func (s *RestServer) deleteItemCategory(request *restful.Request, response *rest } item.Categories = categories // delete category from cache - if err = s.CacheClient.UpdateDocuments(ctx, cache.ItemCache, itemId, cache.DocumentPatch{Categories: withWildCard(categories)}); err != nil { + if err = s.CacheClient.UpdateScores(ctx, cache.ItemCache, itemId, cache.ScorePatch{Categories: withWildCard(categories)}); err != nil { InternalServerError(response, err) return } diff --git a/server/rest_test.go b/server/rest_test.go index a30670e4e..95c544fb9 100644 --- a/server/rest_test.go +++ b/server/rest_test.go @@ -243,7 +243,7 @@ func (suite *ServerTestSuite) TestItems() { }, } // insert popular scores - err := suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.PopularItems, "", []cache.Score{ {Id: "0", Score: 10}, {Id: "2", Score: 12}, {Id: "4", Score: 14}, @@ -297,7 +297,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[3].ItemId, Score: float64(items[3].Timestamp.Unix())}, {Id: items[1].ItemId, Score: float64(items[1].Timestamp.Unix())}, })). @@ -311,7 +311,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[3].ItemId, Score: float64(items[3].Timestamp.Unix())}, {Id: items[1].ItemId, Score: float64(items[1].Timestamp.Unix())}, })). @@ -326,7 +326,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[3].ItemId, Score: 16}, {Id: items[1].ItemId, Score: 12}, })). @@ -340,7 +340,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[3].ItemId, Score: 16}, {Id: items[1].ItemId, Score: 12}, })). @@ -377,7 +377,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[1].ItemId, Score: float64(items[1].Timestamp.Unix())}, })). End() @@ -390,7 +390,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[1].ItemId, Score: float64(items[1].Timestamp.Unix())}, })). End() @@ -404,7 +404,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[1].ItemId, Score: 12}, })). End() @@ -417,7 +417,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: items[1].ItemId, Score: 12}, })). End() @@ -475,7 +475,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: "2", Score: float64(timestamp.Unix())}, })). End() @@ -488,7 +488,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{})). + Body(suite.marshal([]cache.Score{})). End() // get popular items apitest.New(). @@ -500,7 +500,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: "2", Score: 12}, })). End() @@ -513,7 +513,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{})). + Body(suite.marshal([]cache.Score{})). End() // insert category @@ -550,7 +550,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: "2", Score: float64(timestamp.Unix())}, })). End() @@ -564,7 +564,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{ + Body(suite.marshal([]cache.Score{ {Id: "2", Score: 12}, })). End() @@ -603,7 +603,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{})). + Body(suite.marshal([]cache.Score{})). End() // get popular items apitest.New(). @@ -615,7 +615,7 @@ func (suite *ServerTestSuite) TestItems() { }). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{})). + Body(suite.marshal([]cache.Score{})). End() // insert items without timestamp @@ -833,14 +833,14 @@ func (suite *ServerTestSuite) TestNonPersonalizedRecommend() { for i, operator := range operators { suite.T().Run(operator.Name, func(t *testing.T) { // insert documents - documents := []cache.Document{ + documents := []cache.Score{ {Id: strconv.Itoa(i) + "0", Score: 100, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "1", Score: 99, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "2", Score: 98, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "3", Score: 97, Categories: []string{operator.Category}}, {Id: strconv.Itoa(i) + "4", Score: 96, Categories: []string{operator.Category}}, } - err := suite.CacheClient.AddDocuments(ctx, operator.Collection, operator.Subset, documents) + err := suite.CacheClient.AddScores(ctx, operator.Collection, operator.Subset, documents) assert.NoError(t, err) // hidden item apitest.New(). @@ -868,7 +868,7 @@ func (suite *ServerTestSuite) TestNonPersonalizedRecommend() { Header("X-API-Key", apiKey). Expect(t). Status(http.StatusOK). - Body(suite.marshal(([]cache.Document{documents[0], documents[1], documents[2], documents[4]}))). + Body(suite.marshal(([]cache.Score{documents[0], documents[1], documents[2], documents[4]}))). End() apitest.New(). Handler(suite.handler). @@ -879,7 +879,7 @@ func (suite *ServerTestSuite) TestNonPersonalizedRecommend() { "n": "3"}). Expect(t). Status(http.StatusOK). - Body(suite.marshal(([]cache.Document{documents[0], documents[1], documents[2]}))). + Body(suite.marshal(([]cache.Score{documents[0], documents[1], documents[2]}))). End() apitest.New(). Handler(suite.handler). @@ -890,7 +890,7 @@ func (suite *ServerTestSuite) TestNonPersonalizedRecommend() { "n": "3"}). Expect(t). Status(http.StatusOK). - Body(suite.marshal(([]cache.Document{documents[1], documents[2], documents[4]}))). + Body(suite.marshal(([]cache.Score{documents[1], documents[2], documents[4]}))). End() apitest.New(). Handler(suite.handler). @@ -901,7 +901,7 @@ func (suite *ServerTestSuite) TestNonPersonalizedRecommend() { "n": "0"}). Expect(t). Status(http.StatusOK). - Body(suite.marshal(([]cache.Document{documents[0], documents[1], documents[2], documents[4]}))). + Body(suite.marshal(([]cache.Score{documents[0], documents[1], documents[2], documents[4]}))). End() apitest.New(). Handler(suite.handler). @@ -913,7 +913,7 @@ func (suite *ServerTestSuite) TestNonPersonalizedRecommend() { "n": "0"}). Expect(t). Status(http.StatusOK). - Body(suite.marshal(([]cache.Document{documents[0], documents[2], documents[4]}))). + Body(suite.marshal(([]cache.Score{documents[0], documents[2], documents[4]}))). End() }) } @@ -1017,7 +1017,7 @@ func (suite *ServerTestSuite) TestGetRecommends() { ctx := context.Background() t := suite.T() // insert hidden items - err := suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{{Id: "0", Score: 100, Categories: []string{""}}}) + err := suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{{Id: "0", Score: 100, Categories: []string{""}}}) assert.NoError(t, err) // hide item apitest.New(). @@ -1041,7 +1041,7 @@ func (suite *ServerTestSuite) TestGetRecommends() { }) assert.NoError(t, err) // insert recommendation - err = suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{ {Id: "1", Score: 99, Categories: []string{""}}, {Id: "2", Score: 98, Categories: []string{""}}, {Id: "3", Score: 97, Categories: []string{""}}, @@ -1144,7 +1144,7 @@ func (suite *ServerTestSuite) TestGetRecommendsWithMultiCategories() { ctx := context.Background() t := suite.T() // insert recommendation - err := suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{ {Id: "1", Score: 1, Categories: []string{""}}, {Id: "2", Score: 2, Categories: []string{"", "2"}}, {Id: "3", Score: 3, Categories: []string{"", "3"}}, @@ -1175,7 +1175,7 @@ func (suite *ServerTestSuite) TestGetRecommendsWithReplacement() { t := suite.T() suite.Config.Recommend.Replacement.EnableReplacement = true // insert recommendation - err := suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{ {Id: "0", Score: 100, Categories: []string{""}}, {Id: "1", Score: 99, Categories: []string{""}}, {Id: "2", Score: 98, Categories: []string{""}}, @@ -1230,7 +1230,7 @@ func (suite *ServerTestSuite) TestServerGetRecommendsFallbackItemBasedSimilar() suite.Config.Recommend.Online.NumFeedbackFallbackItemBased = 4 suite.Config.Recommend.DataSource.PositiveFeedbackTypes = []string{"a"} // insert recommendation - err := suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{ {Id: "1", Score: 99}, {Id: "2", Score: 98}, {Id: "3", Score: 97}, @@ -1255,25 +1255,25 @@ func (suite *ServerTestSuite) TestServerGetRecommendsFallbackItemBasedSimilar() End() // insert similar items - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "1", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "1", []cache.Score{ {Id: "2", Score: 100000, Categories: []string{""}}, {Id: "9", Score: 1, Categories: []string{"", "*"}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "2", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "2", []cache.Score{ {Id: "3", Score: 100000, Categories: []string{"", "*"}}, {Id: "8", Score: 1, Categories: []string{""}}, {Id: "9", Score: 1, Categories: []string{"", "*"}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "3", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "3", []cache.Score{ {Id: "4", Score: 100000, Categories: []string{""}}, {Id: "7", Score: 1, Categories: []string{"", "*"}}, {Id: "8", Score: 1, Categories: []string{""}}, {Id: "9", Score: 1, Categories: []string{"", "*"}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "4", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "4", []cache.Score{ {Id: "1", Score: 100000, Categories: []string{"", "*"}}, {Id: "6", Score: 1, Categories: []string{""}}, {Id: "7", Score: 1, Categories: []string{"", "*"}}, @@ -1281,7 +1281,7 @@ func (suite *ServerTestSuite) TestServerGetRecommendsFallbackItemBasedSimilar() {Id: "9", Score: 1, Categories: []string{"", "*"}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "5", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "5", []cache.Score{ {Id: "1", Score: 1, Categories: []string{""}}, {Id: "6", Score: 1, Categories: []string{""}}, {Id: "7", Score: 100000, Categories: []string{""}}, @@ -1321,8 +1321,8 @@ func (suite *ServerTestSuite) TestGetRecommendsFallbackUserBasedSimilar() { ctx := context.Background() t := suite.T() // insert recommendation - err := suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", - []cache.Document{{Id: "1", Score: 99}, {Id: "2", Score: 98}, {Id: "3", Score: 97}, {Id: "4", Score: 96}}) + err := suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", + []cache.Score{{Id: "1", Score: 99}, {Id: "2", Score: 98}, {Id: "3", Score: 97}, {Id: "4", Score: 96}}) assert.NoError(t, err) // insert feedback feedback := []data.Feedback{ @@ -1341,7 +1341,7 @@ func (suite *ServerTestSuite) TestGetRecommendsFallbackUserBasedSimilar() { Body(`{"RowAffected": 4}`). End() // insert similar users - err = suite.CacheClient.AddDocuments(ctx, cache.UserNeighbors, "0", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.UserNeighbors, "0", []cache.Score{ {Id: "1", Score: 2, Categories: []string{""}}, {Id: "2", Score: 1.5, Categories: []string{""}}, {Id: "3", Score: 1, Categories: []string{""}}, @@ -1397,52 +1397,52 @@ func (suite *ServerTestSuite) TestGetRecommendsFallbackPreCached() { ctx := context.Background() t := suite.T() // insert offline recommendation - err := suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{ {Id: "1", Score: 99, Categories: []string{""}}, {Id: "2", Score: 98, Categories: []string{""}}, {Id: "3", Score: 97, Categories: []string{""}}, {Id: "4", Score: 96, Categories: []string{""}}}) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{ {Id: "101", Score: 99, Categories: []string{"*"}}, {Id: "102", Score: 98, Categories: []string{"*"}}, {Id: "103", Score: 97, Categories: []string{"*"}}, {Id: "104", Score: 96, Categories: []string{"*"}}}) assert.NoError(t, err) // insert latest - err = suite.CacheClient.AddDocuments(ctx, cache.LatestItems, "", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.LatestItems, "", []cache.Score{ {Id: "5", Score: 95, Categories: []string{""}}, {Id: "6", Score: 94, Categories: []string{""}}, {Id: "7", Score: 93, Categories: []string{""}}, {Id: "8", Score: 92, Categories: []string{""}}}) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.LatestItems, "", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.LatestItems, "", []cache.Score{ {Id: "105", Score: 95, Categories: []string{"*"}}, {Id: "106", Score: 94, Categories: []string{"*"}}, {Id: "107", Score: 93, Categories: []string{"*"}}, {Id: "108", Score: 92, Categories: []string{"*"}}}) assert.NoError(t, err) // insert popular - err = suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.PopularItems, "", []cache.Score{ {Id: "9", Score: 91, Categories: []string{""}}, {Id: "10", Score: 90, Categories: []string{""}}, {Id: "11", Score: 89, Categories: []string{""}}, {Id: "12", Score: 88, Categories: []string{""}}}) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.PopularItems, "", []cache.Score{ {Id: "109", Score: 91, Categories: []string{"*"}}, {Id: "110", Score: 90, Categories: []string{"*"}}, {Id: "111", Score: 89, Categories: []string{"*"}}, {Id: "112", Score: 88, Categories: []string{"*"}}}) assert.NoError(t, err) // insert collaborative filtering - err = suite.CacheClient.AddDocuments(ctx, cache.CollaborativeRecommend, "0", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.CollaborativeRecommend, "0", []cache.Score{ {Id: "13", Score: 79, Categories: []string{""}}, {Id: "14", Score: 78, Categories: []string{""}}, {Id: "15", Score: 77, Categories: []string{""}}, {Id: "16", Score: 76, Categories: []string{""}}}) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.CollaborativeRecommend, "0", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.CollaborativeRecommend, "0", []cache.Score{ {Id: "113", Score: 79, Categories: []string{"*"}}, {Id: "114", Score: 78, Categories: []string{"*"}}, {Id: "115", Score: 77, Categories: []string{"*"}}, @@ -1541,26 +1541,26 @@ func (suite *ServerTestSuite) TestSessionRecommend() { suite.Config.Recommend.DataSource.PositiveFeedbackTypes = []string{"a"} // insert similar items - err := suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "1", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "1", []cache.Score{ {Id: "2", Score: 100000, Categories: []string{""}}, {Id: "9", Score: 1, Categories: []string{"", "*"}}, {Id: "100", Score: 100000, Categories: []string{""}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "2", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "2", []cache.Score{ {Id: "3", Score: 100000, Categories: []string{"", "*"}}, {Id: "8", Score: 1, Categories: []string{""}}, {Id: "9", Score: 1, Categories: []string{"", "*"}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "3", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "3", []cache.Score{ {Id: "4", Score: 100000, Categories: []string{""}}, {Id: "7", Score: 1, Categories: []string{"", "*"}}, {Id: "8", Score: 1, Categories: []string{""}}, {Id: "9", Score: 1, Categories: []string{"", "*"}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "4", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "4", []cache.Score{ {Id: "1", Score: 100000, Categories: []string{"", "*"}}, {Id: "6", Score: 1, Categories: []string{""}}, {Id: "7", Score: 1, Categories: []string{"", "*"}}, @@ -1568,7 +1568,7 @@ func (suite *ServerTestSuite) TestSessionRecommend() { {Id: "9", Score: 1, Categories: []string{"", "*"}}, }) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "5", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "5", []cache.Score{ {Id: "1", Score: 1, Categories: []string{""}}, {Id: "6", Score: 1, Categories: []string{""}}, {Id: "7", Score: 100000, Categories: []string{""}}, @@ -1606,7 +1606,7 @@ func (suite *ServerTestSuite) TestSessionRecommend() { JSON(feedback). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{{Id: "9", Score: 4}, {Id: "8", Score: 3}, {Id: "7", Score: 2}})). + Body(suite.marshal([]cache.Score{{Id: "9", Score: 4}, {Id: "8", Score: 3}, {Id: "7", Score: 2}})). End() apitest.New(). Handler(suite.handler). @@ -1618,7 +1618,7 @@ func (suite *ServerTestSuite) TestSessionRecommend() { JSON(feedback). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document(nil))). + Body(suite.marshal([]cache.Score(nil))). End() suite.Config.Recommend.Online.FallbackRecommend = []string{"item_based"} apitest.New(). @@ -1631,7 +1631,7 @@ func (suite *ServerTestSuite) TestSessionRecommend() { JSON(feedback). Expect(t). Status(http.StatusOK). - Body(suite.marshal([]cache.Document{{Id: "9", Score: 4}, {Id: "7", Score: 2}})). + Body(suite.marshal([]cache.Score{{Id: "9", Score: 4}, {Id: "7", Score: 2}})). End() } @@ -1660,22 +1660,22 @@ func (suite *ServerTestSuite) TestVisibility() { End() // insert cache - var documents []cache.Document + var documents []cache.Score for i := range items { - documents = append(documents, cache.Document{ + documents = append(documents, cache.Score{ Id: strconv.Itoa(i), Score: float64(time.Date(1989, 6, i+1, 1, 1, 1, 1, time.UTC).Unix()), Categories: []string{"", "a"}, }) } lo.Reverse(documents) - err := suite.CacheClient.AddDocuments(ctx, cache.LatestItems, "", documents) + err := suite.CacheClient.AddScores(ctx, cache.LatestItems, "", documents) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", documents) + err = suite.CacheClient.AddScores(ctx, cache.PopularItems, "", documents) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "100", documents) + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "100", documents) assert.NoError(t, err) - err = suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "100", documents) + err = suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "100", documents) assert.NoError(t, err) // delete item diff --git a/storage/cache/database.go b/storage/cache/database.go index 6fa1ce143..60b58d636 100644 --- a/storage/cache/database.go +++ b/storage/cache/database.go @@ -182,7 +182,7 @@ func (r *ReturnValue) Time() (time.Time, error) { return t.In(time.UTC), nil } -type Document struct { +type Score struct { Id string Score float64 IsHidden bool `json:"-"` @@ -190,13 +190,13 @@ type Document struct { Timestamp time.Time `json:"-"` } -func SortDocuments(documents []Document) { +func SortDocuments(documents []Score) { sort.Slice(documents, func(i, j int) bool { return documents[i].Score > documents[j].Score }) } -func ConvertDocumentsToValues(documents []Document) []string { +func ConvertDocumentsToValues(documents []Score) []string { values := make([]string, len(documents)) for i := range values { values[i] = documents[i].Id @@ -208,13 +208,13 @@ func ConvertDocumentsToValues(documents []Document) []string { // In old recommender system, the recommendation is genereated per category. // In the new recommender system, the recommendation is generated globally. type DocumentAggregator struct { - Documents map[string]*Document + Documents map[string]*Score Timestamp time.Time } func NewDocumentAggregator(timestamp time.Time) *DocumentAggregator { return &DocumentAggregator{ - Documents: make(map[string]*Document), + Documents: make(map[string]*Score), Timestamp: timestamp, } } @@ -222,7 +222,7 @@ func NewDocumentAggregator(timestamp time.Time) *DocumentAggregator { func (aggregator *DocumentAggregator) Add(category string, values []string, scores []float64) { for i, value := range values { if _, ok := aggregator.Documents[value]; !ok { - aggregator.Documents[value] = &Document{ + aggregator.Documents[value] = &Score{ Id: value, Score: scores[i], Categories: []string{category}, @@ -235,8 +235,8 @@ func (aggregator *DocumentAggregator) Add(category string, values []string, scor } } -func (aggregator *DocumentAggregator) ToSlice() []Document { - documents := make([]Document, 0, len(aggregator.Documents)) +func (aggregator *DocumentAggregator) ToSlice() []Score { + documents := make([]Score, 0, len(aggregator.Documents)) for _, document := range aggregator.Documents { sort.Strings(document.Categories) documents = append(documents, *document) @@ -244,20 +244,20 @@ func (aggregator *DocumentAggregator) ToSlice() []Document { return documents } -type DocumentCondition struct { +type ScoreCondition struct { Subset *string Id *string Before *time.Time } -func (condition *DocumentCondition) Check() error { +func (condition *ScoreCondition) Check() error { if condition.Id == nil && condition.Before == nil && condition.Subset == nil { return errors.NotValidf("document condition") } return nil } -type DocumentPatch struct { +type ScorePatch struct { IsHidden *bool Categories []string Score *float64 @@ -290,10 +290,10 @@ type Database interface { Pop(ctx context.Context, name string) (string, error) Remain(ctx context.Context, name string) (int64, error) - AddDocuments(ctx context.Context, collection, subset string, documents []Document) error - SearchDocuments(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Document, error) - DeleteDocuments(ctx context.Context, collection []string, condition DocumentCondition) error - UpdateDocuments(ctx context.Context, collection []string, id string, patch DocumentPatch) error + AddScores(ctx context.Context, collection, subset string, documents []Score) error + SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error) + DeleteScores(ctx context.Context, collection []string, condition ScoreCondition) error + UpdateScores(ctx context.Context, collection []string, id string, patch ScorePatch) error AddTimeSeriesPoints(ctx context.Context, points []TimeSeriesPoint) error GetTimeSeriesPoints(ctx context.Context, name string, begin, end time.Time) ([]TimeSeriesPoint, error) diff --git a/storage/cache/database_test.go b/storage/cache/database_test.go index 9bd8235d7..5046d603d 100644 --- a/storage/cache/database_test.go +++ b/storage/cache/database_test.go @@ -228,7 +228,7 @@ func (suite *baseTestSuite) TestPushPop() { func (suite *baseTestSuite) TestDocument() { ts := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) ctx := context.Background() - err := suite.AddDocuments(ctx, "a", "", []Document{{ + err := suite.AddScores(ctx, "a", "", []Score{{ Id: "0", Score: math.MaxFloat64, IsHidden: true, @@ -236,14 +236,14 @@ func (suite *baseTestSuite) TestDocument() { Timestamp: ts, }}) suite.NoError(err) - err = suite.AddDocuments(ctx, "a", "", []Document{{ + err = suite.AddScores(ctx, "a", "", []Score{{ Id: "1", Score: 100, Categories: []string{"a", "b"}, Timestamp: ts, }}) suite.NoError(err) - err = suite.AddDocuments(ctx, "a", "", []Document{ + err = suite.AddScores(ctx, "a", "", []Score{ { Id: "1", Score: 1, @@ -276,7 +276,7 @@ func (suite *baseTestSuite) TestDocument() { }, }) suite.NoError(err) - err = suite.AddDocuments(ctx, "b", "", []Document{{ + err = suite.AddScores(ctx, "b", "", []Score{{ Id: "6", Score: 6, Categories: []string{"b"}, @@ -285,15 +285,15 @@ func (suite *baseTestSuite) TestDocument() { suite.NoError(err) // search documents - documents, err := suite.SearchDocuments(ctx, "a", "", []string{"b"}, 1, 3) + documents, err := suite.SearchScores(ctx, "a", "", []string{"b"}, 1, 3) suite.NoError(err) - suite.Equal([]Document{ + suite.Equal([]Score{ {Id: "3", Score: 3, Categories: []string{"b"}, Timestamp: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)}, {Id: "2", Score: 2, Categories: []string{"b", "c"}, Timestamp: ts}, }, documents) - documents, err = suite.SearchDocuments(ctx, "a", "", []string{"b"}, 0, -1) + documents, err = suite.SearchScores(ctx, "a", "", []string{"b"}, 0, -1) suite.NoError(err) - suite.Equal([]Document{ + suite.Equal([]Score{ {Id: "5", Score: 5, Categories: []string{"b"}, Timestamp: ts}, {Id: "3", Score: 3, Categories: []string{"b"}, Timestamp: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)}, {Id: "2", Score: 2, Categories: []string{"b", "c"}, Timestamp: ts}, @@ -301,45 +301,45 @@ func (suite *baseTestSuite) TestDocument() { }, documents) // search documents with empty category - documents, err = suite.SearchDocuments(ctx, "a", "", []string{""}, 0, -1) + documents, err = suite.SearchScores(ctx, "a", "", []string{""}, 0, -1) suite.NoError(err) - suite.Equal([]Document{{Id: "4", Score: 4, Categories: []string{""}, Timestamp: ts}}, documents) + suite.Equal([]Score{{Id: "4", Score: 4, Categories: []string{""}, Timestamp: ts}}, documents) // delete nothing - err = suite.DeleteDocuments(ctx, []string{"a"}, DocumentCondition{}) + err = suite.DeleteScores(ctx, []string{"a"}, ScoreCondition{}) suite.ErrorIs(err, errors.NotValid) // delete by value - err = suite.DeleteDocuments(ctx, []string{"a"}, DocumentCondition{Id: proto.String("5")}) + err = suite.DeleteScores(ctx, []string{"a"}, ScoreCondition{Id: proto.String("5")}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "", []string{"b"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "", []string{"b"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("3", documents[0].Id) // delete by timestamp - err = suite.DeleteDocuments(ctx, []string{"a"}, DocumentCondition{Before: lo.ToPtr(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC))}) + err = suite.DeleteScores(ctx, []string{"a"}, ScoreCondition{Before: lo.ToPtr(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC))}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "", []string{"b"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "", []string{"b"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("2", documents[0].Id) // update categories - err = suite.UpdateDocuments(ctx, []string{"a"}, "2", DocumentPatch{Categories: []string{"c", "s"}}) + err = suite.UpdateScores(ctx, []string{"a"}, "2", ScorePatch{Categories: []string{"c", "s"}}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "", []string{"s"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "", []string{"s"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("2", documents[0].Id) - err = suite.UpdateDocuments(ctx, []string{"a"}, "2", DocumentPatch{Categories: []string{"c"}}) + err = suite.UpdateScores(ctx, []string{"a"}, "2", ScorePatch{Categories: []string{"c"}}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "", []string{"s"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "", []string{"s"}, 0, 1) suite.NoError(err) suite.Empty(documents) // update is hidden - err = suite.UpdateDocuments(ctx, []string{"a"}, "0", DocumentPatch{IsHidden: proto.Bool(false)}) + err = suite.UpdateScores(ctx, []string{"a"}, "0", ScorePatch{IsHidden: proto.Bool(false)}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "", []string{"b"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "", []string{"b"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("0", documents[0].Id) @@ -348,7 +348,7 @@ func (suite *baseTestSuite) TestDocument() { func (suite *baseTestSuite) TestSubsetDocument() { ts := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) ctx := context.Background() - err := suite.AddDocuments(ctx, "a", "a", []Document{ + err := suite.AddScores(ctx, "a", "a", []Score{ { Id: "1", Score: 1, @@ -369,7 +369,7 @@ func (suite *baseTestSuite) TestSubsetDocument() { }, }) suite.NoError(err) - err = suite.AddDocuments(ctx, "b", "", []Document{ + err = suite.AddScores(ctx, "b", "", []Score{ { Id: "4", Score: 4, @@ -392,49 +392,49 @@ func (suite *baseTestSuite) TestSubsetDocument() { suite.NoError(err) // search documents - documents, err := suite.SearchDocuments(ctx, "a", "a", []string{"b"}, 0, -1) + documents, err := suite.SearchScores(ctx, "a", "a", []string{"b"}, 0, -1) suite.NoError(err) - suite.Equal([]Document{ + suite.Equal([]Score{ {Id: "3", Score: 3, Categories: []string{"b"}, Timestamp: ts}, {Id: "2", Score: 2, Categories: []string{"b", "c"}, Timestamp: ts}, {Id: "1", Score: 1, Categories: []string{"a", "b"}, Timestamp: ts}, }, documents) // update categories - err = suite.UpdateDocuments(ctx, []string{"a", "b"}, "2", DocumentPatch{Categories: []string{"b", "s"}}) + err = suite.UpdateScores(ctx, []string{"a", "b"}, "2", ScorePatch{Categories: []string{"b", "s"}}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "a", []string{"s"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "a", []string{"s"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("2", documents[0].Id) - documents, err = suite.SearchDocuments(ctx, "b", "", []string{"s"}, 0, 1) + documents, err = suite.SearchScores(ctx, "b", "", []string{"s"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("2", documents[0].Id) // delete by value - err = suite.DeleteDocuments(ctx, []string{"a", "b"}, DocumentCondition{Id: proto.String("3")}) + err = suite.DeleteScores(ctx, []string{"a", "b"}, ScoreCondition{Id: proto.String("3")}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "a", []string{"b"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "a", []string{"b"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("2", documents[0].Id) - documents, err = suite.SearchDocuments(ctx, "b", "", []string{"b"}, 0, 1) + documents, err = suite.SearchScores(ctx, "b", "", []string{"b"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("2", documents[0].Id) // delete in subset - err = suite.DeleteDocuments(ctx, []string{"a", "b"}, DocumentCondition{ + err = suite.DeleteScores(ctx, []string{"a", "b"}, ScoreCondition{ Subset: proto.String("a"), Id: proto.String("2"), }) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, "a", "a", []string{"b"}, 0, 1) + documents, err = suite.SearchScores(ctx, "a", "a", []string{"b"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("1", documents[0].Id) - documents, err = suite.SearchDocuments(ctx, "b", "", []string{"b"}, 0, 1) + documents, err = suite.SearchScores(ctx, "b", "", []string{"b"}, 0, 1) suite.NoError(err) suite.Len(documents, 1) suite.Equal("2", documents[0].Id) @@ -491,22 +491,22 @@ func primeFactor(n int) []int { } func benchmark(b *testing.B, database Database) { - b.Run("AddDocuments", func(b *testing.B) { + b.Run("AddScores", func(b *testing.B) { benchmarkAddDocuments(b, database) }) - b.Run("SearchDocuments", func(b *testing.B) { + b.Run("SearchScores", func(b *testing.B) { benchmarkSearchDocuments(b, database) }) - b.Run("UpdateDocuments", func(b *testing.B) { + b.Run("UpdateScores", func(b *testing.B) { benchmarkUpdateDocuments(b, database) }) } func benchmarkAddDocuments(b *testing.B, database Database) { ctx := context.Background() - var documents []Document + var documents []Score for i := 1; i <= b.N; i++ { - documents = append(documents, Document{ + documents = append(documents, Score{ Id: strconv.Itoa(i), Score: float64(-i), Categories: lo.Map(primeFactor(i), func(n, _ int) string { return strconv.Itoa(n) }), @@ -514,23 +514,23 @@ func benchmarkAddDocuments(b *testing.B, database Database) { }) } b.ResetTimer() - err := database.AddDocuments(ctx, "a", "", documents) + err := database.AddScores(ctx, "a", "", documents) assert.NoError(b, err) } func benchmarkSearchDocuments(b *testing.B, database Database) { // insert data ctx := context.Background() - var documents []Document + var documents []Score for i := 1; i <= benchmarkDataSize; i++ { - documents = append(documents, Document{ + documents = append(documents, Score{ Id: strconv.Itoa(i), Score: float64(-i), Categories: lo.Map(primeFactor(i), func(n, _ int) string { return strconv.Itoa(n) }), Timestamp: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), }) } - err := database.AddDocuments(ctx, "a", "", documents) + err := database.AddScores(ctx, "a", "", documents) assert.NoError(b, err) // search data b.ResetTimer() @@ -538,7 +538,7 @@ func benchmarkSearchDocuments(b *testing.B, database Database) { // select a random prime p := primeTable[rand.Intn(len(primeTable))] // search documents - r, err := database.SearchDocuments(ctx, "a", "", []string{strconv.Itoa(p)}, 0, 10) + r, err := database.SearchScores(ctx, "a", "", []string{strconv.Itoa(p)}, 0, 10) assert.NoError(b, err) assert.NotEmpty(b, r) } @@ -551,7 +551,7 @@ func benchmarkUpdateDocuments(b *testing.B, database Database) { // select a random number n := rand.Intn(benchmarkDataSize) + 1 // update documents - err := database.UpdateDocuments(ctx, []string{"a"}, strconv.Itoa(n), DocumentPatch{ + err := database.UpdateScores(ctx, []string{"a"}, strconv.Itoa(n), ScorePatch{ Score: proto.Float64(float64(n)), }) assert.NoError(b, err) diff --git a/storage/cache/mongodb.go b/storage/cache/mongodb.go index cfbc8195b..54dfb831f 100644 --- a/storage/cache/mongodb.go +++ b/storage/cache/mongodb.go @@ -317,7 +317,7 @@ func (m MongoDB) Remain(ctx context.Context, name string) (int64, error) { }) } -func (m MongoDB) AddDocuments(ctx context.Context, collection, subset string, documents []Document) error { +func (m MongoDB) AddScores(ctx context.Context, collection, subset string, documents []Score) error { if len(documents) == 0 { return nil } @@ -341,7 +341,7 @@ func (m MongoDB) AddDocuments(ctx context.Context, collection, subset string, do return errors.Trace(err) } -func (m MongoDB) SearchDocuments(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Document, error) { +func (m MongoDB) SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error) { if len(query) == 0 { return nil, nil } @@ -358,9 +358,9 @@ func (m MongoDB) SearchDocuments(ctx context.Context, collection, subset string, if err != nil { return nil, errors.Trace(err) } - documents := make([]Document, 0) + documents := make([]Score, 0) for cur.Next(ctx) { - var document Document + var document Score if err = cur.Decode(&document); err != nil { return nil, errors.Trace(err) } @@ -369,7 +369,7 @@ func (m MongoDB) SearchDocuments(ctx context.Context, collection, subset string, return documents, nil } -func (m MongoDB) UpdateDocuments(ctx context.Context, collections []string, id string, patch DocumentPatch) error { +func (m MongoDB) UpdateScores(ctx context.Context, collections []string, id string, patch ScorePatch) error { if len(collections) == 0 { return nil } @@ -393,7 +393,7 @@ func (m MongoDB) UpdateDocuments(ctx context.Context, collections []string, id s return errors.Trace(err) } -func (m MongoDB) DeleteDocuments(ctx context.Context, collections []string, condition DocumentCondition) error { +func (m MongoDB) DeleteScores(ctx context.Context, collections []string, condition ScoreCondition) error { if err := condition.Check(); err != nil { return errors.Trace(err) } diff --git a/storage/cache/no_database.go b/storage/cache/no_database.go index c00f9b565..3ca70e932 100644 --- a/storage/cache/no_database.go +++ b/storage/cache/no_database.go @@ -90,19 +90,19 @@ func (NoDatabase) Remain(_ context.Context, _ string) (int64, error) { return 0, ErrNoDatabase } -func (NoDatabase) AddDocuments(_ context.Context, _, _ string, _ []Document) error { +func (NoDatabase) AddScores(_ context.Context, _, _ string, _ []Score) error { return ErrNoDatabase } -func (NoDatabase) SearchDocuments(_ context.Context, _, _ string, _ []string, _, _ int) ([]Document, error) { +func (NoDatabase) SearchScores(_ context.Context, _, _ string, _ []string, _, _ int) ([]Score, error) { return nil, ErrNoDatabase } -func (NoDatabase) UpdateDocuments(_ context.Context, _ []string, _ string, _ DocumentPatch) error { +func (NoDatabase) UpdateScores(_ context.Context, _ []string, _ string, _ ScorePatch) error { return ErrNoDatabase } -func (NoDatabase) DeleteDocuments(_ context.Context, _ []string, _ DocumentCondition) error { +func (NoDatabase) DeleteScores(_ context.Context, _ []string, _ ScoreCondition) error { return ErrNoDatabase } diff --git a/storage/cache/no_database_test.go b/storage/cache/no_database_test.go index 150c9b8fe..b49127754 100644 --- a/storage/cache/no_database_test.go +++ b/storage/cache/no_database_test.go @@ -62,13 +62,13 @@ func TestNoDatabase(t *testing.T) { _, err = database.Remain(ctx, "") assert.ErrorIs(t, err, ErrNoDatabase) - err = database.AddDocuments(ctx, "", "", nil) + err = database.AddScores(ctx, "", "", nil) assert.ErrorIs(t, err, ErrNoDatabase) - _, err = database.SearchDocuments(ctx, "", "", nil, 0, 0) + _, err = database.SearchScores(ctx, "", "", nil, 0, 0) assert.ErrorIs(t, err, ErrNoDatabase) - err = database.UpdateDocuments(ctx, nil, "", DocumentPatch{}) + err = database.UpdateScores(ctx, nil, "", ScorePatch{}) assert.ErrorIs(t, err, ErrNoDatabase) - err = database.DeleteDocuments(ctx, nil, DocumentCondition{}) + err = database.DeleteScores(ctx, nil, ScoreCondition{}) assert.ErrorIs(t, err, ErrNoDatabase) err = database.AddTimeSeriesPoints(ctx, nil) diff --git a/storage/cache/redis.go b/storage/cache/redis.go index 409f7b086..18c74c56f 100644 --- a/storage/cache/redis.go +++ b/storage/cache/redis.go @@ -237,7 +237,7 @@ func (r *Redis) documentKey(collection, subset, value string) string { return r.DocumentTable() + ":" + collection + ":" + subset + ":" + value } -func (r *Redis) AddDocuments(ctx context.Context, collection, subset string, documents []Document) error { +func (r *Redis) AddScores(ctx context.Context, collection, subset string, documents []Score) error { p := r.client.Pipeline() for _, document := range documents { p.HSet(ctx, r.documentKey(collection, subset, document.Id), @@ -253,7 +253,7 @@ func (r *Redis) AddDocuments(ctx context.Context, collection, subset string, doc return errors.Trace(err) } -func (r *Redis) SearchDocuments(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Document, error) { +func (r *Redis) SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error) { if len(query) == 0 { return nil, nil } @@ -278,9 +278,9 @@ func (r *Redis) SearchDocuments(ctx context.Context, collection, subset string, if err != nil { return nil, errors.Trace(err) } - documents := make([]Document, 0, len(result.Docs)) + documents := make([]Score, 0, len(result.Docs)) for _, doc := range result.Docs { - var document Document + var document Score document.Id = doc.Fields["id"] score, err := strconv.ParseFloat(doc.Fields["score"], 64) if err != nil { @@ -307,7 +307,7 @@ func (r *Redis) SearchDocuments(ctx context.Context, collection, subset string, return documents, nil } -func (r *Redis) UpdateDocuments(ctx context.Context, collections []string, id string, patch DocumentPatch) error { +func (r *Redis) UpdateScores(ctx context.Context, collections []string, id string, patch ScorePatch) error { if len(collections) == 0 { return nil } @@ -359,7 +359,7 @@ func (r *Redis) UpdateDocuments(ctx context.Context, collections []string, id st return nil } -func (r *Redis) DeleteDocuments(ctx context.Context, collections []string, condition DocumentCondition) error { +func (r *Redis) DeleteScores(ctx context.Context, collections []string, condition ScoreCondition) error { if err := condition.Check(); err != nil { return errors.Trace(err) } diff --git a/storage/cache/redis_test.go b/storage/cache/redis_test.go index 10b03ae05..a63ab6949 100644 --- a/storage/cache/redis_test.go +++ b/storage/cache/redis_test.go @@ -67,29 +67,29 @@ func (suite *RedisTestSuite) TestEscapeCharacters() { collection := fmt.Sprintf("a%s1", c) subset := fmt.Sprintf("b%s2", c) id := fmt.Sprintf("c%s3", c) - err := suite.AddDocuments(ctx, collection, subset, []Document{{ + err := suite.AddScores(ctx, collection, subset, []Score{{ Id: id, Score: math.MaxFloat64, Categories: []string{"a", "b"}, Timestamp: ts, }}) suite.NoError(err) - documents, err := suite.SearchDocuments(ctx, collection, subset, []string{"b"}, 0, -1) + documents, err := suite.SearchScores(ctx, collection, subset, []string{"b"}, 0, -1) suite.NoError(err) - suite.Equal([]Document{{Id: id, Score: math.MaxFloat64, Categories: []string{"a", "b"}, Timestamp: ts}}, documents) + suite.Equal([]Score{{Id: id, Score: math.MaxFloat64, Categories: []string{"a", "b"}, Timestamp: ts}}, documents) - err = suite.UpdateDocuments(ctx, []string{collection}, id, DocumentPatch{Score: proto.Float64(1)}) + err = suite.UpdateScores(ctx, []string{collection}, id, ScorePatch{Score: proto.Float64(1)}) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, collection, subset, []string{"b"}, 0, -1) + documents, err = suite.SearchScores(ctx, collection, subset, []string{"b"}, 0, -1) suite.NoError(err) - suite.Equal([]Document{{Id: id, Score: 1, Categories: []string{"a", "b"}, Timestamp: ts}}, documents) + suite.Equal([]Score{{Id: id, Score: 1, Categories: []string{"a", "b"}, Timestamp: ts}}, documents) - err = suite.DeleteDocuments(ctx, []string{collection}, DocumentCondition{ + err = suite.DeleteScores(ctx, []string{collection}, ScoreCondition{ Subset: proto.String(subset), Id: proto.String(id), }) suite.NoError(err) - documents, err = suite.SearchDocuments(ctx, collection, subset, []string{"b"}, 0, -1) + documents, err = suite.SearchScores(ctx, collection, subset, []string{"b"}, 0, -1) suite.NoError(err) suite.Empty(documents) }) diff --git a/storage/cache/sql.go b/storage/cache/sql.go index 274291dcc..bef865d75 100644 --- a/storage/cache/sql.go +++ b/storage/cache/sql.go @@ -391,11 +391,11 @@ func (db *SQLDatabase) Remain(ctx context.Context, name string) (count int64, er return } -func (db *SQLDatabase) AddDocuments(ctx context.Context, collection, subset string, documents []Document) error { +func (db *SQLDatabase) AddScores(ctx context.Context, collection, subset string, documents []Score) error { var rows any switch db.driver { case Postgres: - rows = lo.Map(documents, func(document Document, _ int) PostgresDocument { + rows = lo.Map(documents, func(document Score, _ int) PostgresDocument { return PostgresDocument{ Collection: collection, Subset: subset, @@ -407,7 +407,7 @@ func (db *SQLDatabase) AddDocuments(ctx context.Context, collection, subset stri } }) case SQLite, MySQL: - rows = lo.Map(documents, func(document Document, _ int) SQLDocument { + rows = lo.Map(documents, func(document Score, _ int) SQLDocument { return SQLDocument{ Collection: collection, Subset: subset, @@ -426,7 +426,7 @@ func (db *SQLDatabase) AddDocuments(ctx context.Context, collection, subset stri return nil } -func (db *SQLDatabase) SearchDocuments(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Document, error) { +func (db *SQLDatabase) SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error) { if len(query) == 0 { return nil, nil } @@ -451,7 +451,7 @@ func (db *SQLDatabase) SearchDocuments(ctx context.Context, collection, subset s if err != nil { return nil, errors.Trace(err) } - documents := make([]Document, 0, 10) + documents := make([]Score, 0, 10) for rows.Next() { switch db.driver { case Postgres: @@ -459,14 +459,14 @@ func (db *SQLDatabase) SearchDocuments(ctx context.Context, collection, subset s if err = rows.Scan(&document.Id, &document.Score, &document.Categories, &document.Timestamp); err != nil { return nil, errors.Trace(err) } - documents = append(documents, Document{ + documents = append(documents, Score{ Id: document.Id, Score: document.Score, Categories: document.Categories, Timestamp: document.Timestamp, }) case SQLite, MySQL: - var document Document + var document Score if err = db.gormDB.ScanRows(rows, &document); err != nil { return nil, errors.Trace(err) } @@ -477,7 +477,7 @@ func (db *SQLDatabase) SearchDocuments(ctx context.Context, collection, subset s return documents, nil } -func (db *SQLDatabase) UpdateDocuments(ctx context.Context, collections []string, id string, patch DocumentPatch) error { +func (db *SQLDatabase) UpdateScores(ctx context.Context, collections []string, id string, patch ScorePatch) error { if len(collections) == 0 { return nil } @@ -506,7 +506,7 @@ func (db *SQLDatabase) UpdateDocuments(ctx context.Context, collections []string return tx.Error } -func (db *SQLDatabase) DeleteDocuments(ctx context.Context, collections []string, condition DocumentCondition) error { +func (db *SQLDatabase) DeleteScores(ctx context.Context, collections []string, condition ScoreCondition) error { if err := condition.Check(); err != nil { return errors.Trace(err) } diff --git a/worker/worker.go b/worker/worker.go index 88855ab5d..f6c92f501 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -624,7 +624,7 @@ func (w *Worker) Recommend(users []data.User) { scores := make(map[string]float64) for _, itemId := range positiveItems { // load similar items - similarItems, err := w.CacheClient.SearchDocuments(ctx, cache.ItemNeighbors, itemId, []string{category}, 0, w.Config.Recommend.CacheSize) + similarItems, err := w.CacheClient.SearchScores(ctx, cache.ItemNeighbors, itemId, []string{category}, 0, w.Config.Recommend.CacheSize) if err != nil { log.Logger().Error("failed to load similar items", zap.Error(err)) return errors.Trace(err) @@ -662,7 +662,7 @@ func (w *Worker) Recommend(users []data.User) { localStartTime := time.Now() scores := make(map[string]float64) // load similar users - similarUsers, err := w.CacheClient.SearchDocuments(ctx, cache.UserNeighbors, userId, []string{""}, 0, w.Config.Recommend.CacheSize) + similarUsers, err := w.CacheClient.SearchScores(ctx, cache.UserNeighbors, userId, []string{""}, 0, w.Config.Recommend.CacheSize) if err != nil { log.Logger().Error("failed to load similar users", zap.Error(err)) return errors.Trace(err) @@ -715,7 +715,7 @@ func (w *Worker) Recommend(users []data.User) { if w.Config.Recommend.Offline.EnableLatestRecommend { localStartTime := time.Now() for _, category := range append([]string{""}, itemCategories...) { - latestItems, err := w.CacheClient.SearchDocuments(ctx, cache.LatestItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) + latestItems, err := w.CacheClient.SearchScores(ctx, cache.LatestItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) if err != nil { log.Logger().Error("failed to load latest items", zap.Error(err)) return errors.Trace(err) @@ -735,7 +735,7 @@ func (w *Worker) Recommend(users []data.User) { if w.Config.Recommend.Offline.EnablePopularRecommend { localStartTime := time.Now() for _, category := range append([]string{""}, itemCategories...) { - popularItems, err := w.CacheClient.SearchDocuments(ctx, cache.PopularItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) + popularItems, err := w.CacheClient.SearchScores(ctx, cache.PopularItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) if err != nil { log.Logger().Error("failed to load popular items", zap.Error(err)) return errors.Trace(err) @@ -756,7 +756,7 @@ func (w *Worker) Recommend(users []data.User) { // 2. If collaborative filtering model is available, use it to rank items. // 3. Otherwise, merge all recommenders' results randomly. ctrUsed := false - results := make(map[string][]cache.Document) + results := make(map[string][]cache.Score) for category, catCandidates := range candidates { if w.Config.Recommend.Offline.EnableClickThroughPrediction && w.rankers[workerId] != nil && !w.rankers[workerId].Invalid() { results[category], err = w.rankByClickTroughRate(&user, catCandidates, itemCache, w.rankers[workerId]) @@ -794,13 +794,13 @@ func (w *Worker) Recommend(users []data.User) { log.Logger().Error("failed to explore latest and popular items", zap.Error(err)) return errors.Trace(err) } - aggregator.Add(category, lo.Map(scores, func(document cache.Document, _ int) string { + aggregator.Add(category, lo.Map(scores, func(document cache.Score, _ int) string { return document.Id - }), lo.Map(scores, func(document cache.Document, _ int) float64 { + }), lo.Map(scores, func(document cache.Score, _ int) float64 { return document.Score })) } - if err = w.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, userId, aggregator.ToSlice()); err != nil { + if err = w.CacheClient.AddScores(ctx, cache.OfflineRecommend, userId, aggregator.ToSlice()); err != nil { log.Logger().Error("failed to cache recommendation", zap.Error(err)) return errors.Trace(err) } @@ -860,11 +860,11 @@ func (w *Worker) collaborativeRecommendBruteForce(userId string, itemCategories recommend[category] = recommendItems aggregator.Add(category, recommendItems, recommendScores) } - if err := w.CacheClient.AddDocuments(ctx, cache.CollaborativeRecommend, userId, aggregator.ToSlice()); err != nil { + if err := w.CacheClient.AddScores(ctx, cache.CollaborativeRecommend, userId, aggregator.ToSlice()); err != nil { log.Logger().Error("failed to cache collaborative filtering recommendation result", zap.String("user_id", userId), zap.Error(err)) return nil, 0, errors.Trace(err) } - if err := w.CacheClient.DeleteDocuments(ctx, []string{cache.CollaborativeRecommend}, cache.DocumentCondition{Before: &localStartTime}); err != nil { + if err := w.CacheClient.DeleteScores(ctx, []string{cache.CollaborativeRecommend}, cache.ScoreCondition{Before: &localStartTime}); err != nil { log.Logger().Error("failed to delete stale collaborative filtering recommendation result", zap.String("user_id", userId), zap.Error(err)) return nil, 0, errors.Trace(err) } @@ -893,18 +893,18 @@ func (w *Worker) collaborativeRecommendHNSW(rankingIndex *search.HNSW, userId st recommend[category] = recommendItems aggregator.Add(category, recommendItems, recommendScores) } - if err := w.CacheClient.AddDocuments(ctx, cache.CollaborativeRecommend, userId, aggregator.ToSlice()); err != nil { + if err := w.CacheClient.AddScores(ctx, cache.CollaborativeRecommend, userId, aggregator.ToSlice()); err != nil { log.Logger().Error("failed to cache collaborative filtering recommendation result", zap.String("user_id", userId), zap.Error(err)) return nil, 0, errors.Trace(err) } - if err := w.CacheClient.DeleteDocuments(ctx, []string{cache.CollaborativeRecommend}, cache.DocumentCondition{Before: &localStartTime}); err != nil { + if err := w.CacheClient.DeleteScores(ctx, []string{cache.CollaborativeRecommend}, cache.ScoreCondition{Before: &localStartTime}); err != nil { log.Logger().Error("failed to delete stale collaborative filtering recommendation result", zap.String("user_id", userId), zap.Error(err)) return nil, 0, errors.Trace(err) } return recommend, time.Since(localStartTime), nil } -func (w *Worker) rankByCollaborativeFiltering(userId string, candidates [][]string) ([]cache.Document, error) { +func (w *Worker) rankByCollaborativeFiltering(userId string, candidates [][]string) ([]cache.Score, error) { // concat candidates memo := mapset.NewSet[string]() var itemIds []string @@ -917,9 +917,9 @@ func (w *Worker) rankByCollaborativeFiltering(userId string, candidates [][]stri } } // rank by collaborative filtering - topItems := make([]cache.Document, 0, len(candidates)) + topItems := make([]cache.Score, 0, len(candidates)) for _, itemId := range itemIds { - topItems = append(topItems, cache.Document{ + topItems = append(topItems, cache.Score{ Id: itemId, Score: float64(w.RankingModel.Predict(userId, itemId)), }) @@ -929,7 +929,7 @@ func (w *Worker) rankByCollaborativeFiltering(userId string, candidates [][]stri } // rankByClickTroughRate ranks items by predicted click-through-rate. -func (w *Worker) rankByClickTroughRate(user *data.User, candidates [][]string, itemCache *ItemCache, predictor click.FactorizationMachine) ([]cache.Document, error) { +func (w *Worker) rankByClickTroughRate(user *data.User, candidates [][]string, itemCache *ItemCache, predictor click.FactorizationMachine) ([]cache.Score, error) { // concat candidates memo := mapset.NewSet[string]() var itemIds []string @@ -951,7 +951,7 @@ func (w *Worker) rankByClickTroughRate(user *data.User, candidates [][]string, i } } // rank by CTR - topItems := make([]cache.Document, 0, len(items)) + topItems := make([]cache.Score, 0, len(items)) if batchPredictor, ok := predictor.(click.BatchInference); ok { inputs := make([]lo.Tuple4[string, string, []click.Feature, []click.Feature], len(items)) for i, item := range items { @@ -962,14 +962,14 @@ func (w *Worker) rankByClickTroughRate(user *data.User, candidates [][]string, i } output := batchPredictor.BatchPredict(inputs) for i, score := range output { - topItems = append(topItems, cache.Document{ + topItems = append(topItems, cache.Score{ Id: items[i].ItemId, Score: float64(score), }) } } else { for _, item := range items { - topItems = append(topItems, cache.Document{ + topItems = append(topItems, cache.Score{ Id: item.ItemId, Score: float64(predictor.Predict(user.UserId, item.ItemId, click.ConvertLabelsToFeatures(user.Labels), click.ConvertLabelsToFeatures(item.Labels))), }) @@ -979,10 +979,10 @@ func (w *Worker) rankByClickTroughRate(user *data.User, candidates [][]string, i return topItems, nil } -func (w *Worker) mergeAndShuffle(candidates [][]string) []cache.Document { +func (w *Worker) mergeAndShuffle(candidates [][]string) []cache.Score { memo := mapset.NewSet[string]() pos := make([]int, len(candidates)) - var recommend []cache.Document + var recommend []cache.Score for { // filter out ended slice var src []int @@ -1000,13 +1000,13 @@ func (w *Worker) mergeAndShuffle(candidates [][]string) []cache.Document { pos[j]++ if !memo.Contains(candidateId) { memo.Add(candidateId) - recommend = append(recommend, cache.Document{Score: math.Exp(float64(-len(recommend))), Id: candidateId}) + recommend = append(recommend, cache.Score{Score: math.Exp(float64(-len(recommend))), Id: candidateId}) } } return recommend } -func (w *Worker) exploreRecommend(exploitRecommend []cache.Document, excludeSet mapset.Set[string], category string) ([]cache.Document, error) { +func (w *Worker) exploreRecommend(exploitRecommend []cache.Score, excludeSet mapset.Set[string], category string) ([]cache.Score, error) { var localExcludeSet mapset.Set[string] ctx := context.Background() if w.Config.Recommend.Replacement.EnableReplacement { @@ -1024,24 +1024,24 @@ func (w *Worker) exploreRecommend(exploitRecommend []cache.Document, excludeSet exploreLatestThreshold += threshold } // load popular items - popularItems, err := w.CacheClient.SearchDocuments(ctx, cache.PopularItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) + popularItems, err := w.CacheClient.SearchScores(ctx, cache.PopularItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) if err != nil { return nil, errors.Trace(err) } // load the latest items - latestItems, err := w.CacheClient.SearchDocuments(ctx, cache.LatestItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) + latestItems, err := w.CacheClient.SearchScores(ctx, cache.LatestItems, "", []string{category}, 0, w.Config.Recommend.CacheSize) if err != nil { return nil, errors.Trace(err) } // explore recommendation - var exploreRecommend []cache.Document + var exploreRecommend []cache.Score score := 1.0 if len(exploitRecommend) > 0 { score += exploitRecommend[0].Score } for range exploitRecommend { dice := w.randGenerator.Float64() - var recommendItem cache.Document + var recommendItem cache.Score if dice < explorePopularThreshold && len(popularItems) > 0 { score -= 1e-5 recommendItem.Id = popularItems[0].Id @@ -1084,8 +1084,8 @@ func (w *Worker) checkUserActiveTime(ctx context.Context, userId string) bool { return true } // remove recommend cache for inactive users - if err := w.CacheClient.DeleteDocuments(ctx, []string{cache.OfflineRecommend, cache.CollaborativeRecommend}, - cache.DocumentCondition{Subset: proto.String(userId)}); err != nil { + if err := w.CacheClient.DeleteScores(ctx, []string{cache.OfflineRecommend, cache.CollaborativeRecommend}, + cache.ScoreCondition{Subset: proto.String(userId)}); err != nil { log.Logger().Error("failed to delete recommend cache", zap.String("user_id", userId), zap.Error(err)) } return false @@ -1104,7 +1104,7 @@ func (w *Worker) checkRecommendCacheTimeout(ctx context.Context, userId string, ) // check cache for _, category := range append([]string{""}, categories...) { - items, err := w.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, userId, []string{category}, 0, -1) + items, err := w.CacheClient.SearchScores(ctx, cache.OfflineRecommend, userId, []string{category}, 0, -1) if err != nil { log.Logger().Error("failed to load offline recommendation", zap.String("user_id", userId), zap.Error(err)) return true @@ -1213,14 +1213,14 @@ func (w *Worker) pullUsers(peers []string, me string) ([]data.User, error) { } // replacement inserts historical items back to recommendation. -func (w *Worker) replacement(recommend map[string][]cache.Document, user *data.User, feedbacks []data.Feedback, itemCache *ItemCache) (map[string][]cache.Document, error) { +func (w *Worker) replacement(recommend map[string][]cache.Score, user *data.User, feedbacks []data.Feedback, itemCache *ItemCache) (map[string][]cache.Score, error) { upperBounds := make(map[string]float64) lowerBounds := make(map[string]float64) - newRecommend := make(map[string][]cache.Document) + newRecommend := make(map[string][]cache.Score) for category, scores := range recommend { // find minimal score if len(scores) > 0 { - s := lo.Map(scores, func(score cache.Document, _ int) float64 { + s := lo.Map(scores, func(score cache.Score, _ int) float64 { return score.Score }) upperBounds[category] = funk.MaxFloat64(s) @@ -1281,7 +1281,7 @@ func (w *Worker) replacement(recommend map[string][]cache.Document, user *data.U } score += lowerBound } - newRecommend[category] = append(newRecommend[category], cache.Document{Id: itemId, Score: score}) + newRecommend[category] = append(newRecommend[category], cache.Score{Id: itemId, Score: score}) } } else { log.Logger().Warn("item doesn't exists in database", zap.String("item_id", itemId)) diff --git a/worker/worker_test.go b/worker/worker_test.go index 0a9eae460..5a2babac0 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -121,7 +121,7 @@ func (suite *WorkerTestSuite) TestCheckRecommendCacheTimeout() { // empty cache suite.True(suite.checkRecommendCacheTimeout(ctx, "0", nil)) - err := suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{{Id: "0", Score: 0, Categories: []string{""}}}) + err := suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{{Id: "0", Score: 0, Categories: []string{""}}}) suite.NoError(err) // digest mismatch @@ -138,7 +138,7 @@ func (suite *WorkerTestSuite) TestCheckRecommendCacheTimeout() { err = suite.CacheClient.Set(ctx, cache.Time(cache.Key(cache.LastUpdateUserRecommendTime, "0"), time.Now().Add(time.Hour*100))) suite.NoError(err) suite.False(suite.checkRecommendCacheTimeout(ctx, "0", nil)) - err = suite.CacheClient.DeleteDocuments(ctx, []string{cache.OfflineRecommend}, cache.DocumentCondition{Subset: proto.String("0")}) + err = suite.CacheClient.DeleteScores(ctx, []string{cache.OfflineRecommend}, cache.ScoreCondition{Subset: proto.String("0")}) suite.NoError(err) suite.True(suite.checkRecommendCacheTimeout(ctx, "0", nil)) } @@ -239,17 +239,17 @@ func (suite *WorkerTestSuite) TestRecommendMatrixFactorizationBruteForce() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "3", Score: 3, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "2", Score: 2, Categories: []string{""}, Timestamp: recommendTime}, {Id: "1", Score: 1, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "0", Score: 0, Categories: []string{""}, Timestamp: recommendTime}, }, recommends) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "3", Score: 3, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "1", Score: 1, Categories: []string{"", "*"}, Timestamp: recommendTime}, }, recommends) @@ -292,9 +292,9 @@ func (suite *WorkerTestSuite) TestRecommendMatrixFactorizationHNSW() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "3", Score: 3, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "2", Score: 2, Categories: []string{""}, Timestamp: recommendTime}, {Id: "1", Score: 1, Categories: []string{"", "*"}, Timestamp: recommendTime}, @@ -316,20 +316,20 @@ func (suite *WorkerTestSuite) TestRecommendItemBased() { suite.NoError(err) // insert similar items - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "21", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "21", []cache.Score{ {Id: "22", Score: 100000, Categories: []string{"", "*"}}, {Id: "25", Score: 1000000, Categories: []string{""}}, {Id: "29", Score: 1, Categories: []string{""}}, }) suite.NoError(err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "22", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "22", []cache.Score{ {Id: "23", Score: 100000, Categories: []string{"", "*"}}, {Id: "25", Score: 1000000, Categories: []string{""}}, {Id: "28", Score: 1, Categories: []string{"", "*"}}, {Id: "29", Score: 1, Categories: []string{""}}, }) suite.NoError(err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "23", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "23", []cache.Score{ {Id: "24", Score: 100000, Categories: []string{"", "*"}}, {Id: "25", Score: 1000000, Categories: []string{""}}, {Id: "27", Score: 1, Categories: []string{""}}, @@ -337,7 +337,7 @@ func (suite *WorkerTestSuite) TestRecommendItemBased() { {Id: "29", Score: 1, Categories: []string{""}}, }) suite.NoError(err) - err = suite.CacheClient.AddDocuments(ctx, cache.ItemNeighbors, "24", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.ItemNeighbors, "24", []cache.Score{ {Id: "21", Score: 100000, Categories: []string{""}}, {Id: "25", Score: 1000000, Categories: []string{""}}, {Id: "26", Score: 1, Categories: []string{"", "*"}}, @@ -363,16 +363,16 @@ func (suite *WorkerTestSuite) TestRecommendItemBased() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "29", Score: 29, Categories: []string{""}, Timestamp: recommendTime}, {Id: "28", Score: 28, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "27", Score: 27, Categories: []string{""}, Timestamp: recommendTime}, }, recommends) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, 3) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "28", Score: 28, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "26", Score: 26, Categories: []string{"", "*"}, Timestamp: recommendTime}, }, recommends) @@ -383,7 +383,7 @@ func (suite *WorkerTestSuite) TestRecommendUserBased() { suite.Config.Recommend.Offline.EnableColRecommend = false suite.Config.Recommend.Offline.EnableUserBasedRecommend = true // insert similar users - err := suite.CacheClient.AddDocuments(ctx, cache.UserNeighbors, "0", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.UserNeighbors, "0", []cache.Score{ {Id: "1", Score: 2, Categories: []string{""}}, {Id: "2", Score: 1.5, Categories: []string{""}}, {Id: "3", Score: 1, Categories: []string{""}}, @@ -422,16 +422,16 @@ func (suite *WorkerTestSuite) TestRecommendUserBased() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "48", Score: 48, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "13", Score: 13, Categories: []string{""}, Timestamp: recommendTime}, {Id: "12", Score: 12, Categories: []string{"", "*"}, Timestamp: recommendTime}, }, recommends) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, 3) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "48", Score: 48, Categories: []string{"", "*"}, Timestamp: recommendTime}, {Id: "12", Score: 12, Categories: []string{"", "*"}, Timestamp: recommendTime}, }, recommends) @@ -442,7 +442,7 @@ func (suite *WorkerTestSuite) TestRecommendPopular() { suite.Config.Recommend.Offline.EnableColRecommend = false suite.Config.Recommend.Offline.EnablePopularRecommend = true // insert popular items - err := suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.PopularItems, "", []cache.Score{ {Id: "11", Score: 11, Categories: []string{""}}, {Id: "10", Score: 10, Categories: []string{""}}, {Id: "9", Score: 9, Categories: []string{""}}, @@ -469,16 +469,16 @@ func (suite *WorkerTestSuite) TestRecommendPopular() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "10", Score: 10, Categories: []string{""}, Timestamp: recommendTime}, {Id: "9", Score: 9, Categories: []string{""}, Timestamp: recommendTime}, {Id: "8", Score: 8, Categories: []string{""}, Timestamp: recommendTime}, }, recommends) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "20", Score: 20, Categories: []string{"*"}, Timestamp: recommendTime}, {Id: "19", Score: 19, Categories: []string{"*"}, Timestamp: recommendTime}, {Id: "18", Score: 18, Categories: []string{"*"}, Timestamp: recommendTime}, @@ -491,7 +491,7 @@ func (suite *WorkerTestSuite) TestRecommendLatest() { suite.Config.Recommend.Offline.EnableColRecommend = false suite.Config.Recommend.Offline.EnableLatestRecommend = true // insert latest items - err := suite.CacheClient.AddDocuments(ctx, cache.LatestItems, "", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.LatestItems, "", []cache.Score{ {Id: "11", Score: 11, Categories: []string{""}}, {Id: "10", Score: 10, Categories: []string{""}}, {Id: "9", Score: 9, Categories: []string{""}}, @@ -518,16 +518,16 @@ func (suite *WorkerTestSuite) TestRecommendLatest() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "10", Score: 10, Categories: []string{""}, Timestamp: recommendTime}, {Id: "9", Score: 9, Categories: []string{""}, Timestamp: recommendTime}, {Id: "8", Score: 8, Categories: []string{""}, Timestamp: recommendTime}, }, recommends) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "20", Score: 20, Categories: []string{"*"}, Timestamp: recommendTime}, {Id: "19", Score: 19, Categories: []string{"*"}, Timestamp: recommendTime}, {Id: "18", Score: 18, Categories: []string{"*"}, Timestamp: recommendTime}, @@ -539,7 +539,7 @@ func (suite *WorkerTestSuite) TestRecommendColdStart() { suite.Config.Recommend.Offline.EnableColRecommend = true suite.Config.Recommend.Offline.EnableLatestRecommend = true // insert latest items - err := suite.CacheClient.AddDocuments(ctx, cache.LatestItems, "", []cache.Document{ + err := suite.CacheClient.AddScores(ctx, cache.LatestItems, "", []cache.Score{ {Id: "11", Score: 11, Categories: []string{""}}, {Id: "10", Score: 10, Categories: []string{""}}, {Id: "9", Score: 9, Categories: []string{""}}, @@ -564,40 +564,40 @@ func (suite *WorkerTestSuite) TestRecommendColdStart() { // ranking model not exist m := newMockMatrixFactorizationForRecommend(10, 100) suite.Recommend([]data.User{{UserId: "0"}}) - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, -1) suite.NoError(err) - suite.Equal([]string{"10", "9", "8"}, lo.Map(recommends, func(d cache.Document, _ int) string { return d.Id })) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) + suite.Equal([]string{"10", "9", "8"}, lo.Map(recommends, func(d cache.Score, _ int) string { return d.Id })) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{"*"}, 0, -1) suite.NoError(err) - suite.Equal([]string{"20", "19", "18"}, lo.Map(recommends, func(d cache.Document, _ int) string { return d.Id })) + suite.Equal([]string{"20", "19", "18"}, lo.Map(recommends, func(d cache.Score, _ int) string { return d.Id })) // user not predictable suite.RankingModel = m suite.Recommend([]data.User{{UserId: "100"}}) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "100", []string{""}, 0, -1) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "100", []string{""}, 0, -1) suite.NoError(err) - suite.Equal([]string{"10", "9", "8"}, lo.Map(recommends, func(d cache.Document, _ int) string { return d.Id })) - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "100", []string{"*"}, 0, -1) + suite.Equal([]string{"10", "9", "8"}, lo.Map(recommends, func(d cache.Score, _ int) string { return d.Id })) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "100", []string{"*"}, 0, -1) suite.NoError(err) - suite.Equal([]string{"20", "19", "18"}, lo.Map(recommends, func(d cache.Document, _ int) string { return d.Id })) + suite.Equal([]string{"20", "19", "18"}, lo.Map(recommends, func(d cache.Score, _ int) string { return d.Id })) } func (suite *WorkerTestSuite) TestMergeAndShuffle() { scores := suite.mergeAndShuffle([][]string{{"1", "2", "3"}, {"1", "3", "5"}}) - suite.ElementsMatch([]string{"1", "2", "3", "5"}, lo.Map(scores, func(d cache.Document, _ int) string { return d.Id })) + suite.ElementsMatch([]string{"1", "2", "3", "5"}, lo.Map(scores, func(d cache.Score, _ int) string { return d.Id })) } func (suite *WorkerTestSuite) TestExploreRecommend() { ctx := context.Background() suite.Config.Recommend.Offline.ExploreRecommend = map[string]float64{"popular": 0.3, "latest": 0.3} // insert popular items - err := suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", []cache.Document{{Id: "popular", Score: 0, Categories: []string{""}, Timestamp: time.Now()}}) + err := suite.CacheClient.AddScores(ctx, cache.PopularItems, "", []cache.Score{{Id: "popular", Score: 0, Categories: []string{""}, Timestamp: time.Now()}}) suite.NoError(err) // insert latest items - err = suite.CacheClient.AddDocuments(ctx, cache.LatestItems, "", []cache.Document{{Id: "latest", Score: 0, Categories: []string{""}, Timestamp: time.Now()}}) + err = suite.CacheClient.AddScores(ctx, cache.LatestItems, "", []cache.Score{{Id: "latest", Score: 0, Categories: []string{""}, Timestamp: time.Now()}}) suite.NoError(err) - recommend, err := suite.exploreRecommend([]cache.Document{ + recommend, err := suite.exploreRecommend([]cache.Score{ {Id: "8", Score: 8}, {Id: "7", Score: 7}, {Id: "6", Score: 6}, @@ -608,14 +608,14 @@ func (suite *WorkerTestSuite) TestExploreRecommend() { {Id: "1", Score: 1}, }, mapset.NewSet[string](), "") suite.NoError(err) - items := lo.Map(recommend, func(d cache.Document, _ int) string { return d.Id }) + items := lo.Map(recommend, func(d cache.Score, _ int) string { return d.Id }) suite.Contains(items, "latest") suite.Contains(items, "popular") items = funk.FilterString(items, func(item string) bool { return item != "latest" && item != "popular" }) suite.IsDecreasing(items) - scores := lo.Map(recommend, func(d cache.Document, _ int) float64 { return d.Score }) + scores := lo.Map(recommend, func(d cache.Score, _ int) float64 { return d.Score }) suite.IsDecreasing(scores) suite.Equal(8, len(recommend)) } @@ -872,10 +872,10 @@ func (suite *WorkerTestSuite) TestRankByCollaborativeFiltering() { suite.RankingModel = newMockMatrixFactorizationForRecommend(10, 10) result, err := suite.rankByCollaborativeFiltering("1", [][]string{{"1", "2", "3", "4", "5"}}) suite.NoError(err) - suite.Equal([]string{"5", "4", "3", "2", "1"}, lo.Map(result, func(d cache.Document, _ int) string { + suite.Equal([]string{"5", "4", "3", "2", "1"}, lo.Map(result, func(d cache.Score, _ int) string { return d.Id })) - suite.IsDecreasing(lo.Map(result, func(d cache.Document, _ int) float64 { + suite.IsDecreasing(lo.Map(result, func(d cache.Score, _ int) float64 { return d.Score })) } @@ -893,10 +893,10 @@ func (suite *WorkerTestSuite) TestRankByClickTroughRate() { // rank items result, err := suite.rankByClickTroughRate(&data.User{UserId: "1"}, [][]string{{"1", "2", "3", "4", "5"}}, itemCache, new(mockFactorizationMachine)) suite.NoError(err) - suite.Equal([]string{"5", "4", "3", "2", "1"}, lo.Map(result, func(d cache.Document, _ int) string { + suite.Equal([]string{"5", "4", "3", "2", "1"}, lo.Map(result, func(d cache.Score, _ int) string { return d.Id })) - suite.IsDecreasing(lo.Map(result, func(d cache.Document, _ int) float64 { + suite.IsDecreasing(lo.Map(result, func(d cache.Score, _ int) float64 { return d.Score })) } @@ -929,9 +929,9 @@ func (suite *WorkerTestSuite) TestReplacement_ClickThroughRate() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "10", Score: 10, Categories: []string{""}, Timestamp: recommendTime}, {Id: "9", Score: 9, Categories: []string{""}, Timestamp: recommendTime}, }, recommends) @@ -940,7 +940,7 @@ func (suite *WorkerTestSuite) TestReplacement_ClickThroughRate() { err = suite.CacheClient.Set(ctx, cache.Time(cache.Key(cache.LastUpdateUserRecommendTime, "0"), time.Now().AddDate(-1, 0, 0))) suite.NoError(err) // insert popular items - err = suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.PopularItems, "", []cache.Score{ {Id: "7", Score: 10, Categories: []string{""}}, {Id: "6", Score: 9, Categories: []string{""}}, {Id: "5", Score: 8, Categories: []string{""}}, @@ -958,9 +958,9 @@ func (suite *WorkerTestSuite) TestReplacement_ClickThroughRate() { recommendTime, err = suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "10", Score: 9, Categories: []string{""}, Timestamp: recommendTime}, {Id: "9", Score: 7.4, Categories: []string{""}, Timestamp: recommendTime}, {Id: "7", Score: 7, Categories: []string{""}, Timestamp: recommendTime}, @@ -994,9 +994,9 @@ func (suite *WorkerTestSuite) TestReplacement_CollaborativeFiltering() { recommendTime, err := suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) + recommends, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "10", Score: 10, Categories: []string{""}, Timestamp: recommendTime}, {Id: "9", Score: 9, Categories: []string{""}, Timestamp: recommendTime}, }, recommends) @@ -1005,7 +1005,7 @@ func (suite *WorkerTestSuite) TestReplacement_CollaborativeFiltering() { err = suite.CacheClient.Set(ctx, cache.Time(cache.Key(cache.LastUpdateUserRecommendTime, "0"), time.Now().AddDate(-1, 0, 0))) suite.NoError(err) // insert popular items - err = suite.CacheClient.AddDocuments(ctx, cache.PopularItems, "", []cache.Document{ + err = suite.CacheClient.AddScores(ctx, cache.PopularItems, "", []cache.Score{ {Id: "7", Score: 10, Categories: []string{""}}, {Id: "6", Score: 9, Categories: []string{""}}, {Id: "5", Score: 8, Categories: []string{""}}}) @@ -1022,9 +1022,9 @@ func (suite *WorkerTestSuite) TestReplacement_CollaborativeFiltering() { recommendTime, err = suite.CacheClient.Get(ctx, cache.Key(cache.LastUpdateUserRecommendTime, "0")).Time() suite.NoError(err) // read recommend result - recommends, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) + recommends, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 3) suite.NoError(err) - suite.Equal([]cache.Document{ + suite.Equal([]cache.Score{ {Id: "10", Score: 9, Categories: []string{""}, Timestamp: recommendTime}, {Id: "9", Score: 7.4, Categories: []string{""}, Timestamp: recommendTime}, {Id: "7", Score: 7, Categories: []string{""}, Timestamp: recommendTime}, @@ -1037,23 +1037,23 @@ func (suite *WorkerTestSuite) TestUserActivity() { suite.NoError(err) err = suite.CacheClient.Set(ctx, cache.Time(cache.Key(cache.LastModifyUserTime, "1"), time.Now().AddDate(0, 0, -10))) suite.NoError(err) - err = suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "0", []cache.Document{{Id: "0", Score: 1, Categories: []string{""}}}) + err = suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "0", []cache.Score{{Id: "0", Score: 1, Categories: []string{""}}}) suite.NoError(err) - err = suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "1", []cache.Document{{Id: "1", Score: 1, Categories: []string{""}}}) + err = suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "1", []cache.Score{{Id: "1", Score: 1, Categories: []string{""}}}) suite.NoError(err) - err = suite.CacheClient.AddDocuments(ctx, cache.OfflineRecommend, "2", []cache.Document{{Id: "2", Score: 1, Categories: []string{""}}}) + err = suite.CacheClient.AddScores(ctx, cache.OfflineRecommend, "2", []cache.Score{{Id: "2", Score: 1, Categories: []string{""}}}) suite.NoError(err) suite.True(suite.checkUserActiveTime(ctx, "0")) suite.True(suite.checkUserActiveTime(ctx, "1")) suite.True(suite.checkUserActiveTime(ctx, "2")) - docs, err := suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 1) + docs, err := suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 1) suite.NoError(err) suite.NotEmpty(docs) - docs, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "1", []string{""}, 0, 1) + docs, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "1", []string{""}, 0, 1) suite.NoError(err) suite.NotEmpty(docs) - docs, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "2", []string{""}, 0, 1) + docs, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "2", []string{""}, 0, 1) suite.NoError(err) suite.NotEmpty(docs) @@ -1061,13 +1061,13 @@ func (suite *WorkerTestSuite) TestUserActivity() { suite.True(suite.checkUserActiveTime(ctx, "0")) suite.False(suite.checkUserActiveTime(ctx, "1")) suite.True(suite.checkUserActiveTime(ctx, "2")) - docs, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 1) + docs, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "0", []string{""}, 0, 1) suite.NoError(err) suite.NotEmpty(docs) - docs, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "1", []string{""}, 0, 1) + docs, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "1", []string{""}, 0, 1) suite.NoError(err) suite.Empty(docs) - docs, err = suite.CacheClient.SearchDocuments(ctx, cache.OfflineRecommend, "2", []string{""}, 0, 1) + docs, err = suite.CacheClient.SearchScores(ctx, cache.OfflineRecommend, "2", []string{""}, 0, 1) suite.NoError(err) suite.NotEmpty(docs) }