Hello!
I am looking for a way to use Update API with painless script to update the following record. I am using C# and NEST but writing the script as inline so the functionality should be the same.
{
"name": "Boublegum"
"variants": [
{
"variantId": "123456"
"packageSize": 1
}
]
}
The update should add a new variant to the list of variants without adding any duplicates. I have managed to do this using the script from this post: Unique Array Elements in Painless Script
{
"script" : {
"inline": "ctx._source.variants.addAll(params.variants);
ctx._source.variants = ctx._source.variants.stream().distinct().collect(Collectors.toList());"
}
}
However whenever I try to sort the variants aswell (by adding '.sorted()') I get the following error:
{
"script" : {
"inline": "ctx._source.variants.addAll(params.variants);
ctx._source.variants = ctx._source.variants.stream().distinct().sorted().collect(Collectors.toList());"
}
}
Type: class_cast_exception Reason: "java.util.HashMap cannot be cast to java.lang.Comparable"
I can guess the reason why this error is thrown. My C# class Variant has 2 properties, 'variantId' and 'packageSize' and this makes Elastic Search cast it to a java.util.HashMap with each property mapped in a name value pair.
I have tried the above script (with .sorted()) on a list of strings, which worked fine as java natively knows how to sort strings and primitive types.
Now my question is: Is there anyway to sort the list of variants using any of the properties?
I would like to sort according to packageSize. Is there anyway to use painless to sort these? I was thinking something along the lines of .sorted(v => v.packageSize()) or .sorted(v => v.GetPackageSize())