Skip to content

Commit c389505

Browse files
committed
fix: ParsePaginator
1 parent c404c43 commit c389505

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

modules/indexer/internal/paginator.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,24 @@ import (
1010
)
1111

1212
// ParsePaginator parses a db.Paginator into a skip and limit
13-
func ParsePaginator(paginator db.Paginator, max ...int) (int, int) {
13+
func ParsePaginator(listOptions db.ListOptions, max ...int) (int, int) {
1414
// Use a very large number to indicate no limit
1515
unlimited := math.MaxInt32
1616
if len(max) > 0 {
1717
// Some indexer engines have a limit on the page size, respect that
1818
unlimited = max[0]
1919
}
2020

21-
if paginator == nil || paginator.IsListAll() {
21+
if listOptions.IsListAll() {
22+
// It shouldn't happen. In actual usage scenarios, there should not be requests to search all.
23+
// But if it does happen, respect it and return "unlimited".
2224
return 0, unlimited
2325
}
2426

25-
// Warning: Do not use GetSkipTake() for *db.ListOptions
26-
// Its implementation could reset the page size with setting.API.MaxResponseItems
27-
if listOptions, ok := paginator.(*db.ListOptions); ok {
28-
if listOptions.Page >= 0 && listOptions.PageSize > 0 {
29-
var start int
30-
if listOptions.Page == 0 {
31-
start = 0
32-
} else {
33-
start = (listOptions.Page - 1) * listOptions.PageSize
34-
}
35-
return start, listOptions.PageSize
36-
}
37-
return 0, unlimited
27+
if listOptions.PageSize == 0 {
28+
// Do not return any results when searching, it's used to get the total count only.
29+
return 0, 0
3830
}
3931

40-
return paginator.GetSkipTake()
32+
return listOptions.GetSkipTake()
4133
}

modules/indexer/issues/bleve/bleve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
266266
indexerQuery = bleve.NewMatchAllQuery()
267267
}
268268

269-
skip, limit := indexer_internal.ParsePaginator(&options.ListOptions)
269+
skip, limit := indexer_internal.ParsePaginator(options.ListOptions)
270270
search := bleve.NewSearchRequestOptions(indexerQuery, limit, skip, false)
271271

272272
if options.SortBy == "" {

modules/indexer/issues/elasticsearch/elasticsearch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
248248
// TODO: make it configurable since it's configurable in elasticsearch
249249
const maxPageSize = 10000
250250

251-
skip, limit := indexer_internal.ParsePaginator(&options.ListOptions, maxPageSize)
251+
skip, limit := indexer_internal.ParsePaginator(options.ListOptions, maxPageSize)
252252
searchResult, err := b.inner.Client.Search().
253253
Index(b.inner.VersionedIndexName()).
254254
Query(query).

modules/indexer/issues/meilisearch/meilisearch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
216216
"id:desc",
217217
}
218218

219-
skip, limit := indexer_internal.ParsePaginator(&options.ListOptions, maxTotalHits)
219+
skip, limit := indexer_internal.ParsePaginator(options.ListOptions, maxTotalHits)
220220

221221
keyword := options.Keyword
222222
if !options.IsFuzzyKeyword {

0 commit comments

Comments
 (0)