Reputation: 45
I have a set of documents and I want to search for the document which elements/objects has empty array field.
I have tried with the below query , need check for documents where `expenseTypes` field exists but is empty
{
"bool" : {
"must" : [
{
"bool" : {
"should" : [
{
"bool" : {
"must" : [
{
"exists" : {
"field" : "expenseTypes",
"boost" : 1.0
}
},
{
"bool" : {
"must" : [
{
"term" : {
"expenseTypes" : {
"value" : "",
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
which should return the documents which has expenseTypes empty like below
{
"id": "123466",
"expenseTypes": []
}```
Upvotes: 0
Views: 93
Reputation: 45
I tried with below query which is matches the empty array
{
"bool" : {
"must" : [
{
"bool" : {
"should" : [
{
"bool" : {
"must_not" : [
{
"exists" : {
"field" : "expenseTypes",
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
Upvotes: 0
Reputation: 419
You need to use a combination of exists
and term
queries within a bool
query
exists
query ensures that expenseTypes existsterm
query checks if expenseTypes is an empty array{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "expenseTypes"
}
},
{
"term": {
"expenseTypes": {
"value": []
}
}
}
]
}
}
}
Upvotes: 0