# Retrieve array sizein script

**URL:** <https://discuss.elastic.co/t/retrieve-array-sizein-script/363108>\
**Category:** Elasticsearch\
**Created:** [July 14, 2024, 9:10pm UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108 "2024-07-14T21:10:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [July 14, 2024, 9:10pm UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/1 "2024-07-14T21:10:33Z")

</div>

We are trying to execute a script 🙂

```auto
"if (ctx._source.repliesList == null) {ctx._source.repliesList = new ArrayList();}" +
"if(ctx._source.repliesList.size()<10) { ctx._source.repliesList.add('" + proid + "');}"

```

But it does notwork due to the condition:  
`if(ctx._source.repliesList.size()<10)`

We tried 'length' as well,which also does not work.  
Thanks

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 15, 2024, 2:07pm UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/2 "2024-07-15T14:07:40Z")

</div>

Hi @xef,

[Looking at the documentation I believe it should be `length`](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-array.html#array-length-operator) that you should be using.

Can you share the exception/ error that you receive and the mapping of your documents? If you can also check the value is defined in all documents and is an array in all cases that would also be useful.

Let us know!

---

<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:** [July 15, 2024, 2:09pm UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/3 "2024-07-15T14:09:05Z")

</div>

Hi @xef

If repliesList is keyword type, did you try like this: doc['repliesList'].size() or doc['repliesList'].length ?

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [July 15, 2024, 3:58pm UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/4 "2024-07-15T15:58:04Z")

</div>

Hi. Tere is no exception or errors. The query returns a Valid response.  
Also some documents may have null initially as the array that is why the conditional statement is included.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 16, 2024, 9:24am UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/5 "2024-07-16T09:24:57Z")

</div>

Ok, so can you explain what you mean by "it doesn't work"? What are you trying to do?

If you could also share your index mapping that would be useful.

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [July 16, 2024, 11:33am UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/6 "2024-07-16T11:33:25Z")

</div>

Hi  
It seems that it executes the script only once.  
i.e. The first time it works, but subsequent times it is ignored.  
Is our syntax correct because it seems the condition:

`if (ctx._source.repliesList == null) {ctx._source.repliesList = new ArrayList();}`

If it is false, the rest of the script does not execute! Thanks

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 16, 2024, 1:16pm UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/7 "2024-07-16T13:16:02Z")

</div>

By the rest of the script do you mean the excerpt that you're concatenating on:

> +  
> "if(ctx.\_source.repliesList.size()\<10) { ctx.\_source.repliesList.add('" + proid + "');}"

Where are you defining this painless script? If you're looking to run a multi-line script I would recommend using triple quotes [as outlined in the documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-rest-overview.html#sql-kibana-console).

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [July 16, 2024, 4:25pm UTC](https://discuss.elastic.co/t/retrieve-array-sizein-script/363108/8 "2024-07-16T16:25:11Z")

</div>

Hi  
Thanks very much. We have made it work.
