How to query a computed value from a document


(ajay.bh111) #1

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


(Abdon Pijpelink) #2

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

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.


(system) #3

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