# Runtime script: access list of fields

**URL:** <https://discuss.elastic.co/t/runtime-script-access-list-of-fields/349561>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [December 18, 2023, 1:40pm UTC](https://discuss.elastic.co/t/runtime-script-access-list-of-fields/349561 "2023-12-18T13:40:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dao](https://avatars.discourse-cdn.com/v4/letter/d/a6a055/32.png) [@dao](https://discuss.elastic.co/u/dao)\
**Post date:** [December 18, 2023, 1:40pm UTC](https://discuss.elastic.co/t/runtime-script-access-list-of-fields/349561/1 "2023-12-18T13:40:27Z")

</div>

hello, I try to create a field that is an array of strings. Each string is the name of a field

example: I have docs like this:

```json
{
  a: 1, b:2, toto: 'processed', tata:'processed'
}

```

I want to create a field that will be servers:['toto', 'tata']

To do so, Il try this piece of code:

```auto
def servers = doc.entrySet().stream()
    .filter(f->f.getValue().equals("processed"))
    .map(f->f.getKey())
    .collect(Collectors.toList());
emit(servers);

```

unfortunately, it does not work. even if I replace ` emit(servers);` with  
`emit("hardcoded");`

Any clue or doc on that?

beste

---

<div class="post-metadata">

**Author:** ![dao](https://avatars.discourse-cdn.com/v4/letter/d/a6a055/32.png) [@dao](https://discuss.elastic.co/u/dao)\
**Post date:** [December 18, 2023, 2:44pm UTC](https://discuss.elastic.co/t/runtime-script-access-list-of-fields/349561/2 "2023-12-18T14:44:51Z")

</div>

OK, I did try the dev console mode:

```auto
POST /_scripts/painless/_execute
{
  "script": {
    "source": """
    doc.entrySet();
def servers = doc.get("xid").value;
emit(servers);
"""
  },
  "context": "keyword_field",
  "context_setup": {
    "index": "acars-history-000003",
    "document": {
          "hms": "processed",
          "xid": "95fae05a-96dd-4be5-b642-8c1ac876353f"
    }
  }
}

```

and the response is clear:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.server@8.10.0/org.elasticsearch.search.lookup.LeafDocLookup.entrySet(LeafDocLookup.java:251)",
          """doc.entrySet();
def """,
          " ^---- HERE"
        ],
        "script": " ...",
        "lang": "painless",
        "position": {
          "offset": 8,
          "start": 5,
          "end": 25
        }
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "org.elasticsearch.server@8.10.0/org.elasticsearch.search.lookup.LeafDocLookup.entrySet(LeafDocLookup.java:251)",
      """doc.entrySet();
def """,
      " ^---- HERE"
    ],
    "script": " ...",
    "lang": "painless",
    "position": {
      "offset": 8,
      "start": 5,
      "end": 25
    },
    "caused_by": {
      "type": "unsupported_operation_exception",
      "reason": null
    }
  },
  "status": 400
}

```

My question then:

**1. what are the authorized methods in the context of a doc in a runtime script?**  
**2. how to list the different doc entries?**

---

<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 15, 2024, 2:45pm UTC](https://discuss.elastic.co/t/runtime-script-access-list-of-fields/349561/3 "2024-01-15T14:45:16Z")

</div>

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