Nested field size script

Hi, I try to get all documents which nested field size > 1

my mapping:

"selectStatsListNested" : {
          "type" : "nested",
          "properties" : {
            "isSampleSelect" : {
              "type" : "boolean"
            },
            "movedAt" : {
              "type" : "date"
            },
            "searchId" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "selectedAt" : {
              "type" : "date"
            },
            "userId" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }

My query:

GET people/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "selectStatsListNested",
            "query": {
                "script": {
                    "script": """
                      if (doc.containsKey('selectStatsListNested.userId')) {
                        if (doc['selectStatsListNested'].values.size() > 1) {
                          return true;
                        }
                      }
                      return false;
                    """
                }
            }
        }
        }
      ]
        
    }
}
, "_source": "selectStatsListNested"
}

But getting this error:

"error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:88)",
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:40)",
          "if (doc['selectStatsListNested'].values.size() > 1) {\n                          ",
          "        ^---- HERE"
        ]

Can you help what I'm doing wrong :)?

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