title: Elasticsearch Parent/Child. How to query and filter on Parent and sort on Child field
body: <p>So far I can Filter Parents with Children and sort the response on a Child field, works like a charm. See bellow. </p>
  
  <pre><code>GET /parent_child_meta_poc/paren_doc/_search
  {
    "query": {
      "has_child": {
        "inner_hits": {},
        "type": "most_read_doc",
        "query": {
          "function_score": {
            "functions": [
              {
                "field_value_factor": {
                  "factor": 1,
                  "field": "read_count"
                }
              }
            ]
          }
        },
        "score_mode": "avg"
      }
    }
  }
  </code></pre>
  
  <p>Source: <a href="https://gist.github.com/matthiasg/2b8748cfdf919ce77c3fd0438613cee6" rel="noreferrer">Matts gist</a></p>
  
  <p>Example child mapping:</p>
  
  <pre><code>{
    "mappings": {
      "most_read_doc": {
        "_parent": {
          "type": "paren_doc"
        },
        "properties": {
          "parent_uri": {
            "type": "string"
          },
          "read_count": {
            "type": "long"
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>However, the limitation comes in with requirements such as :  </p>
  
  <p>Give me all documents that fit this search criteria, return only documents that have children of "most_read_doc" and sort the returned parent documents on "most_read_doc.read_count"</p>
  
  <p>Essentially I would like to:</p>
  
  <ol>
  <li>query against the parent document. </li>
  <li>return only the Parents results that have a child of "most_read_doc".</li>
  <li>sorted on "most_read_doc.read_count".</li>
  </ol>
  
  <p>Parent/Child seem to have fallen short on 1. With any sort of query resulting to "nested: QueryParsingException...No query registered for [functions]]" Exception.  No official Elasticsearch solution for this yet based on my search.  And struggling to find a working work around this thus so far... </p>
  
  <p><em>Other similar:</em></p>
  
  <ul>
  <li><a href="http://elasticsearch-users.115913.n3.nabble.com/search-child-by-parent-field-td3717937.html" rel="noreferrer">Search child by parent field, elastic forum</a>. 2012, not supported. -Kimchy</li>
  <li><a href="https://github.com/elastic/elasticsearch/issues/2917" rel="noreferrer">Sorting based on parent/child</a>.  nested solution. not for my need, see bellow</li>
  </ul>
  
  <p><strong>Note</strong>: I can't use nested documents/merge the "read_count" with the parent as I have two systems applying updates to this index.</p>
  
  <ol>
  <li>system <strong><em>x</em></strong> insert the parent documents to the index, overriding any previous doc with the given ID, no partial update capability here.</li>
  <li>system <strong><em>y</em></strong> grab Google analytics meta data for most read doc and inserts a child document with association to the parent via the ID. This way system <em>x</em> can carry on inserting/replacement parent doc without affecting children.</li>
  </ol>