0
0
Elasticsearchquery~20 mins

Cardinality aggregation in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cardinality Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this cardinality aggregation query?
Given an Elasticsearch index with documents containing a field user_id, what will be the value of value in the aggregation result?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "unique_users": {
      "cardinality": {
        "field": "user_id"
      }
    }
  }
}
AThe count of unique user_id values in the index
BThe total number of documents in the index
CThe sum of all user_id values
DThe average of all user_id values
Attempts:
2 left
💡 Hint
Cardinality aggregation counts unique values of a field.
Predict Output
intermediate
2:00remaining
What does this cardinality aggregation with precision_threshold do?
Consider this query with precision_threshold set to 1000. What effect does this have on the cardinality result?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "unique_users": {
      "cardinality": {
        "field": "user_id",
        "precision_threshold": 1000
      }
    }
  }
}
AIt limits the count to a maximum of 1000 unique values
BIt sorts the unique user_id values before counting
CIt decreases accuracy for counts above 1000 unique values
DIt increases accuracy for counts up to 1000 unique values, possibly using more memory
Attempts:
2 left
💡 Hint
Precision threshold controls accuracy and memory tradeoff.
🔧 Debug
advanced
2:00remaining
Why does this cardinality aggregation return a lower count than expected?
This query returns a cardinality count lower than the actual unique user_id count. What is the most likely cause?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "unique_users": {
      "cardinality": {
        "field": "user_id",
        "precision_threshold": 100
      }
    }
  }
}
AThe size parameter should be greater than 0 to get correct counts
BThe field user_id is not indexed, so aggregation fails
CThe precision_threshold is too low, causing approximate undercounting
DThe query is missing a filter to include all documents
Attempts:
2 left
💡 Hint
Lower precision_threshold can cause approximate results.
🧠 Conceptual
advanced
2:00remaining
What is the main tradeoff when using cardinality aggregation in Elasticsearch?
Choose the best description of the tradeoff involved in using cardinality aggregation.
ADocument relevance versus aggregation precision
BAccuracy of unique counts versus memory and CPU usage
CNumber of shards versus index size
DSpeed of query execution versus network bandwidth
Attempts:
2 left
💡 Hint
Cardinality uses approximate algorithms to save resources.
Predict Output
expert
2:00remaining
What is the cardinality aggregation result for nested fields?
Given documents with nested field comments.user_id, which aggregation correctly counts unique user_id values across all comments?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "unique_commenters": {
      "cardinality": {
        "field": "comments.user_id"
      }
    }
  }
}
ACounts unique user_id values across all nested comments correctly
BReturns zero because cardinality does not work on nested fields
CCounts unique user_id values only from the first nested comment per document
DThrows an error because nested fields require a nested aggregation wrapper
Attempts:
2 left
💡 Hint
Metric aggregations like cardinality work directly on nested fields.