feat(search): merge per-space results in order_by order

The fan-out passes order_by through to each engine query; the merge
re-sorts the combined matches with CompareMatches and keeps the score
as tiebreaker (and as the sole criterion when no order_by is given).

(cherry picked from commit 66af40fc34fd9daffc028ab06f83f7ade8a360d1)
This commit is contained in:
Dominik Schmidt committed 2026-12-03 17:23:01 +01:00
1 parent c867efb2b7
commit 01bca083a6
2 files changed
+53 -2

No files matched your search

+10 -2
View File
@@ -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,
@@ -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: &timestamppb.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{