Ignore unknown facet search query parameters

This commit is contained in:
Kerollmops 2023-06-22 14:59:44 +02:00 committed by Louis Dureuil
parent 26f0fa678d
commit 29b40295b8
No known key found for this signature in database
2 changed files with 34 additions and 94 deletions

View File

@ -36,7 +36,7 @@ use crate::routes::{create_all_stats, Stats};
use crate::search::{ use crate::search::{
FacetSearchResult, MatchingStrategy, SearchQuery, SearchQueryWithIndex, SearchResult, FacetSearchResult, MatchingStrategy, SearchQuery, SearchQueryWithIndex, SearchResult,
DEFAULT_CROP_LENGTH, DEFAULT_CROP_MARKER, DEFAULT_HIGHLIGHT_POST_TAG, DEFAULT_CROP_LENGTH, DEFAULT_CROP_MARKER, DEFAULT_HIGHLIGHT_POST_TAG,
DEFAULT_HIGHLIGHT_PRE_TAG, DEFAULT_SEARCH_LIMIT, DEFAULT_SEARCH_OFFSET, DEFAULT_HIGHLIGHT_PRE_TAG, DEFAULT_SEARCH_LIMIT,
}; };
use crate::Opt; use crate::Opt;
@ -960,27 +960,7 @@ pub struct FacetSearchAggregator {
impl FacetSearchAggregator { impl FacetSearchAggregator {
pub fn from_query(query: &FacetSearchQuery, request: &HttpRequest) -> Self { pub fn from_query(query: &FacetSearchQuery, request: &HttpRequest) -> Self {
let FacetSearchQuery { let FacetSearchQuery { facet_query: _, facet_name, q, filter, matching_strategy } = query;
facet_query: _,
facet_name,
q,
offset,
limit,
page,
hits_per_page,
attributes_to_retrieve,
attributes_to_crop,
crop_length,
attributes_to_highlight,
show_matches_position,
filter,
sort,
facets,
highlight_pre_tag,
highlight_post_tag,
crop_marker,
matching_strategy,
} = query;
let mut ret = Self::default(); let mut ret = Self::default();
ret.timestamp = Some(OffsetDateTime::now_utc()); ret.timestamp = Some(OffsetDateTime::now_utc());
@ -989,23 +969,8 @@ impl FacetSearchAggregator {
ret.user_agents = extract_user_agents(request).into_iter().collect(); ret.user_agents = extract_user_agents(request).into_iter().collect();
ret.facet_names = Some(facet_name.clone()).into_iter().collect(); ret.facet_names = Some(facet_name.clone()).into_iter().collect();
ret.additional_search_parameters_provided = q.is_some() ret.additional_search_parameters_provided =
|| *offset != DEFAULT_SEARCH_OFFSET() q.is_some() || filter.is_some() || *matching_strategy != MatchingStrategy::default();
|| *limit != DEFAULT_SEARCH_LIMIT()
|| page.is_some()
|| hits_per_page.is_some()
|| attributes_to_retrieve.is_some()
|| attributes_to_crop.is_some()
|| *crop_length != DEFAULT_CROP_LENGTH()
|| attributes_to_highlight.is_some()
|| *show_matches_position
|| filter.is_some()
|| sort.is_some()
|| facets.is_some()
|| *highlight_pre_tag != DEFAULT_HIGHLIGHT_PRE_TAG()
|| *highlight_post_tag != DEFAULT_HIGHLIGHT_POST_TAG()
|| *crop_marker != DEFAULT_CROP_MARKER()
|| *matching_strategy != MatchingStrategy::default();
ret ret
} }

View File

@ -1,5 +1,3 @@
use std::collections::{BTreeSet, HashSet};
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use deserr::actix_web::AwebJson; use deserr::actix_web::AwebJson;
@ -26,7 +24,7 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
// TODO improve the error messages // TODO improve the error messages
#[derive(Debug, Clone, Default, PartialEq, deserr::Deserr)] #[derive(Debug, Clone, Default, PartialEq, deserr::Deserr)]
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)] #[deserr(error = DeserrJsonError, rename_all = camelCase)]
pub struct FacetSearchQuery { pub struct FacetSearchQuery {
#[deserr(default, error = DeserrJsonError<InvalidFacetSearchQuery>)] #[deserr(default, error = DeserrJsonError<InvalidFacetSearchQuery>)]
pub facet_query: Option<String>, pub facet_query: Option<String>,
@ -36,40 +34,8 @@ pub struct FacetSearchQuery {
pub q: Option<String>, pub q: Option<String>,
#[deserr(default, error = DeserrJsonError<InvalidSearchVector>)] #[deserr(default, error = DeserrJsonError<InvalidSearchVector>)]
pub vector: Option<Vec<f32>>, pub vector: Option<Vec<f32>>,
#[deserr(default = DEFAULT_SEARCH_OFFSET(), error = DeserrJsonError<InvalidSearchOffset>)]
pub offset: usize,
#[deserr(default = DEFAULT_SEARCH_LIMIT(), error = DeserrJsonError<InvalidSearchLimit>)]
pub limit: usize,
#[deserr(default, error = DeserrJsonError<InvalidSearchPage>)]
pub page: Option<usize>,
#[deserr(default, error = DeserrJsonError<InvalidSearchHitsPerPage>)]
pub hits_per_page: Option<usize>,
#[deserr(default, error = DeserrJsonError<InvalidSearchAttributesToRetrieve>)]
pub attributes_to_retrieve: Option<BTreeSet<String>>,
#[deserr(default, error = DeserrJsonError<InvalidSearchAttributesToCrop>)]
pub attributes_to_crop: Option<Vec<String>>,
#[deserr(default, error = DeserrJsonError<InvalidSearchCropLength>, default = DEFAULT_CROP_LENGTH())]
pub crop_length: usize,
#[deserr(default, error = DeserrJsonError<InvalidSearchAttributesToHighlight>)]
pub attributes_to_highlight: Option<HashSet<String>>,
#[deserr(default, error = DeserrJsonError<InvalidSearchShowMatchesPosition>, default)]
pub show_matches_position: bool,
#[deserr(default, error = DeserrJsonError<InvalidSearchShowRankingScore>, default)]
pub show_ranking_score: bool,
#[deserr(default, error = DeserrJsonError<InvalidSearchShowRankingScoreDetails>, default)]
pub show_ranking_score_details: bool,
#[deserr(default, error = DeserrJsonError<InvalidSearchFilter>)] #[deserr(default, error = DeserrJsonError<InvalidSearchFilter>)]
pub filter: Option<Value>, pub filter: Option<Value>,
#[deserr(default, error = DeserrJsonError<InvalidSearchSort>)]
pub sort: Option<Vec<String>>,
#[deserr(default, error = DeserrJsonError<InvalidSearchFacets>)]
pub facets: Option<Vec<String>>,
#[deserr(default, error = DeserrJsonError<InvalidSearchHighlightPreTag>, default = DEFAULT_HIGHLIGHT_PRE_TAG())]
pub highlight_pre_tag: String,
#[deserr(default, error = DeserrJsonError<InvalidSearchHighlightPostTag>, default = DEFAULT_HIGHLIGHT_POST_TAG())]
pub highlight_post_tag: String,
#[deserr(default, error = DeserrJsonError<InvalidSearchCropMarker>, default = DEFAULT_CROP_MARKER())]
pub crop_marker: String,
#[deserr(default, error = DeserrJsonError<InvalidSearchMatchingStrategy>, default)] #[deserr(default, error = DeserrJsonError<InvalidSearchMatchingStrategy>, default)]
pub matching_strategy: MatchingStrategy, pub matching_strategy: MatchingStrategy,
} }
@ -117,27 +83,36 @@ pub async fn search(
impl From<FacetSearchQuery> for SearchQuery { impl From<FacetSearchQuery> for SearchQuery {
fn from(value: FacetSearchQuery) -> Self { fn from(value: FacetSearchQuery) -> Self {
let FacetSearchQuery {
facet_query: _,
facet_name: _,
q,
vector,
filter,
matching_strategy,
} = value;
SearchQuery { SearchQuery {
q: value.q, q,
offset: value.offset, offset: DEFAULT_SEARCH_OFFSET(),
limit: value.limit, limit: DEFAULT_SEARCH_LIMIT(),
page: value.page, page: None,
hits_per_page: value.hits_per_page, hits_per_page: None,
attributes_to_retrieve: value.attributes_to_retrieve, attributes_to_retrieve: None,
attributes_to_crop: value.attributes_to_crop, attributes_to_crop: None,
crop_length: value.crop_length, crop_length: DEFAULT_CROP_LENGTH(),
attributes_to_highlight: value.attributes_to_highlight, attributes_to_highlight: None,
show_matches_position: value.show_matches_position, show_matches_position: false,
show_ranking_score: value.show_ranking_score, show_ranking_score: false,
show_ranking_score_details: value.show_ranking_score_details, show_ranking_score_details: false,
filter: value.filter, filter,
sort: value.sort, sort: None,
facets: value.facets, facets: None,
highlight_pre_tag: value.highlight_pre_tag, highlight_pre_tag: DEFAULT_HIGHLIGHT_PRE_TAG(),
highlight_post_tag: value.highlight_post_tag, highlight_post_tag: DEFAULT_HIGHLIGHT_POST_TAG(),
crop_marker: value.crop_marker, crop_marker: DEFAULT_CROP_MARKER(),
matching_strategy: value.matching_strategy, matching_strategy,
vector: value.vector, vector,
} }
} }
} }