Search code examples
elasticsearchelasticsearch-painless

Comparing arrays with Painless


Is there a quick way in Painless to compare the values of one array to another? I'm trying to avoid a long loop statement. I'm looking for something that will evaluate as the following two items:

ARRAY[1,4,3] contains ARRAY[3,1] = true
ARRAY[2,7] is contained by ARRAY[1,7,4,2,6] = true

Solution

  • You can use the following code to achieve what you want:

    Arrays.asList(biggerArray).containsAll(Arrays.asList(smallerArray))
    

    In your case, both of the following statements will yield true:

    Arrays.asList(new Integer[]{1,4,3}).containsAll(Arrays.asList(new Integer[]{3,1}))
    Arrays.asList(new Integer[]{1,7,4,2,6}).containsAll(Arrays.asList(new Integer[]{2,7}))