Return parents document with or without children JOIN

Hi,
i have a problem that is similar to Return parents with or without children and Is it possible to return parent document with optional child? (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?

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