- App Search Guide: other versions:
- Installation
- Getting started
- Authentication
- Limits
- Users and access
- Guides
- Adaptive relevance events logs reference
- Analytics Tags
- Crawl web content
- Crawl a private network using a web crawler on Elastic Cloud
- Crawl custom fields using proxy
- Curations
- Elasticsearch search
- Elasticsearch index engines
- Create Elasticsearch index engines
- Configure dynamic field mappings and analyzers in an Elasticsearch index engine
- Elasticsearch engines text field conventions
- Facets
- Hierarchical Facets
- Indexing Documents
- Language Optimization
- Log settings
- Meta Engines
- Precision tuning (beta)
- Query Suggestions
- Search UI
- Relevance Tuning
- Result Settings
- Result Suggestions
- Role based access control (RBAC)
- Sanitization, Raw or Snippet
- Search
- Synonyms
- View web crawler events logs
- App Search web crawler
- Web crawler FAQ
- Web crawler reference
- Web crawler events logs reference
- API Reference
- Adaptive relevance API reference (beta)
- Analytics APIs
- Analytics clicks API
- Analytics counts API
- Analytics queries API
- API logs API
- Click API
- Credentials API
- Curations API reference
- Documents API
- Elasticsearch search API
- Engines API
- Log settings API
- Multi search API
- Query suggestion API
- Schema API
- Search API
- Search API boosts
- Search API facets
- Search API filters
- Search API group
- Search API precision (beta)
- Search API result fields
- Search API search fields
- Search API sort
- Search API analytics tags
- Search settings API
- Search Explain API
- Source engines API
- Synonyms API
- Web crawler API reference
- API Clients
- Configuration
- Known issues
- Troubleshooting
Search API sort
editSearch API sort
editSort 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 |
---|---|
|
Yes, alphanumerically, case-sensitive. |
|
Yes, numerically. |
|
Yes, historically. |
|
Yes, by distance to a provided geographical point. |
Sorting
editSort 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 Fields
editSort 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 fields
editSort 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 thecenter
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" ] } } ] }
Errors
edit
|
If the field used to sort is not in the schema and is not |
ElasticON events are back!
Learn about the Elastic Search AI Platform from the experts at our live events.
Register now