Elasticsearch allows you to very easily search
across multiple document types. When you perform a search, you get the count of
the total document hits along with some of the matching documents (10 by
default). However, you may need to also show the total document hits for each
document type you’re searching across. For example, when you display search
results as follows:
Fortunately, this can be accomplished using facets.
One thing to note, however, is that this will not work with filters. You must
use queries to get an accurate type facet count.
So, to retrieve type counts, we can construct a query such as:
Or, using Elastica:
This will give us a response such as:
The resulting json tells us our query matched 1 user type document and 1
company type document.