Sort processor

The sort processor sorts an array of items in either ascending or descending order. Numeric arrays are sorted numerically, while string or mixed arrays (strings and numbers) are sorted lexicographically. The processor throws an error if the input is not an array.

The following is the syntax for the sort processor:

  1. {
  2. "description": "Sort an array of items",
  3. "processors": [
  4. {
  5. "sort": {
  6. "field": "my_array_field",
  7. "order": "desc"
  8. }
  9. }
  10. ]
  11. }

copy

Configuration parameters

The following table lists the required and optional parameters for the sort processor.

ParameterRequired/OptionalDescription
fieldRequiredThe field to be sorted. Must be an array.
orderOptionalThe sort order to apply. Accepts asc for ascending or desc for descending. Default is asc.
target_fieldOptionalThe name of the field in which the sorted array is stored. If not specified, then the sorted array is stored in the same field as the original array (the field variable).
descriptionOptionalA description of the processor’s purpose or configuration.
ifOptionalSpecifies to conditionally execute the processor.
ignore_failureOptionalSpecifies to ignore processor failures. See Handling pipeline failures.
on_failureOptionalSpecifies a list of processors to run if the processor fails during execution. These processors are executed in the order they are specified.
tagOptionalAn identifier tag for the processor. Useful for debugging in order to distinguish between processors of the same type.

Using the processor

Follow these steps to use the processor in a pipeline.

Step 1: Create a pipeline

The following query creates a pipeline named sort-pipeline that uses the sort processor to sort the my_field in descending order and store the sorted values in the sorted_field:

  1. PUT _ingest/pipeline/sort-pipeline
  2. {
  3. "description": "Sort an array of items in descending order",
  4. "processors": [
  5. {
  6. "sort": {
  7. "field": "my_array_field",
  8. "order": "desc",
  9. "target_field": "sorted_array"
  10. }
  11. }
  12. ]
  13. }

copy

Step 2 (Optional): Test the pipeline

It is recommended that you test your pipeline before you ingest documents.

To test the pipeline, run the following query:

  1. POST _ingest/pipeline/sort-pipeline/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_source": {
  6. "my_array_field": [3, 1, 4, 1, 5, 9, 2, 6, 5]
  7. }
  8. }
  9. ]
  10. }

copy

Response

The following example response confirms that the pipeline is working as expected:

  1. {
  2. "docs": [
  3. {
  4. "doc": {
  5. "_index": "_index",
  6. "_id": "_id",
  7. "_source": {
  8. "sorted_array": [
  9. 9,
  10. 6,
  11. 5,
  12. 5,
  13. 4,
  14. 3,
  15. 2,
  16. 1,
  17. 1
  18. ],
  19. "my_array_field": [
  20. 3,
  21. 1,
  22. 4,
  23. 1,
  24. 5,
  25. 9,
  26. 2,
  27. 6,
  28. 5
  29. ]
  30. },
  31. "_ingest": {
  32. "timestamp": "2024-05-30T22:10:13.405692128Z"
  33. }
  34. }
  35. }
  36. ]
  37. }

copy

Step 3: Ingest a document

The following query ingests a document into an index named testindex1:

  1. POST testindex1/_doc?pipeline=sort-pipeline
  2. {
  3. "my_array_field": [3, 1, 4, 1, 5, 9, 2, 6, 5]
  4. }

copy

Response

The request indexes the document into the index testindex1 and then indexes all documents with the my_array_field sorted in descending order, as shown in the following response:

  1. {
  2. "_index": "testindex1",
  3. "_id": "no-Py48BwFahnwl9KZzf",
  4. "_version": 1,
  5. "result": "created",
  6. "_shards": {
  7. "total": 2,
  8. "successful": 1,
  9. "failed": 0
  10. },
  11. "_seq_no": 9,
  12. "_primary_term": 2
  13. }

copy

Step 4 (Optional): Retrieve the document

To retrieve the document, run the following query:

  1. GET testindex1/_doc/no-Py48BwFahnwl9KZzf

copy