Skip to content

Commit f886a75

Browse files
committed
fix: handle counting with meilisearch
1 parent dcdd653 commit f886a75

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

modules/indexer/issues/meilisearch/meilisearch.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
218218

219219
skip, limit := indexer_internal.ParsePaginator(options.Paginator, maxTotalHits)
220220

221+
counting := limit == 0
222+
if counting {
223+
// If set limit to 0, it will be 20 by default, and -1 is not allowed.
224+
// See https://www.meilisearch.com/docs/reference/api/search#limit
225+
// So set limit to 1 to make the cost as low as possible, then clear the result before returning.
226+
limit = 1
227+
}
228+
221229
keyword := options.Keyword
222230
if !options.IsFuzzyKeyword {
223231
// to make it non fuzzy ("typo tolerance" in meilisearch terms), we have to quote the keyword(s)
@@ -236,6 +244,10 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
236244
return nil, err
237245
}
238246

247+
if counting {
248+
searchRes.Hits = nil
249+
}
250+
239251
hits, err := convertHits(searchRes)
240252
if err != nil {
241253
return nil, err

0 commit comments

Comments
 (0)