Ah I see what you are saying. My bad, I did not see the nested object type.
So, for nested objects, regular queries do not work as internally nested objects are indexed as separate documents. So whatever query we do, it runs on each object as a separated document.
Since at a given time, each object has only one publish_date
value, even if we need AND, we cant do must
. It would need to be should
. Even then, as a result of hits, you will get back the entire document, that in turn will contain nested objects, that do not match the query since multiple nested objected (books) exist under same document (authors). The solution there is to leverage inner_hits
, that gives access to the matching nested objects within the document.
Having said that, this is the query that worked for me.
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "books",
"query": {
"bool": {
"should": [
{
"term": {
"books.publish_date": "1838"
}
},
{
"term": {
"books.publish_date": "1861"
}
}
]
}
},
"inner_hits": {
"highlight": {
"fields": {
"books.author_name": {}
}
}
}
}
}
]
}
}
}
It returns ...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.6931472,
"hits": [
{
"_index": "my_index2",
"_type": "doc1",
"_id": "2",
"_score": 0.6931472,
"_source": {
"author_name": "Anthony Trollope",
"author_birth": "5/24/1815",
"books": [
{
"book_name": "Doctor Thorne",
"book_type": "novel",
"publish_date": "1858"
},
{
"book_name": "Orley Farm",
"book_type": "novel",
"publish_date": "1861"
}
]
},
"inner_hits": {
"books": {
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_nested": {
"field": "books",
"offset": 1
},
"_score": 0.6931472,
"_source": {
"book_name": "Orley Farm",
"book_type": "novel",
"publish_date": "1861"
}
}
]
}
}
}
},
{
"_index": "my_index2",
"_type": "doc1",
"_id": "1",
"_score": 0.6931472,
"_source": {
"author_name": "Charles Dickens",
"author_birth": "2/7/1812",
"books": [
{
"book_name": "Oliver Twist",
"book_type": "novel",
"publish_date": "1838"
},
{
"book_name": "Great Expectations",
"book_type": "novel",
"publish_date": "1861"
}
]
},
"inner_hits": {
"books": {
"hits": {
"total": 2,
"max_score": 0.6931472,
"hits": [
{
"_nested": {
"field": "books",
"offset": 1
},
"_score": 0.6931472,
"_source": {
"book_name": "Great Expectations",
"book_type": "novel",
"publish_date": "1861"
}
},
{
"_nested": {
"field": "books",
"offset": 0
},
"_score": 0.6931472,
"_source": {
"book_name": "Oliver Twist",
"book_type": "novel",
"publish_date": "1838"
}
}
]
}
}
}
}
]
}
}