title: How to get a count for each type of an index in elasticsearch?
body: <p>I have an index and want to get a count for the entries in every type of one particular index in elasticsearch, but might not know the types ahead of time.</p>
  
  <p>So, for example, the index is</p>
  
  <pre><code>/events
  </code></pre>
  
  <p>and the types could be</p>
  
  <pre><code>/events/type1
  /events/type2
  ...
  /events/typeN
  </code></pre>
  
  <p>And I'd like to query the index and say "Give me the count of each of the types under index events", so maybe a result set like</p>
  
  <pre><code>/events/type1 : 40
  /events/type2: 20
  /events/typeN: 10
  </code></pre>
  
  <p>where /events/_count would give me</p>
  
  <pre><code>/events: 70
  </code></pre>
  
  <p><strong>Edit</strong>:</p>
  
  <p>imotov's answer is great. I'm having trouble figuring how to get it working in JavaScript/Ajax easily though. I have something like this right now:</p>
  
  <pre><code>$.ajax({
  type: 'GET',
  url: 'http://localhost:9200/events/_search?search_type=count',
  data: '{ "facets" : { "count_by_type" : { "terms" : { "field": "_type" }}}}',
  success: function(text) {
      console.log(text);
  }
  )}'
  </code></pre>
  
  <p>But am only getting the total count of elements in the ES, the facets portion of the answer seems to be missing.</p>