Add API search setting

This commit is contained in:
ManyTheFish 2023-06-07 11:24:40 +02:00
parent a3716c5678
commit 461b5118bd
7 changed files with 22 additions and 2 deletions

View File

@ -224,6 +224,7 @@ InvalidIndexLimit , InvalidRequest , BAD_REQUEST ;
InvalidIndexOffset , InvalidRequest , BAD_REQUEST ;
InvalidIndexPrimaryKey , InvalidRequest , BAD_REQUEST ;
InvalidIndexUid , InvalidRequest , BAD_REQUEST ;
InvalidRestrictSearchableAttributes , InvalidRequest , BAD_REQUEST ;
InvalidSearchAttributesToCrop , InvalidRequest , BAD_REQUEST ;
InvalidSearchAttributesToHighlight , InvalidRequest , BAD_REQUEST ;
InvalidSearchAttributesToRetrieve , InvalidRequest , BAD_REQUEST ;

View File

@ -70,6 +70,8 @@ pub struct SearchQueryGet {
crop_marker: String,
#[deserr(default, error = DeserrQueryParamError<InvalidSearchMatchingStrategy>)]
matching_strategy: MatchingStrategy,
#[deserr(default, error = DeserrQueryParamError<InvalidRestrictSearchableAttributes>)]
pub restrict_searchable_attributes: Option<CS<String>>,
}
impl From<SearchQueryGet> for SearchQuery {
@ -102,6 +104,9 @@ impl From<SearchQueryGet> for SearchQuery {
highlight_post_tag: other.highlight_post_tag,
crop_marker: other.crop_marker,
matching_strategy: other.matching_strategy,
restrict_searchable_attributes: other
.restrict_searchable_attributes
.map(|o| o.into_iter().collect()),
}
}
}

View File

@ -73,6 +73,8 @@ pub struct SearchQuery {
pub crop_marker: String,
#[deserr(default, error = DeserrJsonError<InvalidSearchMatchingStrategy>, default)]
pub matching_strategy: MatchingStrategy,
#[deserr(default, error = DeserrJsonError<InvalidRestrictSearchableAttributes>, default)]
pub restrict_searchable_attributes: Option<Vec<String>>,
}
impl SearchQuery {
@ -128,6 +130,8 @@ pub struct SearchQueryWithIndex {
pub crop_marker: String,
#[deserr(default, error = DeserrJsonError<InvalidSearchMatchingStrategy>, default)]
pub matching_strategy: MatchingStrategy,
#[deserr(default, error = DeserrJsonError<InvalidRestrictSearchableAttributes>, default)]
pub restrict_searchable_attributes: Option<Vec<String>>,
}
impl SearchQueryWithIndex {
@ -153,6 +157,7 @@ impl SearchQueryWithIndex {
highlight_post_tag,
crop_marker,
matching_strategy,
restrict_searchable_attributes,
} = self;
(
index_uid,
@ -176,6 +181,7 @@ impl SearchQueryWithIndex {
highlight_post_tag,
crop_marker,
matching_strategy,
restrict_searchable_attributes,
// do not use ..Default::default() here,
// rather add any missing field from `SearchQuery` to `SearchQueryWithIndex`
},
@ -291,6 +297,10 @@ pub fn perform_search(
search.query(query);
}
if let Some(ref searchable) = query.restrict_searchable_attributes {
search.searchable_attributes(searchable);
}
let is_finite_pagination = query.is_finite_pagination();
search.terms_matching_strategy(query.matching_strategy.into());

View File

@ -57,6 +57,7 @@ fn main() -> Result<(), Box<dyn Error>> {
false,
&None,
&None,
None,
GeoSortStrategy::default(),
0,
20,

View File

@ -28,7 +28,7 @@ pub struct Search<'a> {
offset: usize,
limit: usize,
sort_criteria: Option<Vec<AscDesc>>,
searchable_attributes: Option<Vec<String>>,
searchable_attributes: Option<&'a [String]>,
geo_strategy: new::GeoSortStrategy,
terms_matching_strategy: TermsMatchingStrategy,
scoring_strategy: ScoringStrategy,
@ -77,7 +77,7 @@ impl<'a> Search<'a> {
self
}
pub fn searchable_attributes(&mut self, searchable: Vec<String>) -> &mut Search<'a> {
pub fn searchable_attributes(&mut self, searchable: &'a [String]) -> &mut Search<'a> {
self.searchable_attributes = Some(searchable);
self
}
@ -126,6 +126,7 @@ impl<'a> Search<'a> {
self.exhaustive_number_hits,
&self.filter,
&self.sort_criteria,
self.searchable_attributes,
self.geo_strategy,
self.offset,
self.limit,

View File

@ -514,6 +514,7 @@ mod tests {
false,
&None,
&None,
None,
crate::search::new::GeoSortStrategy::default(),
0,
100,

View File

@ -355,6 +355,7 @@ pub fn execute_search(
exhaustive_number_hits: bool,
filters: &Option<Filter>,
sort_criteria: &Option<Vec<AscDesc>>,
searchable_attributes: Option<&[String]>,
geo_strategy: geo_sort::Strategy,
from: usize,
length: usize,