Elasticsearch返回错误计数求助:如何获取文档items正确总数?
Hey there! Let's work through this together.
First off, to clear up your first question: Not setting a manual unique ID isn't causing that inflated count. Elasticsearch auto-generates a unique ID for every document if you don't provide one, and the standard count operation counts documents, not the individual elements inside array fields. If you're seeing a bigger number than expected from a count query, it's almost certainly because that query is tallying up documents, not the items in your items arrays.
Wait, quick side note: In your example, the total number of items across all documents should be 6 (2 from a, 2 from b, 2 from c)—I assume that "3" was a typo? Either way, here's how to get the correct total of array elements:
Option 1: Use value_count Aggregation (Easiest)
If your items field is mapped as a keyword (or if your text field has a keyword sub-field, which is common), the value_count aggregation will count every occurrence of a value in the field—including all elements in each array:
{ "size": 0, // We don't need the actual documents, just the aggregation result "aggs": { "total_items_count": { "value_count": { "field": "items" // Use "items.keyword" if your field is text with a keyword sub-field } } } }
Option 2: Sum Array Lengths with a Script
If fielddata is disabled for your text field (and you don't want to enable it), you can calculate the length of each items array per document and sum those lengths up:
{ "size": 0, "aggs": { "total_items_count": { "sum": { "script": "doc['items'].length" } } } }
Why Your Original Count Was Off
The standard _count API (like GET /your_index/_count) or a count query returns how many documents match your filters. In your case, that's 3 documents—but you're trying to count the elements inside those documents' arrays, which requires an aggregation instead of a simple document count.
If you were seeing an even larger number than 3, double-check that your query isn't matching more documents than you intended (e.g., no accidental filters missing). But the main issue is using a document count when you need an array element tally.
内容的提问来源于stack exchange,提问作者skyfail




