# How to create a runtime field with an array

**URL:** <https://discuss.elastic.co/t/how-to-create-a-runtime-field-with-an-array/365155>\
**Category:** Kibana\
**Tags:** runtime\
**Created:** [August 19, 2024, 3:14pm UTC](https://discuss.elastic.co/t/how-to-create-a-runtime-field-with-an-array/365155 "2024-08-19T15:14:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikg](https://avatars.discourse-cdn.com/v4/letter/e/91b2a8/32.png) [@erikg](https://discuss.elastic.co/u/erikg)\
**Post date:** [August 19, 2024, 3:14pm UTC](https://discuss.elastic.co/t/how-to-create-a-runtime-field-with-an-array/365155/1 "2024-08-19T15:14:43Z")

</div>

Hello,

I was wondering how I can create a runtime field based on a value inside of a field array.

For example I have this keyword field:

`fruit.names : [kiwi, bananas, apple]`

I want to create a new field based on couple of conditions,

```auto
if banana found in fruit.names , emit("Yellow")
if carrot found in fruit.names, emit("Red")

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 19, 2024, 3:46pm UTC](https://discuss.elastic.co/t/how-to-create-a-runtime-field-with-an-array/365155/2 "2024-08-19T15:46:43Z")

</div>

Hi!

Try this:

```auto
DELETE test
POST test/_doc
{
  "fruits" : ["kiwi", "bananas", "apple"]
}
POST test/_doc
{
  "fruits" : ["kiwi", "bananas", "carrot"]
}
GET test/_search
{
  "fields": [
    "color"
  ], 
  "runtime_mappings": {
    "color": {
      "type": "keyword",
      "script": {
        "source": """
        if (doc['fruits.keyword'].contains("bananas")) emit("Yellow");
        if (doc['fruits.keyword'].contains("carrot")) emit("Red");
        """
      }
    }
  }
}

```

This gives:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_id": "GapMa5EBgHbtuK4M7gUY",
        "_score": 1,
        "_source": {
          "fruits": [
            "kiwi",
            "bananas",
            "apple"
          ]
        },
        "fields": {
          "color": [
            "Yellow"
          ]
        }
      },
      {
        "_index": "test",
        "_id": "GqpRa5EBgHbtuK4MEAXy",
        "_score": 1,
        "_source": {
          "fruits": [
            "kiwi",
            "bananas",
            "carrot"
          ]
        },
        "fields": {
          "color": [
            "Red",
            "Yellow"
          ]
        }
      }
    ]
  }
}

```

Does it help?

---

<div class="post-metadata">

**Author:** ![erikg](https://avatars.discourse-cdn.com/v4/letter/e/91b2a8/32.png) [@erikg](https://discuss.elastic.co/u/erikg)\
**Post date:** [August 19, 2024, 6:25pm UTC](https://discuss.elastic.co/t/how-to-create-a-runtime-field-with-an-array/365155/3 "2024-08-19T18:25:43Z")

</div>

that works! thanks @dadoonet
