diff options
author | Alan Pearce | 2024-05-08 00:15:52 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-08 00:16:03 +0200 |
commit | 973345ad50f9b237714fcb364cf7f665b3909f9d (patch) | |
tree | 15225430bd5895b5140df0e301b0e6c3fb5758a8 /internal/search/search.go | |
parent | f459e84ecf7307fe2eeb7fbaa5b0c50613ec04f4 (diff) | |
download | searchix-973345ad50f9b237714fcb364cf7f665b3909f9d.tar.lz searchix-973345ad50f9b237714fcb364cf7f665b3909f9d.tar.zst searchix-973345ad50f9b237714fcb364cf7f665b3909f9d.zip |
feat: paginate search results
Diffstat (limited to 'internal/search/search.go')
-rw-r--r-- | internal/search/search.go | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/internal/search/search.go b/internal/search/search.go index b449512..83a8365 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -15,7 +15,7 @@ import ( "github.com/pkg/errors" ) -const maxResults = 10 +const ResultsPerPage = 20 type Result[T options.NixOption] struct { *bleve.SearchResult @@ -93,9 +93,14 @@ func New[T options.NixOption](kind string) (*Index[T], error) { }, nil } -func (index *Index[T]) Search(ctx context.Context, keyword string) (*Result[T], error) { +func (index *Index[T]) Search(ctx context.Context, keyword string, from uint64) (*Result[T], error) { query := bleve.NewMatchQuery(keyword) search := bleve.NewSearchRequest(query) + search.Size = ResultsPerPage + + if from != 0 { + search.From = int(from) + } bleveResult, err := index.index.SearchInContext(ctx, search) select { @@ -106,13 +111,10 @@ func (index *Index[T]) Search(ctx context.Context, keyword string) (*Result[T], return nil, errors.WithMessage(err, "failed to execute search query") } - results := make([]T, min(maxResults, bleveResult.Total)) + results := make([]T, min(ResultsPerPage, bleveResult.Total)) for i, result := range bleveResult.Hits { doc, _ := index.docs.Load(result.ID) results[i] = doc.(T) - if i > maxResults { - break - } } return &Result[T]{ |