Index document with script

I have documents with a field named "num".
I want to insert a new document with value (max "num" + 1) at that time in ES cluster.
Is it possible to create an index w/script that does this?
Can it promise atomicity and validity?

Unfortunately, not possible with a script. Scripts only have knowledge of the local context they are operating on... in this case, just the document that you're indexing.

You'll have to do this yourself by executing an aggregation to find the max value, then using that when indexing the new document. You'll also have to ensure that other threads/clients do not do the same thing and index the same value.

That's exactly what I was trying to avoid... :sweat:

Sorry :worried:

The limitation is really just a limitation of clustered services, not so much the script. To know the maximum ID, the cluster would either have to synchronize all ID updates across all nodes (expensive), or perform some kind of runtime query to find the max ID (e.g. aggregations).

Because scripts are self-contained, small and fast execution units, the user has to extract this kind of "secondary" knowledge if it's required... otherwise, the update script would be executing expensive requests to the cluster at potentially a high rate.

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