The Value Count Aggregation is a single-value metrics aggregation that counts the number of values that are extracted from the aggregated documents. It can be used to count the number of values a field has across the documents in a bucket.
Syntax
{
"aggs": {
"value_count_name": {
"value_count": {
"field": "field_name"
}
}
}
}
For more details, refer to the official Elasticsearch documentation.
Example Usage
GET /my-index/_search
{
"aggs": {
"total_products": {
"value_count": {
"field": "product_id"
}
}
}
}
This example counts the number of product IDs in the index.
Common Issues
- Missing field: Ensure the specified field exists in your mapping.
- Null values: The aggregation ignores null values. Use
missing
parameter to include documents with missing values. - Performance: On large datasets, consider using sampling or filtering to improve performance.
Best Practices
- Use Value Count Aggregation in combination with other aggregations for more complex analytics.
- When counting unique values, consider using Cardinality Aggregation instead.
- For better performance on text fields, use a keyword sub-field if available.
Frequently Asked Questions
Q: How does Value Count Aggregation differ from Cardinality Aggregation?
A: Value Count Aggregation counts all values, including duplicates, while Cardinality Aggregation counts unique values only.
Q: Can Value Count Aggregation be used on nested fields?
A: Yes, but you need to use it within a nested aggregation to access nested fields correctly.
Q: Does Value Count Aggregation work with all field types?
A: It works with most field types, but it's most commonly used with numeric, keyword, or date fields.
Q: How can I exclude certain documents from the count?
A: Use a filter aggregation or query to exclude documents before applying the Value Count Aggregation.
Q: Is Value Count Aggregation affected by the size
parameter in the query?
A: No, aggregations operate on the entire result set, regardless of the size
parameter used for document retrieval.