From a4ba79dc3ee34318e62bc2e69090c7e4e216722e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Duffeck?= Date: Tue, 12 Jul 2022 15:30:16 +0200 Subject: [PATCH] Compile one sorted list of matches and apply the limit if needed --- .../pkg/search/provider/searchprovider.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/services/search/pkg/search/provider/searchprovider.go b/services/search/pkg/search/provider/searchprovider.go index 4930db606..a744699e6 100644 --- a/services/search/pkg/search/provider/searchprovider.go +++ b/services/search/pkg/search/provider/searchprovider.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "path/filepath" + "sort" "strings" "time" @@ -46,6 +47,16 @@ type Provider struct { type MatchArray []*searchmsg.Match +func (s MatchArray) Len() int { + return len(s) +} +func (s MatchArray) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} +func (s MatchArray) Less(i, j int) bool { + return s[i].Score > s[j].Score +} + func New(gwClient gateway.GatewayAPIClient, indexClient search.IndexClient, machineAuthAPIKey string, eventsChan <-chan interface{}, logger log.Logger) *Provider { p := &Provider{ gwClient: gwClient, @@ -169,6 +180,16 @@ func (p *Provider) Search(ctx context.Context, req *searchsvc.SearchRequest) (*s } } + // compile one sorted list of matches from all spaces and apply the limit if needed + sort.Sort(matches) + limit := req.PageSize + if limit == 0 { + limit = 200 + } + if int32(len(matches)) > limit { + matches = matches[0:limit] + } + return &searchsvc.SearchResponse{ Matches: matches, TotalMatches: total,