You're viewing version 2.16 of the OpenSearch documentation. This version is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.
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 fields
The following table lists all available request fields.
Field | Data type | Description |
---|---|---|
field | String | The field to be sorted. Must be an array. Required. |
order | String | The sort order to apply. Accepts asc for ascending or desc for descending. Default is asc . |
target_field | String | The 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). |
tag | String | The processor’s identifier. |
description | String | A description of the processor. |
ignore_failure | Boolean | If 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:
POST /my_index/_doc/1
{
"message": ["one", "two", "three", "four"],
"visibility": "public"
}
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:
PUT /_search/pipeline/my_pipeline
{
"response_processors": [
{
"sort": {
"field": "message",
"target_field": "sorted_message"
}
}
]
}
Using a search pipeline
Search for documents in my_index
without a search pipeline:
GET /my_index/_search
The response contains the field message
:
Response
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_id": "1",
"_score": 1,
"_source": {
"message": [
"one",
"two",
"three",
"four"
],
"visibility": "public"
}
}
]
}
}
To search with a pipeline, specify the pipeline name in the search_pipeline
query parameter:
GET /my_index/_search?search_pipeline=my_pipeline
The sorted_message
field contains the strings from the message
field sorted alphabetically:
Response
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_id": "1",
"_score": 1,
"_source": {
"visibility": "public",
"sorted_message": [
"four",
"one",
"three",
"two"
],
"message": [
"one",
"two",
"three",
"four"
]
}
}
]
}
}
You can also use the fields
option to search for specific fields in a document:
POST /my_index/_search?pretty&search_pipeline=my_pipeline
{
"fields": ["visibility", "message"]
}
In the response, the message
field is sorted and the results are stored in the sorted_message
field:
Response
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_id": "1",
"_score": 1,
"_source": {
"visibility": "public",
"sorted_message": [
"four",
"one",
"three",
"two"
],
"message": [
"one",
"two",
"three",
"four"
]
},
"fields": {
"visibility": [
"public"
],
"sorted_message": [
"four",
"one",
"three",
"two"
],
"message": [
"one",
"two",
"three",
"four"
]
}
}
]
}
}