title: Elastic Search filter with aggregate like Max or Min
body: <p>I have simple documents with a scheduleId. I would like to get the count of documents for the most recent ScheduleId. Assuming Max ScheduleId is the most recent, how would we write that query. I have been searching and reading for few hours and could get it to work.</p>
  
  <pre><code>{
    "aggs": {
      "max_schedule": {
        "max": {
          "field": "ScheduleId"
        }
      }
    }
  }
  </code></pre>
  
  <p>That is getting me the Max ScheduleId and the total count of documents out side of that aggregate. </p>
  
  <p>I would appreciate if someone could help me on how take this aggregate value and apply it as a filter (like a sub query in SQL!).</p>