I have a filter, where I check some conditions in the nested document "employees" in a bigger document called company, But I want to run this filter, only if "employees" object exists, as some of the document may not have that nested document at all. So I added , {"exists": {"field": "employees"}}
but this doesn't seem to work. Any idea what change I should make to get it work?
In what way is it broken?
False positives or false negatives? Either way I don't think you need the "exists" clause here.
The data model seems unusual here. If I read it right you have a company doc with a nested array of employee objects each of which seem to have a min_age and a max_age value rather than recording an actual age of the employee or a birthdate?
FYI if that is the case and age ranges are what you record then this new feature coming in 5.2 may be of interest: Numeric and Date Ranges...Just Another Brick in the Wall | Elastic Blog
@Mark_Harwood thanks for the suggestion. Regarding "data model seems unusual" - I have intentionally did not put my whole data model and actual field names here. So let's ignore that part.
What I am looking for is - if company document doesn't have employee document, then return that company document, but if that document has employee nested document, then run the filter. Right now the issue is if a document doesn't have employees nested document, then that document do not get returned at all, because it tries to run the filter on a non-existing employees document. So what change I should make in order to ignore filter, if employees does not exists. It is something like this if employee.exists { run filter } else { return company doc} .. make sense?
@Mark_Harwood First of all, I appreciate your time to write up the solution end to end with an example. You made my day. Indeed your solution works, but you missed one key point. As I mentioned in my question that I would also have other filters on root document they would be must.
{ #....other filter here based on root document, not on nested employee document }
So suppose in your example, I added few more documents as follows:
POST test/company/3
{
"name":"Some employees",
"employees":[
{"age":40}
]
}
POST test/company/4
{
"name":"Some employees",
"employees":[
{"age":30}
]
}
I am interested only to grab documents where company name condition matches i.e. company.name == "Some employees". Since You are using should on the top level, then it won't be possible. Because if I add this condition in the query, then it will also pull document where company name == "no employees". Got complex
If you map to a sql analogy so this is what it would be select * from company where (company.employee.age > 10 or company.employee = null) and company.name="some employee"
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.