Hi all,
I'm starting to use ES and I struggle with the concept of nested queries. I would like to filter nested comment of my blog post respecting the following condition
- Only nested elements which respect query are returned
- If there are no nested results, the parent must be returned.
The goal is to return all posts without their unvalidated comments.
My mapping
{
"post":{
"mappings":{
"post":{
"properties":{
"id":{
"type":"integer"
},
"comments":{
"type":"nested",
"include_in_parent":true,
"properties":{
"content":{
"type":"text"
},
"is_valid":{
"type":"integer",
}
}
}
}
}
}
}
}
Current documents
{
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_type": "post",
"_id": "1",
"_score": 1,
"_routing": "1",
"_source": {
"id": "1",
"comments": [
{
"id": "1",
"content": "Lorem ipsum dolor sit amet.",
"is_valid" : 1
},
{
"id": "2",
"content": "Lorem ipsum dolor sit amet.",
"is_valid" : 0
},
]
},
{
"id": "2",
"comments": [
{
"id": "3",
"content": "Lorem ipsum dolor sit amet.",
"is_valid" : 0
}
]
},
{
"id": "3",
},
}
]
}
}
Current query
GET _search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "comments",
"query": {
"match" : { "comments.is_valid" : 1 }
},
"inner_hits": {}
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "comments"
}
}
}
}
]
}
}
}
So with this query, I can retrieve post#1 without invalid comments in his inner_hits field and post#3 cause he has no comments.
But I don't know how to get post#2, he has only one invalid comment and this query doesn't match.
How can I fix this?
Thanks