title: How to aggregate on number of returned results in ElasticSearch?
body: <p>FYI, I'm using ES 1.7.2 in a Mac.</p>
  
  <p>I've been trying to get the aggregations on a result set but what I'm getting is the aggregations on all the records.</p>
  
  <p>Let's say I want to return 200 vehicles that are Ford Focus SE and from those 200, I want to know the Trims of all them and how many vehicles each Trim has. So basically a count for those Trims but also get the 200 results back.</p>
  
  <p>Here's what I have so far(I'm using Sense/Marvel...Easier to test):</p>
  
  <pre><code>GET jdbc/_search
  {
    "size": 200, 
    "query": {
      "filtered": {
        "filter": {
          "bool": {
            "must": [
              { "term": { "listing.model": "Focus"   }},
              { "term": { "listing.make": "Ford" }}
            ]
          }
        }
      }
    },
    "aggs": {
      "trims": {
        "terms": { "field": "listing.trim" }
      },
      "trim_SE": {
        "filter": {
          "term": { "listing.trim": "SE" }
        },
      "aggs": {
        "trims": {
          "terms": { "field": "listing.trim"}
          }
        }
      }
    },
    "post_filter": { 
      "term": { "listing.trim": "SE" }
    }
  }
  </code></pre>
  
  <p>So I do get 20 results back like so:</p>
  
  <pre><code>{
     "took": 18,
     "timed_out": false,
     "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
     },
     "hits": {
        "total": 10338,
        "max_score": 1,
        "hits": [
           {
              "_index": "myindex",
              "_type": "mytype",
              "_id": "472",
              "_score": 1,
              "_source": {
                 "listing": {
                    "vin": "111111111111",
                    "year": 2013,
                    "make": "Ford",
                    "model": "Focus",
                    "trim": "SE",
                 }
              }
           },
           {...}
        ]
     }
     "aggregations": {
        "trims": {
           "doc_count_error_upper_bound": 0,
           "sum_other_doc_count": 30,
           "buckets": [
              {
                 "key": "SE",
                 "doc_count": 10338
              },
              {
                 "key": "SEL",
                 "doc_count": 1000
                  },
              {
                 "key": "Titanium",
                 "doc_count": 874
              },
              {
                 "key": "SES",
                 "doc_count": 585
              },
              {
                 "key": "S",
                 "doc_count": 554
              },
              {
                 "key": "",
                 "doc_count": 447
              },
              {
                 "key": "ST",
                 "doc_count": 339
              },
              {
                 "key": "ZTS",
                 "doc_count": 60
              },
              {
                 "key": "LX",
                 "doc_count": 56
              },
              {
                 "key": "Electric",
                 "doc_count": 18
              }
           ]
        },
        "trim_SE": {
           "doc_count": 10338,
           "trims": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                 {
                    "key": "SE",
                    "doc_count": 10338
                 }
              ]
           }
        }
     }
  }
  </code></pre>
  
  <p>But as you can see, the Trims show up with a bigger count than the 200, which means it's doing the aggregations on all the vehicles.</p>
  
  <p>I need some help and I can't find anything that actually makes this work.</p>
  
  <p>Thanks for the help in advance!</p>