# Union two fields within Vega

**URL:** <https://discuss.elastic.co/t/union-two-fields-within-vega/296613>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [February 8, 2022, 2:38pm UTC](https://discuss.elastic.co/t/union-two-fields-within-vega/296613 "2022-02-08T14:38:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TUKTUK](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@TUKTUK](https://discuss.elastic.co/u/TUKTUK)\
**Post date:** [February 8, 2022, 2:38pm UTC](https://discuss.elastic.co/t/union-two-fields-within-vega/296613/1 "2022-02-08T14:38:42Z")

</div>

Hello together,

I am looking for a way to union the elements of two different fields into one (new) array in Vega (If this is already possible in an Elastic Query that's also fine).  
It should be similar to a SQL-Statement like "select A as NewField from TableA UNION Select B as NewField from TableB"

I run the query \_source["main\_entities\_location","main\_entities\_person"] which gives me the following data.  
 ![Union snip](https://us1.discourse-cdn.com/elastic/original/3X/a/8/a87de538351fd635d8224e74c3050f4f5a683f41.png)

I want to union all the entries of "main\_entities\_location" with the entries of "main\_entities\_person" in one new field/array to be able to do a wordcloud transform afterwards.

Thanks and kind regards

---

<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:** [February 8, 2022, 4:49pm UTC](https://discuss.elastic.co/t/union-two-fields-within-vega/296613/2 "2022-02-08T16:49:59Z")

</div>

How about using runtime mappings if performance is acceptable?

```auto
GET /test_union/_search?filter_path=hits.hits.fields
{
  "runtime_mappings": {
    "united": {
      "type": "keyword",
      "script": {
        "source": """
          for (field in params.fields) {
            for (val in doc[field]){
              emit(val)
            }
          }
        """,
        "params":{
          "fields": ["location","person"]
        }
      }
    }
  },
  "fields": [
    "united"
  ]
}

```

```auto
{
  "hits" : {
    "hits" : [
      {
        "fields" : {
          "united" : [
            "Elon Musk",
            "Gwynne Shotwell",
            "Massachusetts",
            "United States"
          ]
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![TUKTUK](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@TUKTUK](https://discuss.elastic.co/u/TUKTUK)\
**Post date:** [February 8, 2022, 7:54pm UTC](https://discuss.elastic.co/t/union-two-fields-within-vega/296613/3 "2022-02-08T19:54:46Z")

</div>

Thanks that works great in Dev-Console 🙂  
But how can I put this inside my data section with url-data call? I mean especially the GET- request part with "\_search?filter\_path=hits.hits.fields" which seem to be relevant parameter information

```auto
  "data": [
    {
      "name": "values",
      "url": {
        "%context%": true,
        "%timefield%": "first_date"
        "index": "clusters",
        "body": {
           ##My Query or Aggregtion inside here##
        }
      },
      "format": {"property": "something"},
      ...
      ...

```

---

<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:** [February 8, 2022, 8:00pm UTC](https://discuss.elastic.co/t/union-two-fields-within-vega/296613/4 "2022-02-08T20:00:44Z")

</div>

That would most likely go in your `format` if you wanted to filter to that data. For your index just have the index name without the\_search or filter.

```auto
"format": {"property": "hits.hits.fields"}

```

---

<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:** [March 8, 2022, 8:01pm UTC](https://discuss.elastic.co/t/union-two-fields-within-vega/296613/5 "2022-03-08T20:01:30Z")

</div>

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