# Help to convert hardcoded data values in Vega to ElasticSearch

**URL:** <https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [December 7, 2022, 3:28pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694 "2022-12-07T15:28:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tinrik](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tinrik](https://discuss.elastic.co/u/tinrik)\
**Post date:** [December 7, 2022, 3:28pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/1 "2022-12-07T15:28:18Z")

</div>

Hi!

I have a [Vega visualization](https://vega.github.io/editor/#/url/vega/N4IgJAzgxgFgpgWwIYgFwhgF0wBwqgegIDc4BzJAOjIEtMYBXAI0poHsDp5kTykSArJQBWENgDsQAGhAATONABONHJnaT0AQXEACOAA8kCHABs4OtgDMdSHVBqKoZnTiRQA1jXFkdJpAE82BkwdSzZFHRgaOEUkRyioJBMdWSRMKmkQAHcaWXo0AQAGQpl4GjIsAuKZV1lZLzICmSRgtggaAC84NBBxCW6ZVPS0AG1QcSNu9ExFOAGQYiSGBVHxyZ6wtkycAEYelBqAJh6mbYBmDbY2SigcHEz2rrQdw7OAXyk1hCmQJjjtvboA4gHDHdCnGoXcFxG53B6dKZnQ47D5fH5-DoAnpQbZguTnE5IDqUGDwp6oHbFQqo3rrdCbY41QEgHFHHqyAn0q6HElkqYAFmpAF0ZDMkOIIGFFAhViBMP4cD9xApMJl3HB-BBRiC9mzISARSAyHBlbFMFMZssafLFZdpQw-JkDDhFOy0gwEJR1f4dAAfX0pd2eibfP0BgDkEEc4cySC16D8TDgJhA1oVP1cHkylmiJg56Ee8zEilVqFAOeT+YWS26HxAhdl7TIExT6ByeVJNKbLZ6ZQqqreQsHg5k0CSK1QY1p32xbBM4UyNp+4XqPcGbGQXjQoCGKGms3mFbz7Lgqk7Mli3imoC4iB+iXNZHC-kOhVTI-r5Rb8anIZ+Y7MAANTJFhMZZnhkCRZTgUhxEweMWRMGgsxkBgcCGH4AAp23oAhDgASgIIYPUoV1h0+ad-0SMwAE0QJrCCQCgydQBgk14OxJCUJANCMJ6TC+ywPDCOIz0yKHCi-x6fQABVLwgPw1CgmRQPA1ASiYjQpzYuCEKcZD3EyXi0iwnCYGEnQAFpA0wEj9B0AAqHQALgYDyLRHp-Dk8UFLSdR6LAqYNOY7TYI49B9O44zzX4wTMAs6zRMoH0nJcujh0NZBFHcH9QCXHoIH8BAmDnbNFA3bc5DSPc5QPVMZBNKA2HkSr2JiSqIBgJBbTLatAuxBwnFrGQcxMVsb2o+85wXEbcyreQz3quUrhMNR7l67skjdWzPUTZMdAAah0TCksLHQAH4dHDKQrsOmySLOo7wx0Jh-HNCBntQK7w3w99UPQkzKv0Dqvy29Akvs1LJsAu7ZPkxT-LrfwQebMGqp25LHOcyaaLurz4b8qC6wbDbQdbEB+SxpKIic6msZcmGofHdLRxmNh1Uq1SfiyKIYuJtn1QAdVyfJeq5tBCkoAQ3jrGA2FIV0NoF68+rUkBZg5fnyqFkXSTFhjUEOGWZaHIA) working with harcoded data points. I have pasted the code in Kibana and works identically to the Vega editor.

Now I want to replace hardcoded data points with the equivalent Elasticsearch query. I have pushed those 4 datapoints as 4 documents into my Elasticsearch index, but I can't figure out how to actually make it work. I have replaced the data with the following:

```auto
  "data": {
      "name": "tree",
      "url": {
        "%context%": true,
        "index": "test",
        "body" : {
          "_source": ["p1", "p2", "p3", "size", "name"]
        }
      },

```

And replaced the usages of "p1", "p2" and so on with `_source.p1` and so on.

However Kibana throws the error message:

```auto
Cannot read properties of undefined (reading 'name')

```

I don't quite understand where the problem is, could I get some help to figure out how to change the data field in Vega?

Thanks!

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 7, 2022, 6:41pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/2 "2022-12-07T18:41:29Z")

</div>

Can you post your whole spec? I am guessing where you use `name` field you would need to do `_source.name` instead. But would need to [see your full spec with data](https://www.elastic.co/guide/en/kibana/current/vega.html#asking-for-help-with-a-vega-spec).

---

<div class="post-metadata">

**Author:** ![tinrik](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tinrik](https://discuss.elastic.co/u/tinrik)\
**Post date:** [December 8, 2022, 10:09am UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/3 "2022-12-08T10:09:05Z")

</div>

Thanks for the help and link to the docs on how to post a full spec, here it is! [https://gist.githubusercontent.com/carlosgalvezp/cd2d284e5a376e9ced059e9c522475ca/raw/ff290bd8b3096d383670b54d15340a83814b6b27/test.json](https://gist.githubusercontent.com/carlosgalvezp/cd2d284e5a376e9ced059e9c522475ca/raw/ff290bd8b3096d383670b54d15340a83814b6b27/test.json)

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 8, 2022, 1:32pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/4 "2022-12-08T13:32:17Z")

</div>

```auto
        "type": "formula",
        "expr": "datum.key || datum._source.name || 'src'",
        "as": "label"
      }

```

change to

```auto
      {
        "type": "formula",
        "expr": "datum.key || datum.name || 'src'",
        "as": "label"
      }

```

If you get an error like this but for `size` next. Find the field and remove the `_source`.

---

<div class="post-metadata">

**Author:** ![tinrik](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tinrik](https://discuss.elastic.co/u/tinrik)\
**Post date:** [December 8, 2022, 2:22pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/5 "2022-12-08T14:22:02Z")

</div>

Thanks for the answer! If I do that, then no data is displayed. Isn't the problem that querying Elasticsearch puts all the datapoints into the "\_source" field, and so that's why I need to type it?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 8, 2022, 3:08pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/6 "2022-12-08T15:08:27Z")

</div>

Sorry I was just looking how to clear the error. You have an issue with your data and transformations. You have a bunch of undefined rows and `x`, `y,` and `r` columns are blank. This is why you aren't seeing anything.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/f/bf679596d72cfa8885d88dbd2c74faca915bbaa4.jpeg)

Looks like your data is lacking an ID and Parent ID which is required to make this visualization work according to the [Stratify Transform](https://vega.github.io/vega/docs/transforms/stratify/). Take a look at the [sample](https://vega.github.io/editor/#/examples/vega/circle-packing) and see what data they start with and what they end with. That's what you need to match.

---

<div class="post-metadata">

**Author:** ![tinrik](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tinrik](https://discuss.elastic.co/u/tinrik)\
**Post date:** [December 8, 2022, 3:20pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/7 "2022-12-08T15:20:23Z")

</div>

Yes, the reason for that data being missing is that the "nest" transform is not working, as it's not picking up "p1", "p2" and "p3" correctly. Without Elastic, my data points look like:

```auto
{
  "p1": "a",
  "p2": "b",
  "p3": "c",
}

```

This works fine for the `nest` transform. However using Elastic, the datapoints look like:

```auto
{
  "_source": {
    "p1": "a",
    "p2": "b",
    "p3": "c",
  }
}

```

which makes `nest` no longer work.

---

<div class="post-metadata">

**Author:** ![tinrik](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tinrik](https://discuss.elastic.co/u/tinrik)\
**Post date:** [December 8, 2022, 3:24pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/8 "2022-12-08T15:24:19Z")

</div>

I replaced mentions of `p1` with `_source.p1` inspired by [this thread](https://discuss.elastic.co/t/accessing-elasticsearch-from-vega-to-create-custom-visualizations/200714) but it seems it doesn't work?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 8, 2022, 3:55pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/9 "2022-12-08T15:55:46Z")

</div>

Understand. I am not familiar enough with the `nest` transformation to help out. Might be worth the time opening an issue on Vegas GitHub support page.

One thing that might help a little is the `project` transformation. Usually I just add that first and rename all the `_source` fields to drop that portion to make it easier to work with later.

I can keep looking when I get some more time though.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [December 8, 2022, 3:58pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/10 "2022-12-08T15:58:14Z")

</div>

I'd try these transformations.

```auto
      "transform": [
        {
          "type": "project",
          "fields": [
            "_source.p1",
            "_source.p2",
            "_source.p3",
            "_source.size",
            "_source.name"
          ],
          "as": ["p1", "p2", "p3", "size", "name"]
        },
        {"type": "nest", "keys": ["p1", "p2", "p3"], "generate": true},
        {
          "type": "pack",
          "field": "size",
          "sort": {"field": "value"},
          "size": [{"signal": "width"}, {"signal": "height"}]
        }
      ]

```

---

<div class="post-metadata">

**Author:** ![tinrik](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tinrik](https://discuss.elastic.co/u/tinrik)\
**Post date:** [December 9, 2022, 12:50pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/11 "2022-12-09T12:50:38Z")

</div>

> [@aaron-nimocks](#):
>
> ```auto
> "type": "project",
> "fields": [
> "_source.p1",
> "_source.p2",
> "_source.p3",
> "_source.size",
> "_source.name"
> ],
> "as": ["p1", "p2", "p3", "size", "name"]
> 
> ```

Works like a charm, million thanks!!

---

<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:** [January 6, 2023, 12:50pm UTC](https://discuss.elastic.co/t/help-to-convert-hardcoded-data-values-in-vega-to-elasticsearch/320694/12 "2023-01-06T12:50:55Z")

</div>

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