Facets are deprecated and will be removed in a future release. You are
encouraged to migrate to aggregations instead.

The usual purpose of a full-text search engine is to return a small
number of documents matching your query.

Facets provide aggregated data based on a search query. In the
simplest case, a
terms facet
can return facet counts for various facet values for a specific
field. Elasticsearch supports more facet implementations, such as
statistical
or
date histogram facets.

The field used for facet calculations must be of type numeric,
date/time or be analyzed as a single token — see the
Mapping guide for details on the
analysis process.

You can give the facet a custom name and return multiple facets in one
request.

Let’s try it out with a simple example. Suppose we have a number of
articles with a field called tags, preferably analyzed with the
keyword
analyzer. The facet aggregation will return counts for the most popular
tags across the documents matching your query — or across all documents
in the index.

In the terms array, relevant terms and counts are returned. You’ll
probably want to display these to your users. The facet returns several
important counts:

missing : The number of documents which have no value for the
faceted field

total : The total number of terms in the facet

other : The number of terms not included in the returned facet
(effectively other = total - terms )

Notice, that the counts are scoped to the current query: foo is
counted only twice (not three times), bar is counted twice and baz
once. Also note that terms are counted once per document, even if the
occur more frequently in that document.

That’s because the primary purpose of facets is to enable
faceted navigation,
allowing the user to refine her query based on the insight from the
facet, i.e. restrict the search to a specific category, price or date
range. Facets can be used, however, for other purposes: computing
histograms, statistical aggregations, and more. See the blog about
data visualization for inspiration.

As we have already mentioned, facet computation is restricted to the
scope of the current query, called main, by default. Facets can be
computed within the global scope as well, in which case it will return
values computed across all documents in the index:

Nested mapping allows
for better support for "inner" documents faceting, especially when it
comes to multi valued key and value facets (like histograms, or term
stats).

What is it good for? First of all, this is the only way to use facets on
nested documents once they are used (possibly for other reasons). But,
there is also facet specific reason why nested documents can be used,
and that’s the fact that facets working on different key and value field
(like term_stats, or histogram) can now support cases where both are
multi valued properly.

The nested element provides the path to the nested document (can be a
multi level nested docs) that will be used.

Facet filter allows you to filter your facet on the nested object level.
It is important that these filters match on the nested object level and
not on the root document level. In the following example the
terms_stats only applies on nested objects with the name blue.