Ordering and Pagination
Ordering
Elasticsearch inherently ranks search results based on relevance (using internal scoring and ranking algorithms). However, it is often necessary to sort results based on specific fields. The Laravel-Elasticsearch integration provides a simple and intuitive way to sort search results using the orderBy
and orderByDesc
methods.
OrderBy
The orderBy
method allows you to specify the field by which the results should be sorted and the direction of the sort (ascending or descending). This method is straightforward and aligns with the Laravel Eloquent’s orderBy
functionality.
Find all products and order them by their status in ascending order.
Find all products and order them by creation date in descending order.
If you have a field that is mapped as a text
field with a keyword
subfield, you will need to sort on the keyword subfield:
Find all products and order them by their name in ascending order via the keyword subfield.
OrderByDesc
As with Laravel’s standard eloquent, the orderByDesc
method is provided to quickly sort results in descending order by a specified field, without needing to explicitly set the direction.
Find all products and order them by creation date in descending order.
Offset & Limit (skip & take)
As with Eloquent, you can use the skip
and take
methods in your query.
Find all products and skip the first 10, then take the next 5.
Extending ordering for Elasticsearch features
WithSort
The withSort
method allows you to extend the ordering functionality to support more advanced Elasticsearch sorting features.
withSort($field, $type, $value)
Types
mode
- Specify how Elasticsearch should handle sorting by array or multi-valued fields.- Options:
min
,max
,sum
,avg
,median
- Options:
missing
- Specify how Elasticsearch should handle sorting when a document is missing the field being sorted.- Options:
_first
,_last
, or a custom value
- Options:
unmapped_type
- By default, the search request will fail if there is no mapping associated with a field - Theunmapped_type
option allows you to ignore fields that have no mapping and not sort by them.ignore_unmapped
- Ignore documents with unmapped fields when sorting.
Pagination
Pagination works as expected in this Laravel-Elasticsearch integration.
Find all active products and paginate them with 50 products per page.
Pagination links (Blade)
OrderBy Geo Distance
The orderByGeo
method is a specialized method for sorting by geo distance. This sorting method only works with geo
fields and can be used to sort results based on their distance from a specified point.
Parameter scope:
orderByGeo(string $column, array $pin, $direction = 'asc', $unit = 'km', $mode = null, $type = 'arc')
orderByGeoDesc(string $column, array $pin, $unit = 'km', $mode = null, $type = 'arc')
$pin
The point(s) from which to calculate the distance. This can be a single point or an array of points.
- A point is an array with two values:
[lon, lat]
(longitude, latitude) - Order matters and Elasticsearch does not use the standard[lat, lon]
format - For multiple points, use an array of points:
[[lon1, lat1], [lon2, lat2], ...]
- You can also specify the lat and lon keys:
['lat' => 51.50853, 'lon' => -0.12574]
$direction
- The direction in which to sort the results. Options:
asc
,desc
asc
sorts by shortest distance from the pindesc
sorts by longest distance from the pin
$unit
The unit to use when computing sort values. Options: m
, km
, mi
, yd
, ft
$mode
- Used for when a field has several geo points. By default, the shortest distance is used when sorting in ascending order and the longest distance when sorting in descending order.
- Options:
min
,max
,sum
,avg
,median
$type
- The type of distance calculation to use.
- Options:
arc
,plane
- Note:
plane
is faster, but inaccurate on long distances and close to the poles.
Find all active products and order them by the closest distance of the manufacturer’s location to London.
Find all active products and order them by the longest average distance of the manufacturer’s location to Paris and London.
Order By Nested
The orderByNested
method allows you to order the results of a query by a nested field.
This will return all the blog posts with a status of 5 and order them by the sum of the likes of the comments in descending order. It will then limit the results to 5.
Order By Random
orderByRandom(string $column, int $seed = 1)
Uses Elasticsearch’s random_score to randomise ordering
Find all products with a color of silver and order them randomly by creation date. Limit the results to 5.