'Elasticsearch : aggregation "existing" fields
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?
Solution 1:[1]
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"
}
}
}
}
Solution 2:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Noumenon |
Solution 2 | jhilden |