mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-12-05 03:25:06 +08:00
199 lines
6.3 KiB
Rust
199 lines
6.3 KiB
Rust
use actix_web::{web, HttpRequest, HttpResponse};
|
|
use log::debug;
|
|
use meilisearch_lib::index::{default_crop_length, SearchQuery, DEFAULT_SEARCH_LIMIT};
|
|
use meilisearch_lib::MeiliSearch;
|
|
use serde::Deserialize;
|
|
use serde_json::Value;
|
|
|
|
use crate::analytics::Analytics;
|
|
use crate::error::ResponseError;
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
|
use crate::routes::IndexParam;
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(
|
|
web::resource("")
|
|
.route(web::get().to(search_with_url_query))
|
|
.route(web::post().to(search_with_post)),
|
|
);
|
|
}
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
pub struct SearchQueryGet {
|
|
q: Option<String>,
|
|
offset: Option<usize>,
|
|
limit: Option<usize>,
|
|
attributes_to_retrieve: Option<String>,
|
|
attributes_to_crop: Option<String>,
|
|
#[serde(default = "default_crop_length")]
|
|
crop_length: usize,
|
|
attributes_to_highlight: Option<String>,
|
|
filter: Option<String>,
|
|
sort: Option<String>,
|
|
#[serde(default = "Default::default")]
|
|
matches: bool,
|
|
facets_distribution: Option<String>,
|
|
}
|
|
|
|
impl From<SearchQueryGet> for SearchQuery {
|
|
fn from(other: SearchQueryGet) -> Self {
|
|
let attributes_to_retrieve = other
|
|
.attributes_to_retrieve
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
|
|
|
let attributes_to_crop = other
|
|
.attributes_to_crop
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
|
|
|
let attributes_to_highlight = other
|
|
.attributes_to_highlight
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
|
|
|
let facets_distribution = other
|
|
.facets_distribution
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
|
|
|
let filter = match other.filter {
|
|
Some(f) => match serde_json::from_str(&f) {
|
|
Ok(v) => Some(v),
|
|
_ => Some(Value::String(f)),
|
|
},
|
|
None => None,
|
|
};
|
|
|
|
let sort = other.sort.map(|attr| fix_sort_query_parameters(&attr));
|
|
|
|
Self {
|
|
q: other.q,
|
|
offset: other.offset,
|
|
limit: other.limit.unwrap_or(DEFAULT_SEARCH_LIMIT),
|
|
attributes_to_retrieve,
|
|
attributes_to_crop,
|
|
crop_length: other.crop_length,
|
|
attributes_to_highlight,
|
|
filter,
|
|
sort,
|
|
matches: other.matches,
|
|
facets_distribution,
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO: TAMO: split on :asc, and :desc, instead of doing some weird things
|
|
|
|
/// Transform the sort query parameter into something that matches the post expected format.
|
|
fn fix_sort_query_parameters(sort_query: &str) -> Vec<String> {
|
|
let mut sort_parameters = Vec::new();
|
|
let mut merge = false;
|
|
for current_sort in sort_query.trim_matches('"').split(',').map(|s| s.trim()) {
|
|
if current_sort.starts_with("_geoPoint(") {
|
|
sort_parameters.push(current_sort.to_string());
|
|
merge = true;
|
|
} else if merge && !sort_parameters.is_empty() {
|
|
sort_parameters
|
|
.last_mut()
|
|
.unwrap()
|
|
.push_str(&format!(",{}", current_sort));
|
|
if current_sort.ends_with("):desc") || current_sort.ends_with("):asc") {
|
|
merge = false;
|
|
}
|
|
} else {
|
|
sort_parameters.push(current_sort.to_string());
|
|
merge = false;
|
|
}
|
|
}
|
|
sort_parameters
|
|
}
|
|
|
|
pub async fn search_with_url_query(
|
|
meilisearch: GuardedData<Public, MeiliSearch>,
|
|
path: web::Path<IndexParam>,
|
|
params: web::Query<SearchQueryGet>,
|
|
req: HttpRequest,
|
|
analytics: web::Data<&'static dyn Analytics>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
debug!("called with params: {:?}", params);
|
|
let query: SearchQuery = params.into_inner().into();
|
|
|
|
analytics.start_get_search(&query, &req);
|
|
|
|
let search_result = meilisearch
|
|
.search(path.into_inner().index_uid, query)
|
|
.await?;
|
|
|
|
// Tests that the nb_hits is always set to false
|
|
#[cfg(test)]
|
|
assert!(!search_result.exhaustive_nb_hits);
|
|
|
|
analytics.end_post_search(search_result.processing_time_ms as usize);
|
|
|
|
debug!("returns: {:?}", search_result);
|
|
Ok(HttpResponse::Ok().json(search_result))
|
|
}
|
|
|
|
pub async fn search_with_post(
|
|
meilisearch: GuardedData<Public, MeiliSearch>,
|
|
path: web::Path<IndexParam>,
|
|
params: web::Json<SearchQuery>,
|
|
req: HttpRequest,
|
|
analytics: web::Data<&'static dyn Analytics>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
let query = params.into_inner();
|
|
debug!("search called with params: {:?}", query);
|
|
|
|
analytics.start_post_search(&query, &req);
|
|
|
|
let search_result = meilisearch
|
|
.search(path.into_inner().index_uid, query)
|
|
.await?;
|
|
|
|
// Tests that the nb_hits is always set to false
|
|
#[cfg(test)]
|
|
assert!(!search_result.exhaustive_nb_hits);
|
|
|
|
analytics.end_post_search(search_result.processing_time_ms as usize);
|
|
|
|
debug!("returns: {:?}", search_result);
|
|
Ok(HttpResponse::Ok().json(search_result))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_fix_sort_query_parameters() {
|
|
let sort = fix_sort_query_parameters("_geoPoint(12, 13):asc");
|
|
assert_eq!(sort, vec!["_geoPoint(12,13):asc".to_string()]);
|
|
let sort = fix_sort_query_parameters("doggo:asc,_geoPoint(12.45,13.56):desc");
|
|
assert_eq!(
|
|
sort,
|
|
vec![
|
|
"doggo:asc".to_string(),
|
|
"_geoPoint(12.45,13.56):desc".to_string(),
|
|
]
|
|
);
|
|
let sort = fix_sort_query_parameters(
|
|
"doggo:asc , _geoPoint(12.45, 13.56, 2590352):desc , catto:desc",
|
|
);
|
|
assert_eq!(
|
|
sort,
|
|
vec![
|
|
"doggo:asc".to_string(),
|
|
"_geoPoint(12.45,13.56,2590352):desc".to_string(),
|
|
"catto:desc".to_string(),
|
|
]
|
|
);
|
|
let sort = fix_sort_query_parameters("doggo:asc , _geoPoint(1, 2), catto:desc");
|
|
// This is ugly but eh, I don't want to write a full parser just for this unused route
|
|
assert_eq!(
|
|
sort,
|
|
vec![
|
|
"doggo:asc".to_string(),
|
|
"_geoPoint(1,2),catto:desc".to_string(),
|
|
]
|
|
);
|
|
}
|
|
}
|