Skip to main content

Sort

Mutate Elastic Compatible

Synopsis

Sorts the elements of an array in ascending or descending order.

Schema

sort:
- field: <ident>
- description: <text>
- if: <script>
- ignore_failure: <boolean>
- on_failure: <processor[]>
- on_success: <processor[]>
- order: <enum>
- tag: <string>
- target_field: <ident>

Configuration

FieldRequiredDefaultDescription
fieldY-Field containing the array to be sorted
descriptionN-Explanatory note
ifN-Condition to run
ignore_failureNfalseSee Handling Failures
on_failureN-See Handling Failures
on_successN-See Handling Success
orderNascSort order: asc or desc
tagN-Identifier
target_fieldNfieldField to store the sorted array

Details

Arrays of numbers will be sorted numerically, whereas string arrays will be sorted lexicographically. When dealing with mixed arrays containing both strings and numbers, the sorting is performed lexicographically.

The processor raises an exception if the specified field is not an array.

warning

The processor will fail if the specified field does not exist or is not an array. Set ignore_failure to true to handle such cases.

Examples

String Arrays

Sorting a string array in ascending order...

{
"source": {
"types": ["z", "b", "k"]
}
}
sort:
- field: source.types

orders strings lexicographically:

{
"source": {
"types": ["b", "k", "z"]
}
}

Numeric Arrays

Sorting numeric arrays in ascending order...

{
"destination": {
"types": [9, 3, 7]
}
}
sort:
- field: destination.types
- order: asc

orders numbers numerically:

{
"destination": {
"types": [3, 7, 9]
}
}

Target Field

Sorting the array into a new field...

{
"values": [5, 2, 8, 1]
}
sort:
- field: values
- target_field: sorted_values

preserves the original array:

{
"values": [5, 2, 8, 1],
"sorted_values": [1, 2, 5, 8]
}