DEV Community

sugiarto
sugiarto

Posted on

1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay