Reputation: 1287
is there a way to write a query that matches the empty array and any non empty array? I don't want to match nulls and I don't care for absent fields.
I just want to scan all my fields in order to see which ones can return an array. Thank you!
Upvotes: 0
Views: 377
Reputation: 217314
The following query allows you to retrieve documents whose array field is not empty:
POST test/_search
{
"query": {
"script": {
"script": {
"source": "doc.arr.value != null && doc.arr.size() > 0"
}
}
}
}
Note that using >=0
will also return documents with null
arrays.
Detecting fields with an empty array is more complicated and cannot be achieved with a script
query, because there's no different between an field containing an empty array and a field containing null.
A much better and more efficient way to achieve what you want is by adding a field at indexing time with the length of that array (e.g. called arrayLength
below). That way you could use a simple range
query on that field.
POST test/_search
{
"query": {
"range" : {
"arrayLength" : {
"gte" : 0
}
}
}
}
Upvotes: 2