DEV Community

sugiarto
sugiarto

Posted on

searchkick get total records

In one of my projects, I used searchkick gem and elasticsearch for implementing search feature.

Something weird happens when database records are over 10K records. The total records always show 10K, even though the actual records are 267K.

I fixed this issue by adding

body_options: { track_total_hits: true }
Enter fullscreen mode Exit fullscreen mode

As an argument for the search method.

Here is an example

collections = MyModel.pagy_search(query, fields: [:name], load: false, body_options: { track_total_hits: true })
pagy, properties = pagy_searchkick(collections)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)