Reputation: 28573
I would like to run a query to see how many documents have an empty object stored as their value. For example it would return a document like:
"hits": [
{
"_source": {
"otherfield": "abc",
"somefield": {}
}
}
]
But not either no field / the field with an undefined value, or the field with an object containing attributes:
"hits": [
{
"_source": {
"otherfield": "abc",
// <-- note no "somefield"
}
},
{
"_source": {
"otherfield": "abc",
"somefield": { "field1": "value1" }
}
}
]
But the query I have will also return documents where the field is an object with attributes such as "somefield": { "field1": "value1" }
GET /documents/_search
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "somefield.field1"
}
},
]
"should": [
{
"exists": {
"field": "somefield"
}
}
],
"minimum_should_match": 2
}
}
}
Using Elasticsearch 5.4
Upvotes: 2
Views: 2259
Reputation: 2006
The following query should be enough to find all documents with empty somefield
field:
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "somefield"
}
}
}
}
}
While your query is a bit confusing for me. First you are trying to find any documents where somefield.field1
does not exist. Then you combine content of must_not
clause with mutually exclusive content of should
clause that filters documents with non-empty somefield
. Actually should
clause is translated into
"should": [{
"exists": {"field": "somefield.field1"}
}]
So your query should not match neither documents with somefield: {}
nor documents with somefield: {field1: value1}
.
Upvotes: 1