Parent Child Bool Search problem


(Chris K) #1

I have many children per parent document. For example, a parent is a book and each contributor towards the book is a separate child.

One of my successful queries is to find all books that have a certain contributor name. But what doesn't work is trying to find books that have both contributor name A and contributor name B. What I think is happening is ES is asking each child: do you have contributor name A and contributor name B? Which obviously is always false because each child can only be a single contributor. Does anyone know how to write the query I want in this parent/child relationship?

I had this working back when I tried a nested document model because the children field (of a book model) had all the contributors in a single array/list.

Having a single child with all the contributors in a list not an option because "contributors" are constantly coming in (ok ok, so I'm not really dealing with books & contributors) and really defeats the purpose of parent/child... right?

UPDATE:
The key was to format the query like so...
query:{
bool:{
must:[
{
has_child:{
type:"contributors",
query:{
bool:{
must:[
{
term:{
contributorName:"aaa"
}
}
]
}
}
}
},
{
has_child:{
type:"contributors",
query:{
bool:{
must:[
{
term:{
contributorName:"bbb"
}
}
]
}
}
}
}
]
}
}


(system) #2