Search API sortedit

Sort your results in an order other than document score.

Using sort will override the default relevance scoring method.

A special sorting field name is available, _score, to order by relevance.

If a sort parameter is not given, results will be ordered by descending relevance.

Type Sorting

text

Yes, alphanumerically, case-sensitive.

number

Yes, numerically.

date

Yes, historically.

geolocation

Yes, by distance to a provided geographical point.

Sortingedit

Sort on one field.

Example - Sorting alphanumerically by the title of the national park.

curl -X GET '<ENTERPRISE_SEARCH_BASE_URL>/api/as/v1/engines/national-parks-demo/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-soaewu2ye6uc45dr8mcd54v8' \
-d '{
  "query": "everglade",
  "sort": {
    "title": "desc"
  }
}'

Sorting on Multiple Fieldsedit

Sort on multiple fields by using an array.

Example - Sorting by document score and tie-breaking by the title of the national park.

curl -X GET '<ENTERPRISE_SEARCH_BASE_URL>/api/as/v1/engines/national-parks-demo/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-soaewu2ye6uc45dr8mcd54v8' \
-d '{
  "query": "everglade",
  "sort": [
    { "_score": "desc" },
    { "title": "desc" }
  ]
}'

Sorting by geolocation fieldsedit

Sort on geolocation fields by providing a center point. In order to sort by a geolocation field, a JSON object needs to be provided for the field that includes:

center (required)

Center point will be used to calculate the distance from that point to the field in each document, and return the results ordered by distance. Center point can be specified using any of the geolocation field formats, see geolocation fields. Pay close attention to the ordering of coordinates specified. For example, if documents are stored with coordinates in string format "lat, long" and the center point is specified in array format [long, lat], the coordinate pair is reversed.

mode

How to calculate distance in case a field has several geo points. By default, the shortest distance is taken into account when sorting in ascending order and the longest distance when sorting in descending order. Supported values are:

  • min (default for ascending order): Minimum.
  • max (default for desdencing order): Maximum.
  • median: Median
  • avg: Average

Example - Sorting by distance to the location field to coordinates [38.89, -77.08] and tie-breaking by the title of the national park:

curl -X GET '<ENTERPRISE_SEARCH_BASE_URL>/api/as/v1/engines/national-parks-demo/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-soaewu2ye6uc45dr8mcd54v8' \
-d '{
  "query": "park",
  "sort": [
    {
      "location": {
        "center": [38.89, -77.08],
        "order": "asc"
      }
    },
    {
      "title": "asc"
    }
  ]
}'

Response:

{
  "meta": {
    "alerts": [],
    "warnings": [],
    "precision": 2,
    "page": {
      "current": 1,
      "total_pages": 5,
      "total_results": 46,
      "size": 10
    },
    "engine": {
      "name": "national-parks-demo",
      "type": "default"
    },
    "request_id": "8c2b239b-71c8-4db9-bfd5-e42d04b15288"
  },
  "results": [
    {
      "visitors": {
        "raw": 28892.0
      },
      "square_km": {
        "raw": 33.4
      },
      "world_heritage_site": {
        "raw": "false"
      },
      "date_established": {
        "raw": "1988-10-31T06:00:00+00:00"
      },
      "description": {
        "raw": "The southernmost National Park is on three Samoan islands and protects coral reefs, rainforests, volcanic mountains, and white beaches. The area is also home to flying foxes, brown boobies, sea turtles, and 900 species of fish."
      },
      "location": {
        "raw": "-14.25,-170.68"
      },
      "acres": {
        "raw": 8256.67
      },
      "_meta": {
        "id": "park_american-samoa",
        "engine": "national-parks-demo",
        "score": null
      },
      "id": {
        "raw": "park_american-samoa"
      },
      "title": {
        "raw": "American Samoa"
      },
      "nps_link": {
        "raw": "https://www.nps.gov/npsa/index.htm"
      },
      "states": {
        "raw": [
          "American Samoa"
        ]
      }
    },
    {
      "visitors": {
        "raw": 411343.0
      },
      "square_km": {
        "raw": 60.5
      },
      "world_heritage_site": {
        "raw": "false"
      },
      "date_established": {
        "raw": "1956-08-02T05:00:00+00:00"
      },
      "description": {
        "raw": "This island park on Saint John preserves Taíno archaeological sites and the ruins of sugar plantations from Columbus's time, as well as all the natural environs. Surrounding the pristine beaches are mangrove forests, seagrass beds, and coral reefs."
      },
      "location": {
        "raw": "18.33,-64.73"
      },
      "acres": {
        "raw": 14948.46
      },
      "_meta": {
        "id": "park_virgin-islands",
        "engine": "national-parks-demo",
        "score": null
      },
      "id": {
        "raw": "park_virgin-islands"
      },
      "title": {
        "raw": "Virgin Islands"
      },
      "nps_link": {
        "raw": "https://www.nps.gov/viis/index.htm"
      },
      "states": {
        "raw": [
          "US Virgin Islands"
        ]
      }
    }
  ]
}

Errorsedit

sort

If the field used to sort is not in the schema and is not _score. If the sort JSON object has more than one key.