Search code examples
elasticsearchkibanakibana-4

how to fill a scripted field value by condition in kibana


i am using Kibana 4 and my document contains two integer fields called: 'x' & 'y'. i would like to create a scripted field in Kibana returning the division value of 'x' by 'y' if 'y'<> 0. else: return the value of 'x'.

i have tried to add this script to a new screnter code hereipted field: doc['x'].value > 0 ? doc['x'].value/doc['y'].value : doc['x'].value;
but got a parsing error when trying to visualize it:

Error: Request to Elasticsearch failed: {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures

how can i create a scripted field with condition in Kibana, step by step?


Solution

  • What you are seeing is not a parsing error, shardFailures just means that the underlying Elasticsearch is not ready yet. When starting Kibana/Elasticsearch, make sure your ES cluster is ready before diving into Kibana, i.e. run curl -XGET localhost:9200/_cluster/health and in the response, you should see something similar to this:

    {
        cluster_name: your_cluster_name
        status: yellow           <----- this must be either yellow or green
        timed_out: false
        number_of_nodes: 2
        number_of_data_nodes: 2
        active_primary_shards: 227
        active_shards: 454
        relocating_shards: 0     <----- this must be 0
        initializing_shards: 0   <----- this must be 0
        unassigned_shards: 25
    }
    

    As for your script, it is written correctly, however the condition you mentioned is not correct since you wanted y <> 0 and not x > 0, so it should be

     doc['y'].value != 0 ? doc['x'].value / doc['y'].value : doc['x'].value
    

    Please give it a try