title: Elasticsearch sort parent by inner hits doc count
body: <p>Let's say I am indexing into Elasticsearch a bunch of <code>Product</code>s and <code>Store</code>s in which the product is available. For example, a document looks something like:</p>
  
  <pre><code>{
    name: "iPhone 6s",
    price: 600.0,
    stores: [
      {
        name: "Apple Store Union Square",
        location: "San Francisco, CA"
      },
      {
        name: "Target Cupertino",
        location: "Cupertino, CA"
      },
      {
        name: "Apple Store 5th Avenue",
        location: "New York, NY"
      }
      ...
    ]
  }
  </code></pre>
  
  <p>and using the <code>nested</code> type, the mappings will be:</p>
  
  <pre><code>"mappings" : {
    "product" : {
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "price" : {
          "type" : "float"
        },
        "stores" : {
          "type" : "nested",
          "properties" : {
            "name" : {
              "type" : "string"
            },
            "location" : {
              "type" : "string"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I want to create a query to find all the products that are available in certain location, say "CA", and then sort by the number of stores matched. I know Elasticsearch has a <a href="https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-request-inner-hits.html" rel="nofollow">inner hit</a> feature which allows me to find hits in the nested <code>Store</code> documents, but is sorting <code>Product</code> based on the <code>doc_count</code> of the inner hit possible? And to extend the question further, is sorting the parent documents based on some inner aggregation possible? Thanks in advance.</p>