pub struct SortBuilder { /* private fields */ }Expand description
Builds the sort array, one fluent verb per concern — each absorbing its own
optionality so a request maps straight through with no per-field if let.
by/near/tiebreak/or_default dedup by sort key (first wins), so a
field added twice — or an explicit sort that a tiebreak/default would repeat —
appears once; raw is exempt. or_default only contributes when the builder
would otherwise be empty.
use flusso_query::{SortBuilder, SortOrder, OrderBy};
let sorts = SortBuilder::new()
.score_if(true)
.by(count(), SortOrder::Desc)
.by(keyword("city"), None::<OrderBy>) // skipped
.tiebreak(keyword("id"))
.build();
assert_eq!(sorts.len(), 3); // _score, orderCount, idImplementations§
Source§impl SortBuilder
impl SortBuilder
Sourcepub fn by<H: Sortable>(self, handle: H, dir: impl Into<MaybeOrderBy>) -> Self
pub fn by<H: Sortable>(self, handle: H, dir: impl Into<MaybeOrderBy>) -> Self
Sort by a field — or by a map key with fallback
(Type::field().sort_key("it").or("en")). dir accepts a SortOrder,
an OrderBy, or an Option of either (a None skips it), so a
request’s Option<dir> flows straight in. Nesting-aware: a field (or map)
inside nested arrays renders the right nested chain from its scope.
Map-key sorts render a _script sort but dedup on the field path, so
several still coexist; an OrderBy’s missing_first/missing_last
resolves to a direction-correct fallback value (a _script sort can’t use
the missing field). numeric_type/unmapped_type/format are dropped
(field-sort-only); mode is kept (it’s valid on a _script sort).
Sourcepub fn near<S>(
self,
handle: Geo<S>,
center: impl Into<Option<GeoPoint>>,
) -> Self
pub fn near<S>( self, handle: Geo<S>, center: impl Into<Option<GeoPoint>>, ) -> Self
Sort by distance from center (_geo_distance, nearest first). A None
center skips it. Pass a unit / script geo sort through raw.
Sourcepub fn score_if(self, cond: bool) -> Self
pub fn score_if(self, cond: bool) -> Self
Sort by _score only when cond holds (e.g. a free-text query is present).
Sourcepub fn raw(self, sort: impl Into<Option<Sort>>) -> Self
pub fn raw(self, sort: impl Into<Option<Sort>>) -> Self
Append a pre-built Sort verbatim — the escape hatch for sorts the
typed verbs don’t cover (_script, a geo sort with options). A None
adds nothing. Not deduped.
Sourcepub fn tiebreak<H: Sortable>(self, handle: H) -> Self
pub fn tiebreak<H: Sortable>(self, handle: H) -> Self
A stable final sort key (ascending) — append a unique field so equal leading keys still page deterministically.
Sourcepub fn or_default(self, sort: impl Into<Sort>) -> Self
pub fn or_default(self, sort: impl Into<Sort>) -> Self
A fallback used only if nothing else lands in the builder.