# Return parents document with or without children JOIN

**URL:** <https://discuss.elastic.co/t/return-parents-document-with-or-without-children-join/106888>\
**Category:** Elasticsearch\
**Created:** [November 8, 2017, 3:09pm UTC](https://discuss.elastic.co/t/return-parents-document-with-or-without-children-join/106888 "2017-11-08T15:09:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![margonar](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@margonar](https://discuss.elastic.co/u/margonar)\
**Post date:** [November 8, 2017, 3:09pm UTC](https://discuss.elastic.co/t/return-parents-document-with-or-without-children-join/106888/1 "2017-11-08T15:09:28Z")

</div>

Hi,  
i have a problem that is similar to [Return parents with or without children](https://discuss.elastic.co/t/return-parents-with-or-without-children/101721) and [Is it possible to return parent document with optional child?](https://discuss.elastic.co/t/is-it-possible-to-return-parent-document-with-optional-child/55939) (but were closed without a solution)

I have an index with a doc type with a join field

```
"myDataJoin": {
    "type": "join",
    "relations": {
        "original": "personal"
    }
}

```

I want to run a query that returns a list of filtered documents defined by a generic query `Q`. In that list I want to have inner\_hits when the document has children but it should also include documents that do not have children.

Right now the best soluonly solution is to repeat the query `Q` twice (once bare `Q`, and the other with `has_child` and `inner_hits`) in a should query:

```
{
  "query": {
    "bool": {
      "should": [
        Q,
        {
          "bool": {
            "must": [
              Q,
              {
                "has_child": {
                  "parent_type": "original",
                  "query": {
                    "match_all": {}
                  },
                  "inner_hits": {}
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

but i would like to avoid this repetition.

@benjes and @einfoman : Did you manage to solve this problem?

---

<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:** [December 6, 2017, 3:09pm UTC](https://discuss.elastic.co/t/return-parents-document-with-or-without-children-join/106888/2 "2017-12-06T15:09:34Z")

</div>

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