# Vega script

**URL:** <https://discuss.elastic.co/t/vega-script/295049>\
**Category:** Kibana\
**Tags:** vega, painless\
**Created:** [January 21, 2022, 10:31am UTC](https://discuss.elastic.co/t/vega-script/295049 "2022-01-21T10:31:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paigekim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paigekim/32/91494_2.png) [@paigekim](https://discuss.elastic.co/u/paigekim)\
**Post date:** [January 21, 2022, 10:31am UTC](https://discuss.elastic.co/t/vega-script/295049/1 "2022-01-21T10:31:00Z")

</div>

Hello,

I am currently using `Custom visualization` using vega and would like to put a filter on the field option.

```auto
{
  $schema: https://vega.github.io/schema/vega-lite/v4.json
  title: vega test
  data: {
    url: {
      index: content*
      body: {
        size: 0
        query: {
          bool: {
            must: [
            ]
            filter: [
              {
                match_all: {
                }
              }
            ]
            should: [
            ]
            must_not: [
            ]
          }
        }
        aggs: {
          projectCode: {
            terms: {
              field: project_code.raw
              size: "20"
              order: {
                total_cost: desc
              }
            }
            aggs: {
              total_cost: {
                sum: {
                  field: cost
                }
              }
            }
          }
        }
      }
    }
    format: {
      property: aggregations.projectCode.buckets
    }
  }
  mark: {
    type: bar
  }
  encoding: {
    x: {
      field: total_cost.value
      axis: {
        title: cost
      }
      type: quantitative
    }
    y: {
      field: key
      axis: {
        title: project
      }
      type: nominal
      sort: desc
    }
    tooltip: [
      {
        field: key
        title: project
      }
      {
        field: total_cost.value
        title: cost
      }
    ]
  }
}

```

By using this json, I can get top 20 project code, but I would like to get a list that only matches the script I wrote below. I am not sure where I can put this script because it gives `eserror` if I put inside aggregations.

```auto
{
  "lang" : "painless",
  "source" : "doc['project_code.raw'].value.substring(10,14) == "CONT""
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 21, 2022, 1:30pm UTC](https://discuss.elastic.co/t/vega-script/295049/2 "2022-01-21T13:30:33Z")

</div>

Where did you put it in the aggregation? it seems it is not a vega problem but Elasticsearch problem.  
You may have to debug the aggregation query itself using Dev Tools in Kibana or something.

---

<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:** [February 18, 2022, 1:31pm UTC](https://discuss.elastic.co/t/vega-script/295049/3 "2022-02-18T13:31:20Z")

</div>

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