# How to put filter inside Vega visualization scatter plot

**URL:** <https://discuss.elastic.co/t/how-to-put-filter-inside-vega-visualization-scatter-plot/267618>\
**Category:** Kibana\
**Created:** [March 18, 2021, 8:25am UTC](https://discuss.elastic.co/t/how-to-put-filter-inside-vega-visualization-scatter-plot/267618 "2021-03-18T08:25:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shalhevet](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Shalhevet](https://discuss.elastic.co/u/Shalhevet)\
**Post date:** [March 18, 2021, 8:25am UTC](https://discuss.elastic.co/t/how-to-put-filter-inside-vega-visualization-scatter-plot/267618/1 "2021-03-18T08:25:14Z")

</div>

hi,  
I want to present the plots for documents with "group"==a, and I only want to present documents with the latest date of the time range applied by the dashboard.  
How can I do this?  
here is my script:

```auto
{
  $schema: https://vega.github.io/schema/vega-lite/v2.json

  data: {
    data: table
    url: {
      %context%: true
      %timefield%: Time
      index: data*
      body: {
        size: 10000
        _source: ["X", "Y","group"]
      }
    }
    format: {property: "hits.hits"}
  }
  mark: {type: "square", filled: true, size: 800 }
encoding: {
    x: {
      field: "_source.X"
      type: quantitative
      axis: {title: "X"}
    }
    y: {
      field: "_source.Y"
      type: quantitative
      axis: {title: "Y"}
    }  
}
}

```

---

<div class="post-metadata">

**Author:** ![markov00](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/markov00/32/33316_2.png) [@markov00](https://discuss.elastic.co/u/markov00)\
**Post date:** [March 22, 2021, 11:00am UTC](https://discuss.elastic.co/t/how-to-put-filter-inside-vega-visualization-scatter-plot/267618/2 "2021-03-22T11:00:37Z")

</div>

the `%context%: true` and `%timefield%: Time` part defines that the ES query used, will be augmented with the filters and query applied to the query bar and the time range used.  
If you for example type a search on the query bar like: `group : "A"` this will filter out all documents with that specific `group` value also on your vega visualization.  
If you need the top X values sorted by the latest date, you can apply a sort operation on the vega ES query on the timefield, something like `"sort" : [{ "@timestamp" : {"order" : "desc"}}],`

---

<div class="post-metadata">

**Author:** ![Shalhevet](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Shalhevet](https://discuss.elastic.co/u/Shalhevet)\
**Post date:** [April 5, 2021, 7:19am UTC](https://discuss.elastic.co/t/how-to-put-filter-inside-vega-visualization-scatter-plot/267618/3 "2021-04-05T07:19:28Z")

</div>

thanks!  
but then how do I choose only the top 1 result?

---

<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:** [May 3, 2021, 7:20am UTC](https://discuss.elastic.co/t/how-to-put-filter-inside-vega-visualization-scatter-plot/267618/4 "2021-05-03T07:20:15Z")

</div>

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