Hello,
What is the way to genereate a random value in Painless?
Being able to draw one value from a set would be particularly great - I want to use it to add a new field to my mock data and I need a possibility to add one of the statuses ('Working', 'Archived', 'InReview') to my documents at random.
You have access to Random in painless. Alternatively, if you have something like a timestamp in your mock data, you can calculate something like timestamp % 3 and assign 'Working', 'Archived', or 'InReview' based on the value.
Just commenting here so we have a complete example to copy/paste for anyone else reading this thread:
# choose random element from array with 6 entries:
PUT _ingest/pipeline/random_element_pipeline
{
"processors": [
{
"script": {
"source": """
Random r = new Random();
ctx.region = ctx.region[r.nextInt(6) - 1];
"""
}
}
]
}
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.