Retrieve array sizein script

We are trying to execute a script :slight_smile:

"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

Hi @xef,

Looking at the documentation I believe it should be length 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!

Hi @xef

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

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.

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.

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

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.

Hi
Thanks very much. We have made it work.