Sort processor

Introduced 2.16

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.

Request body fields

The following table lists all available request fields.

FieldData typeDescription
fieldStringThe field to be sorted. Must be an array. Required.
orderStringThe sort order to apply. Accepts asc for ascending or desc for descending. Default is asc.
target_fieldStringThe 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).
tagStringThe processor’s identifier.
descriptionStringA description of the processor.
ignore_failureBooleanIf true, then OpenSearch ignores any failure of this processor and continues to run the remaining processors in the search pipeline. Optional. Default is false.

Example

The following example demonstrates using a search pipeline with a sort processor.

Setup

Create an index named my_index and index a document with the field message that contains an array of strings:

  1. POST /my_index/_doc/1
  2. {
  3. "message": ["one", "two", "three", "four"],
  4. "visibility": "public"
  5. }

copy

Creating a search pipeline

Create a search pipeline with a sort response processor that sorts the message field and stores the sorted results in the sorted_message field:

  1. PUT /_search/pipeline/my_pipeline
  2. {
  3. "response_processors": [
  4. {
  5. "sort": {
  6. "field": "message",
  7. "target_field": "sorted_message"
  8. }
  9. }
  10. ]
  11. }

copy

Using a search pipeline

Search for documents in my_index without a search pipeline:

  1. GET /my_index/_search

copy

The response contains the field message:

Response

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "my_index",
  19. "_id": "1",
  20. "_score": 1,
  21. "_source": {
  22. "message": [
  23. "one",
  24. "two",
  25. "three",
  26. "four"
  27. ],
  28. "visibility": "public"
  29. }
  30. }
  31. ]
  32. }
  33. }

To search with a pipeline, specify the pipeline name in the search_pipeline query parameter:

  1. GET /my_index/_search?search_pipeline=my_pipeline

copy

The sorted_message field contains the strings from the message field sorted alphabetically:

Response

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "my_index",
  19. "_id": "1",
  20. "_score": 1,
  21. "_source": {
  22. "visibility": "public",
  23. "sorted_message": [
  24. "four",
  25. "one",
  26. "three",
  27. "two"
  28. ],
  29. "message": [
  30. "one",
  31. "two",
  32. "three",
  33. "four"
  34. ]
  35. }
  36. }
  37. ]
  38. }
  39. }

You can also use the fields option to search for specific fields in a document:

  1. POST /my_index/_search?pretty&search_pipeline=my_pipeline
  2. {
  3. "fields": ["visibility", "message"]
  4. }

copy

In the response, the message field is sorted and the results are stored in the sorted_message field:

Response

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "my_index",
  19. "_id": "1",
  20. "_score": 1,
  21. "_source": {
  22. "visibility": "public",
  23. "sorted_message": [
  24. "four",
  25. "one",
  26. "three",
  27. "two"
  28. ],
  29. "message": [
  30. "one",
  31. "two",
  32. "three",
  33. "four"
  34. ]
  35. },
  36. "fields": {
  37. "visibility": [
  38. "public"
  39. ],
  40. "sorted_message": [
  41. "four",
  42. "one",
  43. "three",
  44. "two"
  45. ],
  46. "message": [
  47. "one",
  48. "two",
  49. "three",
  50. "four"
  51. ]
  52. }
  53. }
  54. ]
  55. }
  56. }