title: Elasticsearch Sorting Tiebreakers
body: <p>Say I am creating a search engine for a photo sharing social network and the documents of the site have the following schema</p>
  
  <pre><code>{
      "id": 123456
      "name": "Foo",
      "num_followers": 123456,
      "num_photos": 123456
  }
  </code></pre>
  
  <p>I would like my search results to satisfy the following requirements:</p>
  
  <ul>
  <li>Only have results where the search query strings matches the "name" field in the document</li>
  <li>Rank the search results by number of followers descending</li>
  <li>In the case where multiple customers have the same number of followers, rank by number of photos descending</li>
  </ul>
  
  <p>For example, say I have the following documents in my index:</p>
  
  <pre><code>{
      "id": 1,
      "name": "Customer",
      "num_followers": 3,
      "num_photos": 27
  }
  {
      "id": 2,
      "name": "Customer",
      "num_followers": 25,
      "num_photos": 1
  }
  {
      "id": 3,
      "name": "Customer",
      "num_followers": 8,
      "num_photos": 2
  }
  {
      "id": 4,
      "name": "Customer",
      "num_followers": 8,
      "num_photos": 5
  }
  {
      "id": 5,
      "name": "FooBar",
      "num_followers": 10000,
      "num_photos": 20000
  }
  </code></pre>
  
  <p>If I search "Customer" in the search bar of the site, the ES hits should be in the following order:</p>
  
  <pre><code>{
      "id": 2,
      "name": "Customer",
      "num_followers": 25,
      "num_photos": 1
  }
  {
      "id": 4,
      "name": "Customer",
      "num_followers": 8,
      "num_photos": 5
  }
  {
      "id": 3,
      "name": "Customer",
      "num_followers": 8,
      "num_photos": 2
  }
  {
      "id": 1,
      "name": "Customer",
      "num_followers": 3,
      "num_photos": 27
  }
  </code></pre>
  
  <p>I'm assuming I will need to perform some sort of compact query to create this "tiebreaker" logic. What clauses should I be using? If anyone had an example of something similar that would be amazing. Thanks in advance.</p>