Nested Query Returns all Children, not just Children that Match

Hi,

I'm very new to Elastic so please bear with me.

I have an index with a nested type in it. Let's call it an Index of Person, each of which has an array of type Car. So one person can have many cars. A car has a model, color and year. So something like this:

{
Name: 'Aaron',
Age: 33,
Cars: [{model: 'Forester', color: 'red'}, {model: 'Pinto', color: 'yellow'}, {model: 'Camero', color: 'red'}]
}

So I would like to return all Aaron's red cars.

But my query is returning me all Person's with at least 1 red car, and all of their cars, including the yellow Pinto:

{
    query: {nested: {
        path: 'Cars'
        query: {bool: {
            must: {match: {color: 'red'}}
        }}
    }}
}

I don't see a way to tell it to only return the red cars with the nested data type.

Thanks.

Aaron

A data index always has a base data type which behaves like a container for whatever nested / child data types specified.
In your case you have person documents which can contain nested car documents: i.e. a parent-child relationship between the person data type and the car data type.
So when you query your index the hits are of the base data type person.
If it is necessary for your use case to return documents of type car, then you could reverse the relationship: Make an index with base data type car and let car have a reference to person.
But the downside of this approach would be that you have a lot of redundant person data.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.