I'm quite new to ElasticSearch aggregations. I want to be able to count how many documents are retrieved with a not null field.
Here's what I do to count how many documents don't have a name
field.
{
"size": 3,
"query": {
"query_string": {
"query": "martin"
}
},
"aggs": {
"results_without_mb_id": {
"missing": {
"field": "name"
}
}
}
}
It works but I want to do the exact opposite. Is there an existing
aggregation?
Elasticsearch Aggregations provide you with the ability to group and perform calculations and statistics (such as sums and averages) on your data by using a simple search query. An aggregation can be viewed as a working unit that builds analytical information across a set of documents.
sum_other_doc_count is the number of documents that didn't make it into the the top size terms.
Create an aggregation-based visualization paneledit Choose the type of visualization you want to create, then use the editor to configure the options. On the dashboard, click All types > Aggregation based. Select the visualization type you want to create. Select the data source you want to visualize.
Do this by passing an 'exists' filter to a filter aggregation. Like above, just replace 'missing' with 'exists', and also add 'filter' key, so:
{ "size": 3,
"query": {
"query_string": {
"query" : "martin"
}
},
"aggs": {
"results_without_mb_id": {
"filter": {
"exists": {
"field": "name"
}
}
}
}
You want to use the "exists" filter.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html
Here is a sample that finds all the documents where authResult.codeID exists, and then runs an aggregation on it.:
GET prodstarbucks/authEvent/_search
{
"size": 0,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"exists": {
"field": "authResult.codeID"
}
}
}
},
"aggs": {
"users": {
"terms": {
"field": "authInput.userName.userNameNotAnalyzed",
"size": 5
}
}
}
}
}
Note: If you only want to count the documents you don't even need an aggregation, just use the "total" number of hits returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With