How to count the number of occurrence of fields with data in a given index of douments

I am trying to count the number of occurrence of fields with data in search bar so that after finding the count i can use in metric visualization.

These fields are with in array object (data) for ex Below is JSON for one document

{
"_index": "test",
"_type": "extracted",
"_id": "123",
"_source": {
"document_id": 12345,
"document_number": "56789",
"data": [
{
"field1": null,
"name": "field1",
"value": null,
"page_number": null,
"rect": null,
"user_id": null
},
{
"field2": null,
"name": "field2",
"value": null,
"page_number": null,
"rect": null,
"user_id": null
}, {
"field3": null,
"name": "field3",
"value": null,
"page_number": null,
"rect": null,
"user_id": null
}]
}

These array object (data) present in all document ids. Suppose i have 10 documents and with in that 10 document if "field1" appearing 3 times and "field2" appearing 5 times and "field3" appearing 6 times then i want result Total fields count = 14 for metric visualization. Please note i want to use only metric visualization. How to achieve this. Tried query but getting single field count in search bar of kibana like below

{
"query": {
"term": {
"data.name": "field1"
}
}
}

Try using a field term aggregation.

{ 
"aggs": { 
"field": { 
"terms": { "field": "field" }
} 
}, 
"size": 0 
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.