0
0
Elasticsearchquery~10 mins

Cardinality aggregation in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Elasticsearch
{
  "aggs": {
    "unique_users": {
      "cardinality": { "field": "user_id" }
    }
  }
}
This aggregation counts the number of unique user_id values in the documents.
Execution Table
StepActionField Values ExtractedUnique Values CountedResult
1Start query execution---
2Search documents matching criteria[user_id: 1, 2, 1, 3, 2]--
3Extract user_id field values[1, 2, 1, 3, 2]--
4Count unique values-3 (values: 1, 2, 3)-
5Return cardinality result--3
💡 All documents processed, unique user_id count computed as 3
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
field_values-[1, 2, 1, 3, 2][1, 2, 1, 3, 2][1, 2, 3][1, 2, 3]
unique_count---33
Key Moments - 2 Insights
Why does the count of unique values ignore repeated user_id values?
Because cardinality aggregation counts only distinct values, duplicates like user_id 1 and 2 are counted once, as shown in step 4 of the execution_table.
What happens if the field has missing values in some documents?
Documents without the field are ignored in counting unique values, so only existing field values contribute to the cardinality, as implied in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, how many unique user_id values are counted?
A3
B5
C2
D4
💡 Hint
Check the 'Unique Values Counted' column at step 4 in the execution_table.
At which step are the field values extracted from documents?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column describing extraction in the execution_table.
If a document has no user_id field, how does it affect the unique count?
AIt increases the unique count by 1
BIt decreases the unique count by 1
CIt does not affect the unique count
DIt causes an error
💡 Hint
Refer to the key_moments explanation about missing field values.
Concept Snapshot
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.
Full Transcript
Cardinality aggregation in Elasticsearch counts the number of unique values for a specified field. The process starts by executing a query to find documents. Then, it extracts the values of the chosen field from these documents. Next, it counts how many distinct values appear, ignoring duplicates. Finally, it returns this count as the result. For example, if user_id values are [1, 2, 1, 3, 2], the unique count is 3 because only values 1, 2, and 3 are distinct. Documents missing the field do not affect the count. This aggregation helps find how many different values exist in a dataset.