Compile one sorted list of matches and apply the limit if needed

This commit is contained in:
André Duffeck
2022-07-12 15:30:16 +02:00
parent 0fb8721da6
commit a4ba79dc3e

View File

@@ -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,