# Nested query and empty results

**URL:** <https://discuss.elastic.co/t/nested-query-and-empty-results/91665>\
**Category:** Elasticsearch\
**Created:** [July 3, 2017, 3:44pm UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665 "2017-07-03T15:44:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![airpons1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airpons1/32/19727_2.png) [@airpons1](https://discuss.elastic.co/u/airpons1)\
**Post date:** [July 3, 2017, 3:44pm UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665/1 "2017-07-03T15:44:59Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [July 3, 2017, 4:00pm UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665/2 "2017-07-03T16:00:20Z")

</div>

What is it exactly that you want the query to return? All posts that have at least 1 invalid comment or no comments at all??

---

<div class="post-metadata">

**Author:** ![airpons1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airpons1/32/19727_2.png) [@airpons1](https://discuss.elastic.co/u/airpons1)\
**Post date:** [July 3, 2017, 4:11pm UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665/3 "2017-07-03T16:11:45Z")

</div>

Ideally this

```
{
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_source": {
          "id": "1",
          "comments": [
            {
              "id": "1",
              "content": "Lorem ipsum dolor sit amet",
              "is_valid" : 1
            },
          ]
        }
      },
      {
        "_source": {
          "id": "2"
        }
      },
      {
        "_source": {
          "id": "3",
      },
    ]
  }
}

```

I want to get all posts, regardless they have comments or not. And if they had, only valid ones will be returned.

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [July 13, 2017, 8:59pm UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665/4 "2017-07-13T20:59:16Z")

</div>

Sorry for the late reply. But i think what you need to do is use two nested queries:

```
GET /indexname/_search
{
  "size":0,
  "query":{
    "bool":{
      "should": [
        {
          "nested":{
            #condition 1
          }
        },
        {
          "nested":{
            #condition 2
          }
        }
      ]
    }
  }
}

```

I am not 100% sure why the query format you used doesn't work, maybe someone on the Elastic Team can reply to the thread, I tried using your format and it surely doesn't work. But the one I provided works for me, let me know if this solves your issue.

Sorry for the late reply.

Thanks.

---

<div class="post-metadata">

**Author:** ![airpons1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airpons1/32/19727_2.png) [@airpons1](https://discuss.elastic.co/u/airpons1)\
**Post date:** [July 18, 2017, 3:29pm UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665/5 "2017-07-18T15:29:41Z")

</div>

Hi,  
Thx for your answer

I tried with 2 nested queries, But I'm not able to get **post#2** without his invalid comments, or I miss something.

Basically, the question is, can nested query filter doc's children without impact their parents?  
If my nested query returns no result, can I still get the parent?

Maybe I'm trying to achieve something that ES is not designed for. ^^

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [July 19, 2017, 1:54am UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665/6 "2017-07-19T01:54:02Z")

</div>

So you have 3 different kinds of nested documents that you want the query to return. Have you tried using 3 nested queries inside the should block(each nested block would return one kind of document that you want the query to return)...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 16, 2017, 1:54am UTC](https://discuss.elastic.co/t/nested-query-and-empty-results/91665/7 "2017-08-16T01:54:03Z")

</div>

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