Reputation: 149
I'm new with Elastic Search. I have documents in Elastic Search that contain nested fields like this:
Document 1:
"Volume": [
{
"partition": "s1",
"type": "west"
}
{
"partition": "s2",
"type": "south"
}
]
Document 2:
"Volume": [
{
"partition": "a2",
"type": "north"
}
]
Document 3:
"Volume": [
{
"partition": "f3",
"type": "north"
}
{
"partition": "a1",
"type": "south"
}
]
and so on. I need to count the number of "type" fields, so the expected result would be: "west": 1 "south": 2 "north":2
I used nested aggregation, like this:
"size":0,
"aggs": {
"nested_properties": {
"nested": {
"path": "Volume"
},
"aggs": {
"count": {
"cardinality": {
"field": "Volume.type"
}
}
}
}
}
But the result is:
"aggregations": {
"nested_properies": {
"doc_count": 123456,
"count": {
"value": 9
}
}
}
How can I count the number of entries for each "type" subfield?
Upvotes: 0
Views: 188
Reputation: 3261
You can use Term Aggregation.
Like this:
{
"size": 0,
"aggs": {
"groups": {
"nested": {
"path": "Volume"
},
"aggs": {
"NAME": {
"terms": {
"field": "Volume.type.keyword",
"size": 10
}
}
}
}
}
}
Response:
"aggregations": {
"groups": {
"doc_count": 5,
"NAME": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "north",
"doc_count": 2
},
{
"key": "south",
"doc_count": 2
},
{
"key": "west",
"doc_count": 1
}
]
}
}
}
Upvotes: 1