# From visState to raw aggregations request

**URL:** <https://discuss.elastic.co/t/from-visstate-to-raw-aggregations-request/293465>\
**Category:** Kibana\
**Tags:** kibana-plugin-development\
**Created:** [January 4, 2022, 4:13pm UTC](https://discuss.elastic.co/t/from-visstate-to-raw-aggregations-request/293465 "2022-01-04T16:13:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolap/32/64933_2.png) [@Nicolap](https://discuss.elastic.co/u/Nicolap)\
**Post date:** [January 4, 2022, 4:13pm UTC](https://discuss.elastic.co/t/from-visstate-to-raw-aggregations-request/293465/1 "2022-01-04T16:13:45Z")

</div>

Hi all,

I'm trying to create an aggregation request from a visstate of a visualize (mainly from a data table).  
I'm searching in the kibana code but didn't find anything...

What I want to do is to get the same result of an inspect data request from vis builder in Kibana...

Someone can help me ?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dosant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dosant/32/64489_2.png) [@dosant](https://discuss.elastic.co/u/dosant)\
**Post date:** [January 10, 2022, 2:45pm UTC](https://discuss.elastic.co/t/from-visstate-to-raw-aggregations-request/293465/2 "2022-01-10T14:45:25Z")

</div>

Hi @Nicolap, I'll try to point you to that code, but what version of Kibana are you using?

---

<div class="post-metadata">

**Author:** ![Nicolap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolap/32/64933_2.png) [@Nicolap](https://discuss.elastic.co/u/Nicolap)\
**Post date:** [January 10, 2022, 3:43pm UTC](https://discuss.elastic.co/t/from-visstate-to-raw-aggregations-request/293465/3 "2022-01-10T15:43:18Z")

</div>

Hi @dosant,

I found the code I need some days ago.

I paste the code I used to achive what I wanted to do:

```auto
      const startServices = await SavedObjectsServices.startServices;
      const savedObjectsClient = startServices[0].savedObjects.client;

      const vis = await savedObjectsClient.get(
        "visualization",
        SOME_SAVED_OBJECT_ID
      );

      let factory;
      if (!factory) {
        factory = getEmbeddable().getEmbeddableFactory("visualization");
      }

      const visParams = {
        filters: [],
        query: {},
      };

      const visHandler = await factory.createFromSavedObject(vis.id, visParams);

      const aggConfig = visHandler.vis.data.aggs;
      const aggsDsl = aggConfig.toDsl(); //this is the magic snippet of code :D

```

---

<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 7, 2022, 3:44pm UTC](https://discuss.elastic.co/t/from-visstate-to-raw-aggregations-request/293465/4 "2022-02-07T15:44:17Z")

</div>

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