# How to use scripted fields in vega/vega-lite visualizations?

**URL:** https://discuss.elastic.co/t/how-to-use-scripted-fields-in-vega-vega-lite-visualizations/150677
**Category:** Kibana
**Created:** [October 2, 2018, 10:38am UTC](https://discuss.elastic.co/t/how-to-use-scripted-fields-in-vega-vega-lite-visualizations/150677 "2018-10-02T10:38:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tonyskulk](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@tonyskulk](https://discuss.elastic.co/u/tonyskulk)
#### Post date: [October 2, 2018, 10:38am UTC](https://discuss.elastic.co/t/how-to-use-scripted-fields-in-vega-vega-lite-visualizations/150677/1 "2018-10-02T10:38:37Z")

</div>

Simple question, but I could not find any documentation about that. On **Discover** view I can see scripted field values but on my vega visualizations I can not.

This is my current spec:

```
{
  $schema: "https://vega.github.io/schema/vega-lite/v2.json"
  mark: line
  data: {
    url: {
      %context%: true
      %timefield%: timestamp
      index: gaptrader.heartbeat
      body: {
        size: 10000,
        // _source: ['timestamp', 'lastSignal', 'lastSignalSeconds']
      }
    }
    format: { property: "hits.hits" }
  }
  transform: [
    {
      calculate: "toDate(datum._source['timestamp'])"
      as: "time"
    }
  ]
  encoding: {
    x: {
      field: time
      type: temporal
      axis: { title: false }
    }
    y: {
      field: lastSignalSeconds
      type: quantitative
      axis: { title: false }
    }
  }
}

```

and I want my (already created) scripted field **lastSignalSeconds** to be shown on my chart. I tried many things like

lastSignalSeconds  
datum.lastSignalSeconds  
\_source.lastSignalSeconds  
\_source['lastSignalSeconds']

but nothing seems to work. What is the correct syntax accessing scripted fields?

---

<div class="post-metadata">

### Author: ![thomasneirynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomasneirynck/32/23313_2.png) [@thomasneirynck](https://discuss.elastic.co/u/thomasneirynck)
#### Post date: [October 2, 2018, 2:52pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-fields-in-vega-vega-lite-visualizations/150677/2 "2018-10-02T14:52:51Z")

</div>

hi @tonyskulk,

Scripted fields are a Kibana-concept and part of the Kibana index-pattern. When creating Vega-visualizations you are not actually querying through the index pattern-framework of Kibana. Rather, you are submitting a raw Elasticsearch query that bypasses the Kibana index-pattern configurations.

You will have to write the raw ES-query. So you will have to define the script-field again in that Elasticsearch-query (`body` property). For more info, see [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html).

---

<div class="post-metadata">

### Author: ![tonyskulk](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@tonyskulk](https://discuss.elastic.co/u/tonyskulk)
#### Post date: [October 2, 2018, 5:09pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-fields-in-vega-vega-lite-visualizations/150677/3 "2018-10-02T17:09:49Z")

</div>

Thanks @thomasneirynck for clarification.  
This helped alot.  
My working spec is now looking like that:

```
data: {
  url: {
    %context%: true
    %timefield%: timestamp
    index: gaptrader.heartbeat
    body: {
      size: 10000,
      _source: ['timestamp', 'lastSignal'],
      script_fields : {
        lastSignalSeconds : {
          script : {
            lang: 'painless',
            source: (doc['timestamp'].value.getMillis() - doc['lastSignal'].value.getMillis()) / 1000
          }
        }
      }
    }
  }
  format: { property: "hits.hits" }
}

```

And in my **encoding** part I can access scripted fields by

```
fields.lastSignalSeconds
```

---

<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: [October 30, 2018, 5:09pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-fields-in-vega-vega-lite-visualizations/150677/4 "2018-10-30T17:09:54Z")

</div>

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