title: fetch perticular number of documents satisfying multiple conditions - Elasticsearch
body: <p>I have a Elasticsearch index for an information of fruits as below</p>
  
  <pre><code>GET fruits/fruits_data/_search
  
  [{ id: 1,
     name: apple},
   { id: 2,
     name: mango},
   { id: 3,
     name: apple},
   { id: 4,
     name: banana},
   { id: 5,
     name: apple},
   { id: 6,
     name: mango},
   { id: 7,
     name: pineapple},
   { id: 8,
     name: jackfruit}]
  </code></pre>
  
  <p>Now I need to fetch <code>7</code> fruits as per the priority (below):</p>
  
  <pre><code>{"apple": 3, "banana": 3, "mango": 2, "guava": 2, "pineapple": 1, "jackfruit": 1}
  </code></pre>
  
  <p>Here the <code>key</code> indicates the fruit to be fetched and <code>value</code>indicates the maximum number of the document to be fetched.
  This means I need to fetch maximum <code>3 apple, 3 banana and 1 mango</code> and I can ignore the others in priority hash when I have required number of fruits. But here I have only <code>1 banana</code> in my ES index so I need to fetch maximum <code>3 apple, 1 banana, 2 mango and 1 pineapple</code> (Since <code>guava</code> is not present in index we need to ignore it. 
  Is there a way to fetch fruits like this in ES in a single query. I don't want to use multiple queries.
  Thanks</p>