Concept Flow - Cardinality aggregation
Start Query
Search Documents
Extract Field Values
Count Unique Values
Return Cardinality Result
End
The query searches documents, extracts values of a field, counts unique values, and returns the count.
{
"aggs": {
"unique_users": {
"cardinality": { "field": "user_id" }
}
}
}| Step | Action | Field Values Extracted | Unique Values Counted | Result |
|---|---|---|---|---|
| 1 | Start query execution | - | - | - |
| 2 | Search documents matching criteria | [user_id: 1, 2, 1, 3, 2] | - | - |
| 3 | Extract user_id field values | [1, 2, 1, 3, 2] | - | - |
| 4 | Count unique values | - | 3 (values: 1, 2, 3) | - |
| 5 | Return cardinality result | - | - | 3 |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| field_values | - | [1, 2, 1, 3, 2] | [1, 2, 1, 3, 2] | [1, 2, 3] | [1, 2, 3] |
| unique_count | - | - | - | 3 | 3 |
Cardinality aggregation counts unique values of a field.
Syntax: { "aggs": { "name": { "cardinality": { "field": "fieldname" } } } }
It returns the count of distinct values.
Duplicates are counted once.
Missing fields are ignored.What does the cardinality aggregation in Elasticsearch do?
Which of the following is the correct syntax to define a cardinality aggregation on the field user_id in Elasticsearch?
{
"aggs": {
"unique_users": {
"cardinality": {
"field": "user_id"
}
}
}
}Given the following Elasticsearch aggregation query:
{
"aggs": {
"unique_countries": {
"cardinality": {
"field": "country"
}
}
}
}If the indexed documents have countries: ["US", "US", "FR", "DE", "FR", "JP"], what is the expected approximate output value for unique_countries?
Identify the error in this cardinality aggregation query:
{
"aggs": {
"unique_users": {
"cardinality": {
"field_name": "user_id"
}
}
}
}You want to find the approximate number of unique products sold in each category using Elasticsearch. Which aggregation setup correctly combines terms and cardinality aggregations?
{
"aggs": {
"by_category": {
"terms": { "field": "category" },
"aggs": {
"unique_products": {
"cardinality": { "field": "product_id" }
}
}
}
}
}Pass the test and this pattern moves unknown