Script filed

Hello ,
i want to add script filed to calcul members of "DO" ===> ( 1 memberId) and "FO" ===> ( 2 memberId) and return sum --- (3)

i can't get size of doc["model.members"].size() or .length() to make boucle for and sum the result
I just have access to the filed "model.members.memberId"

"_index": "Test",

   "_type": "pl",
    "_id": "3cd0-0d2f-4e03",
    "_score": 1,
    "_source": {
      "id": "3cd0-0d2f-4e03",
      "model": {
        "name": "FO",
        "members": [
          {
            "memberId": "2173-fdf4",
          },
          {
            "memberId": "d361-21f6",       
          }],

    "_type": "pl",
    "_id": "3cd0-0d2f-4e03",
    "_score": 1,
    "_source": {
      "id": "3cd0-0d2f-4e03",
      "model": {
        "name": "DO",
        "members": [
          {
            "memberId": "4173-fdf4",
          }
       ],

I think it's just length, like doc["model.members"].length, not length() (no parens).

hi, I tested with doc["model.members"].length and i have this messages :

"script_stack": [
        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:77)",
        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:36)",
        "doc['model.members'].length",
        "    ^---- HERE"
      ],
      "script": "doc['model.members'].length",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "No field found for [model.members] in mapping with types []"
      }

"No field found for [model.members] in mapping with types "

Are you sure that field exists in the index you're trying to query? Maybe some rows don't have the value? Also, the types [] in the message is odd, I don't know if that means it is looking at all types, or no types.

With doc['model.members.membersId'].size() in my script i got the count of members

1 Like

Interesting, I would have thought what you had before would work, I didn't realize you had to reach into the document too. Thanks for following up, it's helpful for other people searching for answers.

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