# How to excute size function in script Plainess when I want access my field with type nested?

**URL:** <https://discuss.elastic.co/t/how-to-excute-size-function-in-script-plainess-when-i-want-access-my-field-with-type-nested/345429>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 20, 2023, 4:23am UTC](https://discuss.elastic.co/t/how-to-excute-size-function-in-script-plainess-when-i-want-access-my-field-with-type-nested/345429 "2023-10-20T04:23:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![duyhunter1001](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/duyhunter1001/32/126712_2.png) [@duyhunter1001](https://discuss.elastic.co/u/duyhunter1001)\
**Post date:** [October 20, 2023, 4:23am UTC](https://discuss.elastic.co/t/how-to-excute-size-function-in-script-plainess-when-i-want-access-my-field-with-type-nested/345429/1 "2023-10-20T04:23:52Z")

</div>

Example, I have index following:

```auto
PUT candidates
{
 "mappings": {
  "language": {
      type: "nested"
   }
 }
}

```

```auto
POST candidates/_doc
{
  "firstname": "Mike", 
  "age": 31, 
  "city": "New York",
  "language":[
    {
      "name": "English",
      "level": "native"
    },
    {
      "name": "French",
      "level": "basic"
    },
    {
      "name": "German",
      "level": "advanced"
    },
    {
      "name": "Spanish",
      "level": "low"
    }
  ]   
}

```

```auto
POST candidates/_doc
{
  "firstname": "Andre", 
  "age": 28, 
  "city": "Texas",
  "language":[
    {
      "name": "Portuguese",
      "level": "native"
    }
  ]   
}

```

```auto
POST candidates/_doc
{
  "firstname": "Andre", 
  "age": 28, 
  "city": "Texas",
  "language":[]   
}

```

I want check language.length \> 0 == true then return true.

I try script query for plainess following:

```auto
if (doc['language'].size() > 0) { 
   return true;
}
return false;

```

But, I received error:

```auto
"caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [language] in mapping"
 }

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [October 21, 2023, 3:58pm UTC](https://discuss.elastic.co/t/how-to-excute-size-function-in-script-plainess-when-i-want-access-my-field-with-type-nested/345429/2 "2023-10-21T15:58:18Z")

</div>

Hi @duyhunter1001

You can try using [script\_fields](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-fields.html#script-fields).

```auto
GET candidates/_search
{
  "_source": "*", 
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "exists_languages": {
      "script": {
        "lang": "painless",
        "source": """
        if (params['_source']['language'].length > 0) { 
            return true;
        }
        return false;
        """
      }
    }
  }
}

```

---

<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:** [November 18, 2023, 3:59pm UTC](https://discuss.elastic.co/t/how-to-excute-size-function-in-script-plainess-when-i-want-access-my-field-with-type-nested/345429/3 "2023-11-18T15:59:04Z")

</div>

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