- Elasticsearch Guide: other versions:
- Getting Started
- Setup
- Breaking changes in 1.0
- API Conventions
- Document APIs
- Search APIs
- Search
- URI Search
- Request Body Search
- Search Template
- Search Shards API
- Aggregations
- Min Aggregation
- Max Aggregation
- Sum Aggregation
- Avg Aggregation
- Stats Aggregation
- Extended Stats Aggregation
- Value Count Aggregation
- Percentiles Aggregation
- Percentile Ranks Aggregation
- Cardinality Aggregation
- Geo Bounds Aggregation
- Top hits Aggregation
- Global Aggregation
- Filter Aggregation
- Missing Aggregation
- Nested Aggregation
- Reverse nested Aggregation
- Terms Aggregation
- Significant Terms Aggregation
- Range Aggregation
- Date Range Aggregation
- IPv4 Range Aggregation
- Histogram Aggregation
- Date Histogram Aggregation
- Geo Distance Aggregation
- GeoHash grid Aggregation
- Facets
- Suggesters
- Multi Search API
- Count API
- Validate API
- Explain API
- Percolator
- More Like This API
- Indices APIs
- Create Index
- Delete Index
- Indices Exists
- Open / Close Index API
- Put Mapping
- Get Mapping
- Get Field Mapping
- Types Exists
- Delete Mapping
- Index Aliases
- Update Indices Settings
- Get Settings
- Analyze
- Index Templates
- Warmers
- Status
- Indices Stats
- Indices Segments
- Indices Recovery
- Clear Cache
- Flush
- Refresh
- Optimize
- cat APIs
- Cluster APIs
- Query DSL
- Queries
- Match Query
- Multi Match Query
- Bool Query
- Boosting Query
- Common Terms Query
- Constant Score Query
- Dis Max Query
- Filtered Query
- Fuzzy Like This Query
- Fuzzy Like This Field Query
- Function Score Query
- Fuzzy Query
- GeoShape Query
- Has Child Query
- Has Parent Query
- Ids Query
- Indices Query
- Match All Query
- More Like This Query
- More Like This Field Query
- Nested Query
- Prefix Query
- Query String Query
- Simple Query String Query
- Range Query
- Regexp Query
- Span First Query
- Span Multi Term Query
- Span Near Query
- Span Not Query
- Span Or Query
- Span Term Query
- Term Query
- Terms Query
- Top Children Query
- Wildcard Query
- Minimum Should Match
- Multi Term Query Rewrite
- Template Query
- Filters
- And Filter
- Bool Filter
- Exists Filter
- Geo Bounding Box Filter
- Geo Distance Filter
- Geo Distance Range Filter
- Geo Polygon Filter
- GeoShape Filter
- Geohash Cell Filter
- Has Child Filter
- Has Parent Filter
- Ids Filter
- Indices Filter
- Limit Filter
- Match All Filter
- Missing Filter
- Nested Filter
- Not Filter
- Or Filter
- Prefix Filter
- Query Filter
- Range Filter
- Regexp Filter
- Script Filter
- Term Filter
- Terms Filter
- Type Filter
- Queries
- Mapping
- Analysis
- Analyzers
- Tokenizers
- Token Filters
- Standard Token Filter
- ASCII Folding Token Filter
- Length Token Filter
- Lowercase Token Filter
- Uppercase Token Filter
- NGram Token Filter
- Edge NGram Token Filter
- Porter Stem Token Filter
- Shingle Token Filter
- Stop Token Filter
- Word Delimiter Token Filter
- Stemmer Token Filter
- Stemmer Override Token Filter
- Keyword Marker Token Filter
- Keyword Repeat Token Filter
- KStem Token Filter
- Snowball Token Filter
- Phonetic Token Filter
- Synonym Token Filter
- Compound Word Token Filter
- Reverse Token Filter
- Elision Token Filter
- Truncate Token Filter
- Unique Token Filter
- Pattern Capture Token Filter
- Pattern Replace Token Filter
- Trim Token Filter
- Limit Token Count Token Filter
- Hunspell Token Filter
- Common Grams Token Filter
- Normalization Token Filter
- CJK Width Token Filter
- CJK Bigram Token Filter
- Delimited Payload Token Filter
- Keep Words Token Filter
- Classic Token Filter
- Apostrophe Token Filter
- Character Filters
- ICU Analysis Plugin
- Modules
- Index Modules
- Testing
- Glossary of terms
WARNING: Version 1.3 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Migrating to aggregations
editMigrating to aggregations
editFacets have been deprecated in favor of aggregations and as such it is recommended to migrate existing code using facets to aggregations.
It is recommended to read the documentation about aggregations before this section.
Simple cases
editIn quite a number of cases, the migration is rather straightforward as simple
facets have their direct aggregation equivalent and the only thing that is
required is to replace facets
with aggs
.
For instance:
{ "facets" : { "wow" : { "filter" : { "term" : { "tag" : "wow" } } } } }
can be translated to the following aggregation:
{ "aggs" : { "wow" : { "filter" : { "term" : { "tag" : "wow" } } } } }
We will now spend more time on facets that don’t have their direct aggregation equivalent and need more modifications.
Query facets
editThere is no query
aggregation so such facets must be migrated to the filter
aggregation.
For example:
{ "facets" : { "wow" : { "query" : { "query_string" : { "query" : "tag:wow" } } } } }
can be replaced with the following filter aggregation that uses the query filter:
{ "aggs" : { "wow" : { "filter" : { "query" : { "query_string" : { "query" : "tag:wow" } } } } } }
Term stats
editThere is no term_stats
aggregation, so you actually need to create a
terms aggregation that will
create buckets that will be processed with a
stats aggregation.
For example
{ "facets" : { "tag_price_stats" : { "terms_stats" : { "key_field" : "tag", "value_field" : "price" } } } }
can be replaced with
{ "aggs" : { "tags" : { "terms" : { "field" : "tag" }, "aggs" : { "price_stats" : { "stats" : { "field" : "price" } } } } } }
value_field
editThe histogram
, date_histogram
, range
and geo_distance
facets have a
value_field
parameter that allows to compute statistics per bucket. With
aggregations this needs to be changed to a sub
stats aggregation.
For example
{ "facets" : { "histo1" : { "date_histogram" : { "key_field" : "timestamp", "value_field" : "price", "interval" : "day" } } } }
can be replaced with
{ "aggs" : { "histo1" : { "date_histogram" : { "field" : "timestamp", "interval" : "day" }, "aggs" : { "price_stats" : { "stats" : { "field" : "price" } } } } } }
Global scope
editFacets allow to set a global scope by setting global : true
in the facet
definition. With aggregations, you will need to put your aggregation under a
global aggregation instead.
For example
{ "facets" : { "terms1" : { "terms" : { ... }, "global" : true } } }
can be replaced with
{ "aggs" : { "global_count" : { "global" : {}, "aggs" : { "terms1" : { "terms" : { ... } } } } } }
Facet filters
editFacet filters can be replaced with a filter aggregation.
For example
{ "facets" : { "<FACET NAME>" : { "<FACET TYPE>" : { ... }, "facet_filter" : { "term" : { "user" : "mvg" } } } } }
can be replaced with
{ "aggs" : { "filter1" : { "filter" : { "term" : { "user" : "mvg" } }, "aggs" : { "<AGG NAME>" : { "<AGG TYPE>" : { ... } } } } } }
Nested
editAggregations have a dedicated nested aggregation to deal with nested objects.
For example
{ "facets" : { "facet1" : { "terms" : { "field" : "name" }, "nested" : "obj1" } } }
can be replaced with
{ "aggs" : { "agg1" : { "nested" : { "path" : "obj1" }, "aggs" : { "agg1": { "terms": { "field" : "obj1.name" } } } } } }
Note how fields are identified with their full path instead of relative path.
Similarly, this more complex facet that combines nested
and facet filters:
{ "facets" : { "facet1" : { "terms" : { "field" : "name" }, "nested" : "obj1", "facet_filter" : { "term" : { "color" : "blue" } } } } }
can be replaced with the following aggregation, which puts a terms aggregation under a filter aggregation, and the filter aggregation under a nested aggregation:
{ "aggs" : { "nested_obj1" : { "nested" : { "path" : "obj1" }, "aggs" : { "color_filter" : { "filter" : { "term" : { "obj1.color" : "blue" } }, "aggs" : { "name_terms" : { "terms" : { "field" : "obj1.name" } } } } } } } }
In short, this aggregation first moves from the root documents to their nested
documents following the path obj1
. Then for each nested document, it filters
out those that are not blue, and for the remaining documents, it computes a
terms aggregation on the name
field.