title: Sub-queries with "union" in elasticsearch
body: <p>I'm currently busy working on a project in which we chose to use Elasticsearch as the search engine for a classifieds website.</p>
  
  <p>Currently, I have the following business rule: </p>
  
  <blockquote>
    <p>List 25 adverts per page. Of these 25, 10 of the displayed adverts must be "Paid Adverts", and the other 15 must be "Free". All 25 must be relevant to the search performed (i.e. Keywords, Region, Price, Category, etc.)</p>
  </blockquote>
  
  <p>I know I can do this using two seperate queries, but this seems like an immense waste of resources. Is it possible to do "sub-queries" (if you can call them that?) and union these results into a single result set? Somehow only fetching 10 "Paid" adverts and 15 "Free" ones from elasticsearch, in one single query? Assuming of course that there are enough adverts to make this requirement possible. </p>
  
  <p>Thanks for any help!</p>
  
  <p><strong>edit</strong> - Just adding my mapping information for more clarity. </p>
  
  <pre><code>"properties": {
         "advertText": {
            "type": "string",
            "boost": 2,
            "store": true,
            "analyzer": "snowball"
         },
         "canonical": {
            "type": "string",
            "store": true
         },
         "category": {
            "properties": {
               "id": {
                  "type": "string",
                  "store": true
               },
               "name": {
                  "type": "string",
                  "store": true
               },
               "parentCategory": {
                  "type": "string",
                  "store": true
               }
            }
         },
         "contactNumber": {
            "type": "string",
            "index": "not_analyzed",
            "store": true
         },
         "emailAddress": {
            "type": "string",
            "store": true,
            "analyzer": "url_email_analyzer"
         },
         "advertType": {
            "type": "string",
            "index": "not_analyzed"
         },
         ...
  }
  </code></pre>
  
  <p>What I want then is to be able to query this and get 10 results where "advertType": "Paid" <strong>and</strong> 15 where "advertType": "Free"...</p>