Sorting comblex Array elements with Painless script

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())

I found the answer myself after looking up how .sorted() works on a stream.
The following code did the trick:

{
	"script" : {
		"inline": "ctx._source.name = params.name; 
                   ctx._source.variants.addAll(params.variants); 
                   ctx._source.variants = ctx._source.variants.stream().distinct()
                      .sorted((o1, o2) -> o1.get(\"packageSize\").compareTo(o2.get(\"packageSize\")))
                      .collect(Collectors.toList());"
	}
}

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