title: Elasticsearch sort by single nested document key in array
body: <p>I have documents which look like this (here are two examples):</p>
  
  <pre><code>{
      "id": 1234,
      "title": "the title",
      "body": "the body",
      "examples": [
          {
              "evidence_source": "friend",
              "source_score": 15
          },
          {
              "evidence_source": "parent",
              "source_score": 12
          }
      ]
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
      "id": 6346,
      "title": "new title",
      "body": "lots of content",
      "examples": [
          {
              "evidence_source": "friend",
              "source_score": 10
          },
          {
              "evidence_source": "parent",
              "source_score": 27
          },
          {
              "evidence_source": "child",
              "source_score": 4
          }
      ]
  }
  </code></pre>
  
  <p>The format of the sub-documents in the <code>examples</code> array will always have an <code>evidence_source</code> and a <code>source_score</code> but there will be a variable amount of these sub-documents, each with different <code>evidence_source</code> values.</p>
  
  <p>I am wondering if it is possible to sort documents with this format based on one of the <code>source_score</code> values matched to a specific <code>evidence_source</code> value. I'd really like to be able to do this:</p>
  
  <ul>
  <li>Sort documents by <code>source_score</code> descending where the related <code>evidence_source</code> is <code>friend</code>. The resulting ordering of the document <code>id</code>s would be 1234,6346.</li>
  <li>Sort documents by <code>source_score</code> descending where the related <code>evidence_source</code> is <code>parent</code>. The resulting ordering of the document <code>id</code>s would be 6346,1234.</li>
  </ul>
  
  <p>The closest results that I'm come up with for doing something like this are <a href="https://stackoverflow.com/questions/6465852/how-to-search-by-array-elements-in-elasticsearch">1</a> and <a href="http://elasticsearch-users.115913.n3.nabble.com/Query-Syntax-for-arrays-and-nested-objects-td212582.html" rel="nofollow noreferrer">2</a> but I don't believe that they get at exactly what I want to do.</p>
  
  <p>Any ideas about how I might go about this? I've contemplated some ideas based on indexing these <code>examples</code> sub-documents separately, but I'm fairly new to elasticsearch and so am looking for some advice on a how to achieve my goal in the most straightforward manner (which may be a pipe-dream...)</p>
  
  <p><strong>Update</strong>: A post on the <a href="https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/ldRIB0YYbB4/discussion" rel="nofollow noreferrer">elasticsearch mailing list</a> seems to indicate that this is NOT possible, but I'm wondering if someone else here has any different ideas!</p>