title: Kibana and groovy scripting
body: <p>I was looking for a way to calculate a ratio on Kibana. After many researches i found this way :
  Using the "JSON Input" feature in a visualisation.</p>
  
  <p>I have all my informations in an index, with 2 types of documents (boots and reboots).
  <strong>I am looking for the script which count the number of documents with the type boots, same for the reboots type then divide the second by the first.</strong>
  It sounds really easy, but i do not find any way to get it after my researches, and i am not used to groovy enough <strong>yet</strong> to do it by myself.</p>
  
  <p>I found many ways to manipulate documents values (doc['mydocname'].values etc), but nothing about the type.</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT : I tried this</p>
  
  <pre><code>{
  "aggs" : {
      "boots_count" : { "value_count" : { "_type" : "boots" } }
  }
  </code></pre>
  
  <p>}
  Which is supposed to count the number of fields (here the field _type) in the index. But when i put it into "JSON Input" in a visualisation, that results in an error :</p>
  
  <p>Error: Request to Elasticsearch failed: {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[BbXJ0O6tRxa_OcyBfYCGJQ][informationbe][0]: SearchParseException[[informationbe][0]: from[-1],size[0]: Parse Failure [Failed to parse source [{\"size\":0,\"aggs\":{\"2\":{\"terms\":{\"field\":\"@sitePoste\",\"size\":5,\"order\":{\"1\":\"desc\"}},\"aggs\":{\"1\":{\"avg\":{\"script\":\"0\",\"lang\":\"expression\",\"ratio\":{\"boots_count\":{\"value_count\":{\"_type\":\"boots\"}}}}}}}}</p>
  
  <p>I am wrong. But where ?</p>
  
  <p>EDIT2 : In other hand, i am trying scripted fields, with something like this using lucene expression :</p>
  
  <pre><code>doc['_type:boots'].count / doc['_type:reboots'].count
  </code></pre>
  
  <p>but it doesnt work more, i am pretty confident about the "doc['_type:boots']" part, i guess the problem is on the "XXX.count" part.</p>
  
  <p>After many attempts, i understand better and better how it works. Default scripted fields scope is on the document, not on the whole index, so i cant do a count action of whole values of the index from documents in it.
  I am looking for a workaround, i'll post it it if find something interesting.</p>