Bird
Raised Fist0
Elasticsearchquery~20 mins

Cardinality aggregation in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What does the cardinality aggregation in Elasticsearch do?

easy
A. Calculates the sum of values in a field
B. Sorts documents by a field
C. Finds the average value of a field
D. Counts the number of unique values in a field

Solution

  1. Step 1: Understand the purpose of cardinality aggregation

    Cardinality aggregation is designed to count unique values in a specified field.
  2. Step 2: Compare with other aggregations

    Sum and average aggregations calculate totals and averages, not unique counts. Sorting is unrelated.
  3. Final Answer:

    Counts the number of unique values in a field -> Option D
  4. Quick Check:

    Cardinality = Unique count [OK]
Hint: Cardinality = count unique values fast [OK]
Common Mistakes:
  • Confusing cardinality with sum or average
  • Thinking it returns exact counts always
  • Mixing it with sorting or filtering
2.

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"
      }
    }
  }
}
easy
A. { "aggs": { "unique_users": { "cardinality": { "field": "user_id" } } } }
B. { "aggs": { "unique_users": { "terms": { "field": "user_id" } } } }
C. { "aggs": { "unique_users": { "sum": { "field": "user_id" } } } }
D. { "aggs": { "unique_users": { "avg": { "field": "user_id" } } } }

Solution

  1. Step 1: Identify the aggregation type for unique counts

    The cardinality aggregation is used to count unique values, so the key must be "cardinality".
  2. Step 2: Check the field name and structure

    The field is correctly specified as "user_id" inside the cardinality object.
  3. Final Answer:

    { "aggs": { "unique_users": { "cardinality": { "field": "user_id" } } } } -> Option A
  4. Quick Check:

    Correct syntax uses "cardinality" with "field" [OK]
Hint: Cardinality uses "cardinality" key with "field" [OK]
Common Mistakes:
  • Using terms aggregation instead of cardinality
  • Using sum or avg aggregations for unique counts
  • Incorrect JSON structure or missing braces
3.

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?

medium
A. 3
B. 4
C. 5
D. 6

Solution

  1. Step 1: Identify unique countries in the list

    The countries are US, FR, DE, JP. Counting unique values gives 4.
  2. Step 2: Understand cardinality output

    Cardinality aggregation returns approximate unique counts, which should be 4 here.
  3. Final Answer:

    4 -> Option B
  4. Quick Check:

    Unique countries count = 4 [OK]
Hint: Count distinct values in the field for cardinality [OK]
Common Mistakes:
  • Counting duplicates as unique
  • Confusing total documents with unique count
  • Expecting exact count always (approximate)
4.

Identify the error in this cardinality aggregation query:

{
  "aggs": {
    "unique_users": {
      "cardinality": {
        "field_name": "user_id"
      }
    }
  }
}
medium
A. Aggregation name cannot be "unique_users"
B. Missing closing brace for "aggs"
C. "field_name" should be "field"
D. Cardinality aggregation requires a "size" parameter

Solution

  1. Step 1: Check the field parameter name

    The correct key for specifying the field is "field", not "field_name".
  2. Step 2: Verify JSON structure and parameters

    The braces are correctly closed, aggregation name is valid, and "size" is not required for cardinality.
  3. Final Answer:

    "field_name" should be "field" -> Option C
  4. Quick Check:

    Use "field" key for cardinality field [OK]
Hint: Use "field", not "field_name" in cardinality [OK]
Common Mistakes:
  • Using wrong key "field_name" instead of "field"
  • Adding unnecessary parameters like "size"
  • Misnaming aggregation or missing braces
5.

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" }
        }
      }
    }
  }
}
hard
A. This setup groups by category and counts unique products per category
B. This setup counts unique categories per product
C. This setup sums product_id values per category
D. This setup finds average product_id per category

Solution

  1. Step 1: Understand the terms aggregation

    "terms" groups documents by unique values in "category" field.
  2. Step 2: Understand the nested cardinality aggregation

    Inside each category bucket, cardinality counts unique "product_id" values.
  3. Final Answer:

    This setup groups by category and counts unique products per category -> Option A
  4. Quick Check:

    Terms + cardinality = unique counts per group [OK]
Hint: Nest cardinality inside terms to count unique per group [OK]
Common Mistakes:
  • Confusing aggregation order or nesting
  • Using cardinality on category instead of product_id
  • Expecting sum or average instead of unique count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard