title: Elasticsearch nested query and order by "hits"
body: <p>Using Elasticsearch, I'm trying to search nested data and return the documents with the most "hits."</p>
  
  <p>Relevant Example Data</p>
  
  <pre><code>POST myrecipes/recipe
  {
      "title": "Potato Salad",
      "ingredients": [
          {"name":"potato"},
          {"name":"mayonaise"},
          {"name":"mustard"},
          {"name":"bacon"},
          {"name":"onion"},
          {"name":"parsley"}  
      ]
  }
  
  POST myrecipes/recipe
  {
      "title": "Seared Scallops",
      "ingredients": [
          {"name":"scallops"},
          {"name":"butter"},
          {"name":"bacon"}
      ]
  }
  
  POST myrecipes/recipe
  {
      "title": "Tuna melt",
      "ingredients": [
          {"name":"tuna"},
          {"name":"onion"},
          {"name":"butter"},
          {"name":"bacon"},
          {"name":"mayonaise"},
          {"name":"lettuce"},
          {"name":"tomato"},
          {"name":"bread"}
      ]
  }
  </code></pre>
  
  <p>My Index</p>
  
  <pre><code>PUT myrecipes
  {
    "mappings": {
      "recipe": {
        "properties": {
          "ingredients": {
            "type": "nested",
            "include_in_parent": true,
            "properties": {
              "name": {
                  "type": "string",
                  "fields": {
                      "untouched": {
                          "type": "string",
                          "index": "not_analyzed"
                      }
                  }              
              }            
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>My Search:</p>
  
  <pre><code>POST  myrecipes/recipe/_search
  {
      "query": {
          "nested": {
            "path": "ingredients",
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "ingredients.name": {
                        "query": "bacon"
                      }
                    }
                  },
                  {
                    "match": {
                      "ingredients.name": {
                        "query": "butter"
                      }
                    }
                  }
                ]
              }
            }
          }
      }
  }
  </code></pre>
  
  <p>The query returns</p>
  
  <p>"Tuna melt", "Potato Salad", "Seared Scallops"</p>
  
  <p>I'm hoping for:</p>
  
  <p>"Seared Scallops", "Tuna melt", "Potato Salad"</p>
  
  <p>Since there are two "hits" on "Seared scallops" and "Tuna Melt" (bacon and butter), and only one "hit" on "Potato Salad" (bacon).</p>