Search code examples
ruby-on-railssearchkick

Searchkick: counting records in index


I can open a Rails console on the production server and do a search * and then count on my ElasticSearch db.

s = Sentence.search '*'
s.count

...5, 381587, 381590, 381597, 381600, 381602, 381604, 381618, 381632, 381637, 381640, 381642, 381645, 381648, 381670, 381678, 381679, 381686, 381693, 381698, 381712, 387518, 387519, 387535, 387541)
=> 1000

There are two problems here

(1) It is paging at 1000. Where is this set and how can I ignore it for a total count of entries in the index?

(2) The search is bringing back a Rails ID not an ElasticSearch ID. Does this mean I am then doing a db call?

How can I get a count of entries directly from ElasticSearch via the Rails console with Searchkick?


Solution

  • You can use the method total_count to get the full count regardless of pagination.

    s = Sentence.search '*'
    s.total_count
    

    (2) The search is bringing back a Rails ID not an ElasticSearch ID. Does this mean I am then doing a db call?

    According to my understanding searchkick would create a elasticsearchid for each of your row in the table. Then, when you search it would search elasticsearch and bring the list of ids that match the search keyword and then, pull out all the records from the database based on the elasticsearchids returned.