I have been working on migrating from elastic search 2.2 to 5.x and re-writing the queries.
There is one place where I have hit a road block and would really appreciate help.
Here is the scenario:
We have an index which contains people information like name, gender etc. It contains nested objects like addresses, as 1 person can have multiple address or none at all.
The query I am trying to create is to get all the people who have an address.state along with those who do not have an address at all.
I am able to get separately everyone who does not have the nested address by using “must_not” and exists:
{
"query": {
"bool":{
"must_not" :{"exists":{"field":"address.state"}}
}
}
}
And get people who have an address.State = WA or CA by a nested filter query
{
"query": {
"bool": {
"filter":[
{"nested":{
"path":" address ",
"query":{
"bool":
{
"filter":[
{"terms":{" address.state ":["WA","CA"]}}
]
}
}
}
}
]
}
}
}
But not able to join both into 1 query, to get all who do not have the nested address or have an address in WA/CA
{
"query": {
"bool":{
"filter":[
{"nested":{"path":"test-degree",
"query":{
"bool":
{"filter":[
{"terms":{" address.state ":["WA","CA"]}},
]
, "must_not" :{"exists":{"field":" address.state"}}
}
}
}
}
]
}
}
}