Elasticsearch : aggregation "existing" fields
10,629
Solution 1
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"
}
}
}
}
Solution 2
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.
Author by
litil
Updated on June 13, 2022Comments
-
litil almost 2 years
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?