How to get ElasticSearch total count of items matching
Today I've Learned postUsing the ElasticSearch Rails this is how you can get total count of items in ElasticSearch document that match search query.
In this case we are searching for title “cat” inside “Work” model / document and we are limiting only 8 results to be returned
# this can be anything you need in your business logic
es_query = {:from=>0, :size=>8, :sort=>[{:_score=>{:order=>"desc"}},{"created_at"=>{"order"=>"desc"}}], :query=>{:bool=>{:must=>{:bool=>{:should=>[{:match=>{:title=>{:query=>"cat"}}}]}}}}}
Work.__elasticsearch__.search(es_query).count
# => 8 #...because you are counting just the returned items, not total
Work.__elasticsearch__.search(es_query).results.total
# => 180 # this is the real total in ElasticSearch
Special thx to my collegue Charlie who done the research around this
Entire blog website and all the articles can be forked from this Github Repo