Add a sort_criteria method to the Search builder struct

This commit is contained in:
Kerollmops 2021-08-17 15:36:03 +02:00
parent 687cd2e205
commit 407f53872a
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 18 additions and 8 deletions

View File

@ -1,6 +1,5 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr;
use roaring::RoaringBitmap; use roaring::RoaringBitmap;
@ -13,6 +12,7 @@ use self::r#final::Final;
use self::typo::Typo; use self::typo::Typo;
use self::words::Words; use self::words::Words;
use super::query_tree::{Operation, PrimitiveQueryPart, Query, QueryKind}; use super::query_tree::{Operation, PrimitiveQueryPart, Query, QueryKind};
use crate::criterion::AscDesc as AscDescName;
use crate::search::{word_derivations, WordDerivationsCache}; use crate::search::{word_derivations, WordDerivationsCache};
use crate::{DocumentId, FieldId, Index, Result, TreeLevel}; use crate::{DocumentId, FieldId, Index, Result, TreeLevel};
@ -274,9 +274,9 @@ impl<'t> CriteriaBuilder<'t> {
query_tree: Option<Operation>, query_tree: Option<Operation>,
primitive_query: Option<Vec<PrimitiveQueryPart>>, primitive_query: Option<Vec<PrimitiveQueryPart>>,
filtered_candidates: Option<RoaringBitmap>, filtered_candidates: Option<RoaringBitmap>,
sort_criteria: Option<Vec<String>>, sort_criteria: Option<Vec<AscDescName>>,
) -> Result<Final<'t>> { ) -> Result<Final<'t>> {
use crate::criterion::{AscDesc as AscDescName, Criterion as Name}; use crate::criterion::Criterion as Name;
let primitive_query = primitive_query.unwrap_or_default(); let primitive_query = primitive_query.unwrap_or_default();
@ -288,19 +288,19 @@ impl<'t> CriteriaBuilder<'t> {
Name::Typo => Box::new(Typo::new(self, criterion)), Name::Typo => Box::new(Typo::new(self, criterion)),
Name::Sort => match sort_criteria { Name::Sort => match sort_criteria {
Some(ref sort_criteria) => { Some(ref sort_criteria) => {
for text in sort_criteria { for asc_desc in sort_criteria {
criterion = match AscDescName::from_str(text)? { criterion = match asc_desc {
AscDescName::Asc(field) => Box::new(AscDesc::asc( AscDescName::Asc(field) => Box::new(AscDesc::asc(
&self.index, &self.index,
&self.rtxn, &self.rtxn,
criterion, criterion,
field, field.to_string(),
)?), )?),
AscDescName::Desc(field) => Box::new(AscDesc::desc( AscDescName::Desc(field) => Box::new(AscDesc::desc(
&self.index, &self.index,
&self.rtxn, &self.rtxn,
criterion, criterion,
field, field.to_string(),
)?), )?),
}; };
} }

View File

@ -18,6 +18,7 @@ pub(crate) use self::facet::ParserRule;
pub use self::facet::{FacetDistribution, FacetNumberIter, FilterCondition, Operator}; pub use self::facet::{FacetDistribution, FacetNumberIter, FilterCondition, Operator};
pub use self::matching_words::MatchingWords; pub use self::matching_words::MatchingWords;
use self::query_tree::QueryTreeBuilder; use self::query_tree::QueryTreeBuilder;
use crate::criterion::AscDesc;
use crate::search::criteria::r#final::{Final, FinalResult}; use crate::search::criteria::r#final::{Final, FinalResult};
use crate::{DocumentId, Index, Result}; use crate::{DocumentId, Index, Result};
@ -37,6 +38,7 @@ pub struct Search<'a> {
filter: Option<FilterCondition>, filter: Option<FilterCondition>,
offset: usize, offset: usize,
limit: usize, limit: usize,
sort_criteria: Option<Vec<AscDesc>>,
optional_words: bool, optional_words: bool,
authorize_typos: bool, authorize_typos: bool,
words_limit: usize, words_limit: usize,
@ -51,6 +53,7 @@ impl<'a> Search<'a> {
filter: None, filter: None,
offset: 0, offset: 0,
limit: 20, limit: 20,
sort_criteria: None,
optional_words: true, optional_words: true,
authorize_typos: true, authorize_typos: true,
words_limit: 10, words_limit: 10,
@ -74,6 +77,11 @@ impl<'a> Search<'a> {
self self
} }
pub fn sort_criteria(&mut self, criteria: Vec<AscDesc>) -> &mut Search<'a> {
self.sort_criteria = Some(criteria);
self
}
pub fn optional_words(&mut self, value: bool) -> &mut Search<'a> { pub fn optional_words(&mut self, value: bool) -> &mut Search<'a> {
self.optional_words = value; self.optional_words = value;
self self
@ -135,7 +143,7 @@ impl<'a> Search<'a> {
}; };
let criteria_builder = criteria::CriteriaBuilder::new(self.rtxn, self.index)?; let criteria_builder = criteria::CriteriaBuilder::new(self.rtxn, self.index)?;
let sort_criteria = None; let sort_criteria = self.sort_criteria.clone();
let criteria = criteria_builder.build( let criteria = criteria_builder.build(
query_tree, query_tree,
primitive_query, primitive_query,
@ -205,6 +213,7 @@ impl fmt::Debug for Search<'_> {
filter, filter,
offset, offset,
limit, limit,
sort_criteria,
optional_words, optional_words,
authorize_typos, authorize_typos,
words_limit, words_limit,
@ -216,6 +225,7 @@ impl fmt::Debug for Search<'_> {
.field("filter", filter) .field("filter", filter)
.field("offset", offset) .field("offset", offset)
.field("limit", limit) .field("limit", limit)
.field("sort_criteria", sort_criteria)
.field("optional_words", optional_words) .field("optional_words", optional_words)
.field("authorize_typos", authorize_typos) .field("authorize_typos", authorize_typos)
.field("words_limit", words_limit) .field("words_limit", words_limit)