liveBook · Manning

Document count

As explained in Elasticsearch in Action, Second Edition

doc_count is the Elasticsearch aggregation response property containing the number of documents in a bucket.

When a bookstore search groups books by rating, release year, or author and title, each bucket reports how many documents it contains. That value is doc_count. section 13.3.1 section 13.3.5

A bucket response commonly contains key and doc_count. key identifies the bucket; doc_count counts the documents that fit it. This differs from the _count API’s count variable, which reports documents across an index or query scope. section 2.2.1 section 13.3.1

Read doc_count according to the aggregation:

  • A rating histogram can report two books in one rating range and 35 in another. section 13.3.1
  • A date histogram can show yearly release totals, such as five books in 2020 and six in 2021. section 13.3.1
  • A multi_terms aggregation can count documents for each author-title combination. section 13.3.5
Listing 13.10 A histogram aggregation for books
GET books/_search
{
  "size": 0, 
  "aggs": {
    "ratings_histogram": {          #1
      "histogram": {                #2
        "field": "amazon_rating",   #3
        "interval": 1               #4
      }
    }
  }
}

Run the histogram query to see how aggregation buckets produce document counts. section 13.3.1

Listing 13.18 Aggregation for authors and their book titles as a map
GET books/_search?size=0
{
  "aggs": {
    "author_title_map": {
      "multi_terms": {       #1
        "terms": [           #2
          {
            "field": "author.keyword"
          },
          {
            "field": "title.keyword"
          }
        ]
      }
    }
  }
}

Compare author-title keys with the doc_count returned for each combined key. section 13.3.5

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines doc_count and illustrates it in histogram, date_histogram, and multi_terms responses without detailing counting internals.
Examples
Uses book ratings, release years, and author-title combinations as concrete counting examples.
What you can do after
Evaluate aggregation results by interpreting each bucket’s key alongside its document count.
How it connects
Connects doc_count to buckets, histogram aggregation, date_histogram, multi-terms aggregation, and terms aggregation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.