diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index 8c4bd1173c..3ada609301 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -356,8 +356,15 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se return nil, errtypes.BadRequest(err.Error()) } - // compile one sorted list of matches from all spaces and apply from/limit if needed - sort.Sort(matches) + // every engine answers in order_by order (by score without one); the merge + // re-establishes it across spaces, ties by score and id + orderBy := req.GetOrderBy() + sort.SliceStable(matches, func(i, j int) bool { + if c := CompareMatches(matches[i], matches[j], orderBy); c != 0 { + return c < 0 + } + return matches.Less(i, j) + }) limit := PageSizeOrDefault(req.PageSize) if from := int(req.GetFrom()); from > 0 { if from < len(matches) { @@ -528,6 +535,7 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, Query: expanded, Aggregations: req.GetAggregations(), AggregationFilters: req.GetAggregationFilters(), + OrderBy: req.GetOrderBy(), Ref: &searchmsg.Reference{ ResourceId: searchRootID, Path: searchPathPrefix, diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 0e1f19fc3c..9d3d59c9ec 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -22,6 +22,7 @@ import ( "github.com/stretchr/testify/mock" "google.golang.org/grpc" "google.golang.org/protobuf/testing/protocmp" + "google.golang.org/protobuf/types/known/timestamppb" "github.com/opencloud-eu/opencloud/pkg/conversions" "github.com/opencloud-eu/opencloud/pkg/log" @@ -980,6 +981,48 @@ var _ = Describe("Searchprovider", func() { }) }) + Context("with two personal spaces returning photos", func() { + photoMatch := func(space, name string, taken int64, score float32) *searchmsg.Match { + return &searchmsg.Match{Score: score, Entity: &searchmsg.Entity{ + Id: &searchmsg.ResourceID{StorageId: "storageid", SpaceId: space, OpaqueId: name}, + Ref: &searchmsg.Reference{ResourceId: &searchmsg.ResourceID{StorageId: "storageid", SpaceId: space, OpaqueId: space}, Path: "./" + name}, + Name: name, + Photo: &searchmsg.Photo{TakenDateTime: ×tamppb.Timestamp{Seconds: taken}}, + }} + } + names := func(req *searchsvc.SearchRequest) []string { + res, err := s.Search(ctx, req) + Expect(err).ToNot(HaveOccurred()) + out := []string{} + for _, m := range res.Matches { + out = append(out, m.GetEntity().GetName()) + } + return out + } + + BeforeEach(func() { + listsSpacesAB() + searchOfSpace("a").Return(&search.IndexResult{TotalMatches: 2, Matches: []*searchmsg.Match{ + photoMatch("a", "a-old.jpg", 100, 0.9), photoMatch("a", "a-new.jpg", 300, 0.1), + }}, nil) + searchOfSpace("b").Return(&search.IndexResult{TotalMatches: 2, Matches: []*searchmsg.Match{ + photoMatch("b", "b-newest.jpg", 400, 0.5), photoMatch("b", "b-mid.jpg", 200, 0.4), + }}, nil) + }) + + It("forwards order_by to the engine and merges matches across spaces in that order", func() { + byTaken := []*searchsvc.SortProperty{{Name: "photo.takenDateTime", IsDescending: true}} + Expect(names(&searchsvc.SearchRequest{Query: "mediatype:image", OrderBy: byTaken})).To(Equal([]string{"b-newest.jpg", "a-new.jpg", "b-mid.jpg", "a-old.jpg"})) + indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool { + return len(req.OrderBy) == 1 && req.OrderBy[0].GetName() == "photo.takenDateTime" && req.OrderBy[0].GetIsDescending() + })) + }) + + It("merges matches by score when no order_by is given", func() { + Expect(names(&searchsvc.SearchRequest{Query: "mediatype:image"})).To(Equal([]string{"a-old.jpg", "b-newest.jpg", "b-mid.jpg", "a-new.jpg"})) + }) + }) + Context("with two personal spaces returning matches of the same score", func() { match := func(space, name string) *searchmsg.Match { return &searchmsg.Match{Score: 1, Entity: &searchmsg.Entity{