# How to query a computed value from a document

**URL:** https://discuss.elastic.co/t/how-to-query-a-computed-value-from-a-document/97116
**Category:** Elasticsearch
**Created:** [August 15, 2017, 2:50pm UTC](https://discuss.elastic.co/t/how-to-query-a-computed-value-from-a-document/97116 "2017-08-15T14:50:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ajay\_bh111](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ajay\_bh111](https://discuss.elastic.co/u/ajay_bh111)
#### Post date: [August 15, 2017, 2:50pm UTC](https://discuss.elastic.co/t/how-to-query-a-computed-value-from-a-document/97116/1 "2017-08-15T14:50:05Z")

</div>

Hi,  
If I want to query a computed value from "a specific document" how this should be written for Kibana or Lucene or in painless script ?

For example, If I want to get delete\_time\_per\_ document = (indexing\_delete\_time\_in\_millis / indexing\_delete\_total) from the two fields in the same index mapping below

...  
"indexing\_delete\_time\_in\_millis" : {  
"type" : "long"  
},  
"indexing\_delete\_total" : {  
"type" : "long"  
},  
....

Thanks

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [August 18, 2017, 9:38am UTC](https://discuss.elastic.co/t/how-to-query-a-computed-value-from-a-document/97116/2 "2017-08-18T09:38:00Z")

</div>

Kibana has a concept of "scripted fields". These are fields that are calculated dynamically, and you can use them in visualizations just like any other fields. Check out the docs here: [https://www.elastic.co/guide/en/kibana/current/scripted-fields.html](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html)

One thing you can't do though is query these scripted fields. If you want to do that, you will have to use a `script` query. For example, the following query will match all docs for which (indexing\_delete\_time\_in\_millis / indexing\_delete\_total) is larger than 1.0:

```
GET my_index/_search
{
  "query": {
    "script": {
      "script": {
        "inline": "doc['indexing_delete_time_in_millis'].value / doc['indexing_delete_total'].value > 1",
        "lang": "painless"
      }
    }
  }
}

```

Be careful with script queries though. They are computationally expensive and can result in errors (for example for document in which the indexing\_delete\_time\_in\_millis and indexing\_delete\_total do not exist, or for documents for which indexing\_delete\_total is 0.0).

A better way to solve this is to add the actual `delete_time_per_ document` field to your documents when you index those.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 15, 2017, 9:38am UTC](https://discuss.elastic.co/t/how-to-query-a-computed-value-from-a-document/97116/3 "2017-09-15T09:38:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
