mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-24 05:30:16 +01:00
Return a new error when the sort criteria is not sortable
This commit is contained in:
parent
71602e0f1b
commit
1b7f6ea1e7
@ -64,6 +64,15 @@ pub enum AscDesc {
|
|||||||
Desc(String),
|
Desc(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AscDesc {
|
||||||
|
pub fn field(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
AscDesc::Asc(field) => field,
|
||||||
|
AscDesc::Desc(field) => field,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for AscDesc {
|
impl FromStr for AscDesc {
|
||||||
type Err = UserError;
|
type Err = UserError;
|
||||||
|
|
||||||
|
@ -58,6 +58,7 @@ pub enum UserError {
|
|||||||
InvalidFacetsDistribution { invalid_facets_name: HashSet<String> },
|
InvalidFacetsDistribution { invalid_facets_name: HashSet<String> },
|
||||||
InvalidFilter(pest::error::Error<ParserRule>),
|
InvalidFilter(pest::error::Error<ParserRule>),
|
||||||
InvalidFilterAttribute(pest::error::Error<ParserRule>),
|
InvalidFilterAttribute(pest::error::Error<ParserRule>),
|
||||||
|
InvalidSortableAttribute { field: String, valid_fields: HashSet<String> },
|
||||||
InvalidStoreFile,
|
InvalidStoreFile,
|
||||||
MaxDatabaseSizeReached,
|
MaxDatabaseSizeReached,
|
||||||
MissingDocumentId { document: Object },
|
MissingDocumentId { document: Object },
|
||||||
@ -226,6 +227,15 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
Self::InvalidFilterAttribute(error) => error.fmt(f),
|
Self::InvalidFilterAttribute(error) => error.fmt(f),
|
||||||
|
Self::InvalidSortableAttribute { field, valid_fields } => {
|
||||||
|
let valid_names =
|
||||||
|
valid_fields.iter().map(AsRef::as_ref).collect::<Vec<_>>().join(", ");
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"Attribute {} is not sortable, available sortable attributes are: {}",
|
||||||
|
field, valid_names
|
||||||
|
)
|
||||||
|
}
|
||||||
Self::MissingDocumentId { document } => {
|
Self::MissingDocumentId { document } => {
|
||||||
let json = serde_json::to_string(document).unwrap();
|
let json = serde_json::to_string(document).unwrap();
|
||||||
write!(f, "document doesn't have an identifier {}", json)
|
write!(f, "document doesn't have an identifier {}", json)
|
||||||
|
@ -19,6 +19,7 @@ pub use self::facet::{FacetDistribution, FacetNumberIter, FilterCondition, Opera
|
|||||||
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::criterion::AscDesc;
|
||||||
|
use crate::error::UserError;
|
||||||
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};
|
||||||
|
|
||||||
@ -142,13 +143,28 @@ impl<'a> Search<'a> {
|
|||||||
None => MatchingWords::default(),
|
None => MatchingWords::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// We check that we are allowed to use the sort criteria, we check
|
||||||
|
// that they are declared in the sortable fields.
|
||||||
|
let sortable_fields = self.index.sortable_fields(self.rtxn)?;
|
||||||
|
if let Some(sort_criteria) = &self.sort_criteria {
|
||||||
|
for asc_desc in sort_criteria {
|
||||||
|
let field = asc_desc.field();
|
||||||
|
if !sortable_fields.contains(field) {
|
||||||
|
return Err(UserError::InvalidSortableAttribute {
|
||||||
|
field: field.to_string(),
|
||||||
|
valid_fields: sortable_fields,
|
||||||
|
}
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let criteria_builder = criteria::CriteriaBuilder::new(self.rtxn, self.index)?;
|
let criteria_builder = criteria::CriteriaBuilder::new(self.rtxn, self.index)?;
|
||||||
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,
|
||||||
filtered_candidates,
|
filtered_candidates,
|
||||||
sort_criteria,
|
self.sort_criteria.clone(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
match self.index.distinct_field(self.rtxn)? {
|
match self.index.distinct_field(self.rtxn)? {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user