Counting matched results

It is often required to return only the count of the matched results and not the results themselves. The advantages of using a count request is the performance it offers and reduced resource usage, as a standard search call also returns hits count.

A lot of scenarios involve counting, as follows:

  • To return the number of, for example, posts for a blog or comments for a post.
  • To validate that some items are available. Are there posts? Are there comments?

Getting ready

You need a working ElasticSearch cluster and an index populated with the script chapter_05/populate_query.sh, available in the code bundle for this book.

How to do it...

In order to execute a counting query, perform the following steps:

  1. From the command line, execute ...

Get ElasticSearch Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.