Runtime script: access list of fields

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:

{
  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:

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

OK, I did try the dev console mode:

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:

{
  "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?

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