title: Elastic Search: return matching parents with matched/unmatched childs
body: <p>I am using elastic search 7.8.1 and have used parent-child method to index the documents. My requirement is to search both parent and child documents, but return response in a format that parent document is the main document and child document is a field within the parent document. i.e</p>
  <pre><code>1) If the child matches, I wish to return parent &amp; child in a document. I am able to achieve this using has_child and inner_hits.
  2) If the parent matches the query, I wish to return parent and child in a document even if the child does not matches. (Not sure how to achieve this)
  
  # This is the parent child relationship mapping in index
  *curl -X PUT &quot;localhost:9200/my-index-000001?pretty&quot; -H 'Content-Type: application/json' -d'
  {
    &quot;mappings&quot;: {
      &quot;properties&quot;: {
        &quot;my_id&quot;: {
          &quot;type&quot;: &quot;keyword&quot;
        },
        &quot;my_join_field&quot;: { 
          &quot;type&quot;: &quot;join&quot;,
          &quot;relations&quot;: {
            &quot;question&quot;: &quot;answer&quot; 
          }
        }
      }
    }
  }
  '*
      
  Below is the query I am trying to use, but it does not return the child when the parent matches:
  *curl -X POST &quot;localhost:9200/my-index-000001/_search?pretty&quot; -H 'Content-Type: application/json' -d'
  {
    &quot;query&quot;: {
      &quot;bool&quot;: {
        &quot;should&quot;: [
          {
            &quot;has_child&quot;: {
              &quot;type&quot;: &quot;answer&quot;,
              &quot;query&quot;: {
                &quot;match&quot;: {
                  &quot;my_id&quot;: &quot;4&quot;
                }
              },
              &quot;inner_hits&quot;: {
                &quot;size&quot;: 1
              }
            }
          },
          {
            &quot;match&quot;: {
              &quot;my_id&quot;: &quot;1&quot;
            }
          }
        ]
      }
    }
  }'*
  </code></pre>
  <p>#Parent docs curl -X PUT &quot;localhost:9200/my-index-000001/_doc/1?refresh&amp;pretty&quot; -H 'Content-Type: application/json' -d' { &quot;my_id&quot;: &quot;1&quot;, &quot;text&quot;: &quot;This is a question&quot;, &quot;my_join_field&quot;: &quot;question&quot; } ' curl -X PUT &quot;localhost:9200/my-index-000001/_doc/2?refresh&amp;pretty&quot; -H 'Content-Type: application/json' -d' { &quot;my_id&quot;: &quot;2&quot;, &quot;text&quot;: &quot;This is another question&quot;, &quot;my_join_field&quot;: &quot;question&quot; } '</p>
  <p>#Child docs curl -X PUT &quot;localhost:9200/my-index-000001/_doc/3?routing=1&amp;refresh&amp;pretty&quot; -H 'Content-Type: application/json' -d' { &quot;my_id&quot;: &quot;3&quot;, &quot;text&quot;: &quot;This is an answer&quot;, &quot;my_join_field&quot;: { &quot;name&quot;: &quot;answer&quot;, &quot;parent&quot;: &quot;1&quot; } } ' curl -X PUT &quot;localhost:9200/my-index-000001/_doc/4?routing=1&amp;refresh&amp;pretty&quot; -H 'Content-Type: application/json' -d' { &quot;my_id&quot;: &quot;4&quot;, &quot;text&quot;: &quot;This is another answer&quot;, &quot;my_join_field&quot;: { &quot;name&quot;: &quot;answer&quot;, &quot;parent&quot;: &quot;1&quot; } } '</p>
  <p>How can I search both parent and child, but return child as a field in parent doc. Thanks in advance.</p>