API version: 0.3.0

---

# Sorting

This example uses the [long form](/reference/search/elasticsearch-query-dsl-mixin.md#step-query) of the query. The full
text search for "lily" is sorted by the name of the plants in ascending order, instead of by relevance. The search is
restricted to plants, and only the first five results are returned.

```bash
curl -X POST \
  -H 'Authorization: Bearer secret-token:PIPeJGUt7c00ENn8a5uDlc' \
  -H 'Content-Type: application/json' \
  -d '{
        "debug": true,
        "steps": [
          {
            "type": "elasticsearch-query-dsl-mixin",
            "query": {
              "query": {
                "match": {
                  "description": "lily"
                }
              },
              "sort": [
                {
                  "name.keyword": "asc"
                }
              ]
            },
            "parameters": {
              "nodeTypes": [
                "Plant"
              ],
              "pageSize": 5
            }
          },
          {
            "type": "element-hydration"
          }
        ]
      }' \
  https://api.example.com/search
```

##### Response Body

```json
{
  "type": "_SearchResultResponse",
  "results": [
    {
      "type": "Plant",
      "id": "02IiNr0GOoKiPMMeFdFREieZfdDYxEGHG3xKj",
      "data": {
        "images": [
          {
            "description": "Narcissus pseudonarcissus",
            "imageLinkFull": "https://upload.wikimedia.org/wikipedia/commons/a/a4/Narcissus_pseudonarcissus_flower_300303.jpg",
            "imageLinkThumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Narcissus_pseudonarcissus_flower_300303.jp...",
            "link": "https://en.wikipedia.org/wiki/File:Narcissus_pseudonarcissus_flower_300303.jpg"
          }
        ],
        "hasFile": true,
        "name": "Daffodil",
        "scenario": "general.botanicExample",
        "scientificName": "N. pseudonarcissus",
        "link": "https://en.wikipedia.org/wiki/Narcissus_pseudonarcissus",
        "description": "Narcissus pseudonarcissus, commonly named the wild daffodil or Lent lily (Welsh: Cennin Pedr), is a ...",
        "createdAt": "2026-12-05T12:16:05+00:00",
        "updatedAt": "2026-12-05T12:16:05+00:00"
      },
      "file": {
        "contentLength": 62307,
        "extension": "jpg",
        "mimeType": "image/jpeg",
        "hash": {
          "sha256": "ecda5f79c0714acbe609146166abbf3872ba51f459debd5e20c8b61bb932ed37"
        }
      },
      "metadata": {
        "score": 1.8153768,
        "sort": [
          "Daffodil"
        ]
      }
    },
    {
      "type": "Plant",
      "id": "02IiNr0GOoKiPMMr41NLkpxWBZFZ6Xwo147EV",
      "data": {
        "images": [
          {
            "description": "Adenium obesum (desert rose), macro of a flower, side view showing calyx, corolla and styles. Focus ...",
            "imageLinkFull": "https://upload.wikimedia.org/wikipedia/commons/4/4a/Adenium_Obesum_Flower_Side_Macro_Mar22_D72_23052...",
            "imageLinkThumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Adenium_Obesum_Flower_Side_Macro_Mar22_D72...",
            "link": "https://en.wikipedia.org/wiki/File:Adenium_Obesum_Flower_Side_Macro_Mar22_D72_23052-58_ZS_P.jpg"
          },
          {
            "description": "Ra Chi Nee Pan Dok's mother plant",
            "imageLinkFull": "https://upload.wikimedia.org/wikipedia/commons/7/7e/RCN_Mother.jpg",
            "imageLinkThumbnail": "https://upload.wikimedia.org/wikipedia/commons/7/7e/RCN_Mother.jpg",
            "link": "https://en.wikipedia.org/wiki/File:RCN_Mother.jpg"
          }
        ],
        "hasFile": true,
        "name": "Desert Rose",
        "scenario": "general.botanicExample",
        "scientificName": "A. obesum",
        "link": "https://en.wikipedia.org/wiki/Adenium_obesum",
        "description": "Adenium obesum, more commonly known as a desert rose, is a poisonous species of flowering plant belo...",
        "createdAt": "2026-12-05T12:16:05+00:00",
        "updatedAt": "2026-12-05T12:16:05+00:00"
      },
      "file": {
        "contentLength": 58116,
        "extension": "jpg",
        "mimeType": "image/jpeg",
        "hash": {
          "sha256": "858fe2bee255dd252d51d161d4f183a93c2da0d2d6662b20870cbbf3eec81acb"
        }
      },
      "metadata": {
        "score": 1.9940743,
        "sort": [
          "Desert Rose"
        ]
      }
    },
    {
      "type": "Plant",
      "id": "02IiNr0GOoKiPMLPDlPbKrlR4vnBQsvSBSkU5",
      "data": {
        "images": [
          {
            "description": "",
            "imageLinkFull": "https://upload.wikimedia.org/wikipedia/commons/f/fe/Flame_lily.png",
            "imageLinkThumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Flame_lily.png/500px-Flame_lily.png",
            "link": "https://en.wikipedia.org/wiki/File:Flame_lily.png"
          },
          {
            "description": "Gloriosa superba at Mabvuku, Harare, Zimbabwe",
            "imageLinkFull": "https://upload.wikimedia.org/wikipedia/commons/b/bc/Gloriosa_superba_1.jpg",
            "imageLinkThumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Gloriosa_superba_1.jpg/960px-Gloriosa_supe...",
            "link": "https://en.wikipedia.org/wiki/File:Gloriosa_superba_1.jpg"
          }
        ],
        "hasFile": true,
        "name": "Flame Lily",
        "scenario": "general.botanicExample",
        "scientificName": "G. superba",
        "link": "https://en.wikipedia.org/wiki/Gloriosa_superba",
        "description": "Gloriosa superba is a species of flowering plant in the family Colchicaceae. Common names include fl...",
        "createdAt": "2026-12-05T12:16:05+00:00",
        "updatedAt": "2026-12-05T12:16:05+00:00"
      },
      "file": {
        "contentLength": 90681,
        "extension": "jpg",
        "mimeType": "image/jpeg",
        "hash": {
          "sha256": "72e3e3770db795b2f04f0b5bb0be43e74df70de4faa0bf83a706bbde29e4ea0b"
        }
      },
      "metadata": {
        "score": 4.001134,
        "sort": [
          "Flame Lily"
        ]
      }
    },
    "... 2 more items omitted (shapes seen: type:Plant) ..."
  ],
  "debug": [
    {
      "input": {
        "type": "elasticsearch-query-dsl-mixin",
        "query": {
          "query": {
            "match": {
              "description": "lily"
            }
          },
          "sort": [
            {
              "name.keyword": "asc"
            }
          ]
        },
        "parameters": {
          "nodeTypes": [
            "Plant"
          ],
          "pageSize": 5,
          "stepResults": []
        }
      },
      "step-handler": "elasticsearch-query-dsl-mixin (App\\EventSystem\\SearchStep\\EventListener\\ElasticsearchQueryDslMixinSe...",
      "start": 1796473118.058331,
      "indices": "node_plant",
      "combinedQuery": {
        "_source": [
          "_id"
        ],
        "from": 0,
        "size": 5,
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "description": "lily"
                }
              }
            ],
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "_groupsWithSearchAccess.keyword": []
                      }
                    },
                    {
                      "term": {
                        "_usersWithSearchAccess.keyword": {
                          "value": "02IiNr0GOoKiPMM5A0eq9jHk2upaJezsVtWjL"
                        }
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        },
        "sort": [
          {
            "name.keyword": "asc"
          }
        ],
        "track_scores": true
      },
      "end": 1796473118.068568,
      "duration": 0.010237
    },
    {
      "input": {
        "type": "element-hydration",
        "query": null,
        "parameters": {
          "stepResults": [
            {
              "elements": [
                {
                  "id": "02IiNr0GOoKiPMMeFdFREieZfdDYxEGHG3xKj",
                  "type": "Plant",
                  "metadata": {
                    "score": 1.8153768,
                    "sort": [
                      "Daffodil"
                    ]
                  }
                },
                {
                  "id": "02IiNr0GOoKiPMMr41NLkpxWBZFZ6Xwo147EV",
                  "type": "Plant",
                  "metadata": {
                    "score": 1.9940743,
                    "sort": [
                      "Desert Rose"
                    ]
                  }
                },
                {
                  "id": "02IiNr0GOoKiPMLPDlPbKrlR4vnBQsvSBSkU5",
                  "type": "Plant",
                  "metadata": {
                    "score": 4.001134,
                    "sort": [
                      "Flame Lily"
                    ]
                  }
                },
                "... 2 more items omitted (shapes seen: type:Plant) ..."
              ],
              "totalHits": 8,
              "maxScore": 4.001134
            }
          ]
        }
      },
      "step-handler": "element-hydration (App\\EventSystem\\SearchStep\\EventListener\\ElementHydrationSearchStepEventListener)",
      "start": 1796473118.068903,
      "accessibleElementIds": [
        "02IiNr0GOoKiPMMeFdFREieZfdDYxEGHG3xKj",
        "02IiNr0GOoKiPMMr41NLkpxWBZFZ6Xwo147EV",
        "02IiNr0GOoKiPMLPDlPbKrlR4vnBQsvSBSkU5",
        "... 2 more items omitted (shapes seen: string) ..."
      ],
      "end": 1796473118.087063,
      "duration": 0.01816
    }
  ]
}
```

##### Response Headers

```http
Access-Control-Allow-Headers: Authorization, Content-Type, Prefer
Access-Control-Allow-Methods: OPTIONS, POST
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Preference-Applied
Allow: OPTIONS, POST
Cache-Control: no-cache, private
Content-Type: application/json; charset=utf-8
Date: Thu, 03 Dec 2026 12:24:50 GMT
Transfer-Encoding: chunked
Vary: Prefer, Accept-Encoding
X-Powered-By: Ember Nexus API
```

Text properties like `name` can not be sorted directly, therefore the example sorts by `name.keyword`, the exact value
of the property. The [hydrated](/reference/search/element-hydration.md#metadata) elements keep the order and contain the
values they were sorted by in `metadata.sort`. The score is still calculated and available as `metadata.score`, it
just does not decide the order. To sort by relevance first and use a property as a tie-breaker, use
`"sort": ["_score", {"name.keyword": "asc"}]`.

The next page of results, requested with the step parameter `page`, continues the same order.

---

Home: [Ember Nexus API docs](/index.md)

Full site map: [llms.txt](/llms.txt)

Previous: [Highlighting](/reference/search/elasticsearch-query-dsl-mixin/highlighting.md)

Next: [Reverse sorting](/reference/search/elasticsearch-query-dsl-mixin/reverse-sorting.md)
