I have doc with multiple nested documents. The nested queries work fine but they would still return all the nested objects (i.e. the whole document) even though search query would only match a few nested objects. It does however filter the documents as a whole.
Here is an example:
PUT /demo
{
"mappings": {
"company": {
"properties": {
"employees": {
"type": "nested"
}
}
}
}
}
PUT
/demo/company/1
{
"id": 1,
"name": "Google",
"emp_count": 3,
"employees": [{
"id": 1,
"name": "John",
"address": {
"city": "Mountain View",
"state": "California",
"country": "United States"
}
}]
}
PUT
/demo/company/2
{
"id": 1,
"name": "Facebook",
"emp_count": 3,
"employees": [{
"id": 1,
"name": "Amber",
"address": {
"city": "Bangalore",
"state": "Karnataka",
"country": "India"
}
}, {
"id": 1,
"name": "Adrian",
"address": {
"city": "Palo Alto",
"state": "California",
"country": "United States"
}
}]
}
PUT
/demo/company/3
{
"id": 1,
"name": "Microsoft",
"emp_count": 3,
"employees": [{
"id": 1,
"name": "Aman",
"address": {
"city": "New York",
"state": "New York",
"country": "United States"
}
}]
}
When searching for India
in the address, I should ideally only get Facebook
with one nested object, but I get all the nested objects. How can I filter the nested objects returned?
Example query:
{
"query": {
"function_score":{
"query":{
"nested":{
"path":"employees",
"score_mode":"max",
"query": {
"multi_match":{
"query":"India",
"type":"cross_fields",
"fields":[
"employees.address.city",
"employees.address.country",
"employees.address.state"
]
}
}
}
}
}
}
}
Output of this query is Facebook
with all employees while I only want Amber
.