Given doc: books
[
{
name: "book a",
authors:
[
{
name: "John",
}
{
name: "Paul"
}
]
},
{
name: "book b",
authors:
[
{
name: "Paul"
}
]
}
]
Goal: Return the book which match name
= book a
AND match one of the author name with authors.name
= John
.
What I tried:
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"name": {
"value": "book a"
}
}
},
{
"bool": {
"should": [
{
"term": {
"authors.name": {
"value": "John"
}
}
}
]
}
}
]
}
}
]
}
}
]
mapping name: text authors: dynamic
CodePudding user response:
You can use below query:
Please note that, if you are using dynemic mapping of elasticsearch then used name.keyword
and authors.name.keyword
. otherwise use name
and authors.name
if you have specified your custom mapping.
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "book a"
}
},
{
"term": {
"authors.name.keyword": {
"value": "John"
}
}
}
]
}
}
}