2021-10-14 02:56:28 +08:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2021-06-29 21:25:18 +08:00
|
|
|
use log::debug;
|
2022-01-12 22:35:33 +08:00
|
|
|
use meilisearch_auth::IndexSearchRules;
|
2021-12-02 23:03:26 +08:00
|
|
|
use meilisearch_error::ResponseError;
|
2021-09-27 21:41:14 +08:00
|
|
|
use meilisearch_lib::index::{default_crop_length, SearchQuery, DEFAULT_SEARCH_LIMIT};
|
2021-09-21 19:23:22 +08:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2021-02-16 22:54:07 +08:00
|
|
|
use serde::Deserialize;
|
2021-10-25 22:41:23 +08:00
|
|
|
use serde_json::Value;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-10-28 22:28:41 +08:00
|
|
|
use crate::analytics::{Analytics, SearchAggregator};
|
2021-06-24 22:25:52 +08:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2022-03-05 03:12:44 +08:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2021-06-23 05:49:34 +08:00
|
|
|
|
2021-07-05 20:29:20 +08:00
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2021-06-23 05:49:34 +08:00
|
|
|
cfg.service(
|
2021-07-05 20:29:20 +08:00
|
|
|
web::resource("")
|
2022-03-05 03:12:44 +08:00
|
|
|
.route(web::get().to(SeqHandler(search_with_url_query)))
|
|
|
|
.route(web::post().to(SeqHandler(search_with_post))),
|
2021-06-23 05:49:34 +08:00
|
|
|
);
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-02-16 22:54:07 +08:00
|
|
|
#[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>,
|
2021-06-22 20:22:36 +08:00
|
|
|
#[serde(default = "default_crop_length")]
|
2021-06-13 18:37:38 +08:00
|
|
|
crop_length: usize,
|
2021-02-16 22:54:07 +08:00
|
|
|
attributes_to_highlight: Option<String>,
|
2021-05-05 00:20:56 +08:00
|
|
|
filter: Option<String>,
|
2021-08-24 18:31:35 +08:00
|
|
|
sort: Option<String>,
|
2021-06-22 20:22:36 +08:00
|
|
|
#[serde(default = "Default::default")]
|
|
|
|
matches: bool,
|
2021-06-23 02:07:23 +08:00
|
|
|
facets_distribution: Option<String>,
|
2021-02-16 22:54:07 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 22:28:10 +08:00
|
|
|
impl From<SearchQueryGet> for SearchQuery {
|
|
|
|
fn from(other: SearchQueryGet) -> Self {
|
2021-02-16 22:54:07 +08:00
|
|
|
let attributes_to_retrieve = other
|
|
|
|
.attributes_to_retrieve
|
2021-08-24 18:31:35 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
|
|
|
let attributes_to_crop = other
|
|
|
|
.attributes_to_crop
|
2021-08-24 18:31:35 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
|
|
|
let attributes_to_highlight = other
|
|
|
|
.attributes_to_highlight
|
2021-08-24 18:31:35 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
2021-06-23 02:07:23 +08:00
|
|
|
let facets_distribution = other
|
|
|
|
.facets_distribution
|
2021-08-24 18:31:35 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
2021-05-05 00:20:56 +08:00
|
|
|
let filter = match other.filter {
|
2021-06-15 22:28:10 +08:00
|
|
|
Some(f) => match serde_json::from_str(&f) {
|
|
|
|
Ok(v) => Some(v),
|
|
|
|
_ => Some(Value::String(f)),
|
2021-05-05 00:22:48 +08:00
|
|
|
},
|
2021-02-16 22:54:07 +08:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2021-09-27 21:41:14 +08:00
|
|
|
let sort = other.sort.map(|attr| fix_sort_query_parameters(&attr));
|
2021-08-24 18:31:35 +08:00
|
|
|
|
2021-06-15 22:28:10 +08:00
|
|
|
Self {
|
2021-02-16 22:54:07 +08:00
|
|
|
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,
|
2021-05-05 00:20:56 +08:00
|
|
|
filter,
|
2021-08-24 18:31:35 +08:00
|
|
|
sort,
|
2021-06-22 17:27:07 +08:00
|
|
|
matches: other.matches,
|
2021-06-23 02:07:23 +08:00
|
|
|
facets_distribution,
|
2021-06-15 22:28:10 +08:00
|
|
|
}
|
2021-02-16 22:54:07 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
/// Incorporate search rules in search query
|
|
|
|
fn add_search_rules(query: &mut SearchQuery, rules: IndexSearchRules) {
|
|
|
|
query.filter = match (query.filter.take(), rules.filter) {
|
|
|
|
(None, rules_filter) => rules_filter,
|
|
|
|
(filter, None) => filter,
|
|
|
|
(Some(filter), Some(rules_filter)) => {
|
|
|
|
let filter = match filter {
|
|
|
|
Value::Array(filter) => filter,
|
|
|
|
filter => vec![filter],
|
|
|
|
};
|
|
|
|
let rules_filter = match rules_filter {
|
|
|
|
Value::Array(rules_filter) => rules_filter,
|
|
|
|
rules_filter => vec![rules_filter],
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Value::Array([filter, rules_filter].concat()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 00:10:45 +08:00
|
|
|
// TODO: TAMO: split on :asc, and :desc, instead of doing some weird things
|
|
|
|
|
2021-09-27 21:41:14 +08:00
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub async fn search_with_url_query(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SEARCH }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-02-16 22:54:07 +08:00
|
|
|
params: web::Query<SearchQueryGet>,
|
2021-10-14 02:56:28 +08:00
|
|
|
req: HttpRequest,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("called with params: {:?}", params);
|
2022-01-12 22:35:33 +08:00
|
|
|
let mut query: SearchQuery = params.into_inner().into();
|
|
|
|
|
|
|
|
let index_uid = path.into_inner();
|
|
|
|
// Tenant token search_rules.
|
|
|
|
if let Some(search_rules) = meilisearch
|
|
|
|
.filters()
|
|
|
|
.search_rules
|
|
|
|
.get_index_search_rules(&index_uid)
|
|
|
|
{
|
|
|
|
add_search_rules(&mut query, search_rules);
|
|
|
|
}
|
2021-10-12 20:54:09 +08:00
|
|
|
|
2021-10-28 22:28:41 +08:00
|
|
|
let mut aggregate = SearchAggregator::from_query(&query, &req);
|
2021-10-12 20:54:09 +08:00
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
let search_result = meilisearch.search(index_uid, query).await;
|
2021-11-02 19:38:01 +08:00
|
|
|
if let Ok(ref search_result) = search_result {
|
|
|
|
aggregate.succeed(search_result);
|
|
|
|
}
|
|
|
|
analytics.get_search(aggregate);
|
|
|
|
|
|
|
|
let search_result = search_result?;
|
2021-07-06 17:54:37 +08:00
|
|
|
|
|
|
|
// Tests that the nb_hits is always set to false
|
|
|
|
#[cfg(test)]
|
|
|
|
assert!(!search_result.exhaustive_nb_hits);
|
|
|
|
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", search_result);
|
2021-06-15 22:28:10 +08:00
|
|
|
Ok(HttpResponse::Ok().json(search_result))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub async fn search_with_post(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SEARCH }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2020-12-24 19:58:34 +08:00
|
|
|
params: web::Json<SearchQuery>,
|
2021-10-14 02:56:28 +08:00
|
|
|
req: HttpRequest,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-01-12 22:35:33 +08:00
|
|
|
let mut query = params.into_inner();
|
2021-10-12 20:54:09 +08:00
|
|
|
debug!("search called with params: {:?}", query);
|
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
let index_uid = path.into_inner();
|
|
|
|
// Tenant token search_rules.
|
|
|
|
if let Some(search_rules) = meilisearch
|
|
|
|
.filters()
|
|
|
|
.search_rules
|
|
|
|
.get_index_search_rules(&index_uid)
|
|
|
|
{
|
|
|
|
add_search_rules(&mut query, search_rules);
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:28:41 +08:00
|
|
|
let mut aggregate = SearchAggregator::from_query(&query, &req);
|
2021-10-12 20:54:09 +08:00
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
let search_result = meilisearch.search(index_uid, query).await;
|
2021-11-02 19:38:01 +08:00
|
|
|
if let Ok(ref search_result) = search_result {
|
|
|
|
aggregate.succeed(search_result);
|
|
|
|
}
|
2021-11-04 15:03:48 +08:00
|
|
|
analytics.post_search(aggregate);
|
2021-11-02 19:38:01 +08:00
|
|
|
|
|
|
|
let search_result = search_result?;
|
2021-07-06 17:54:37 +08:00
|
|
|
|
|
|
|
// Tests that the nb_hits is always set to false
|
|
|
|
#[cfg(test)]
|
|
|
|
assert!(!search_result.exhaustive_nb_hits);
|
|
|
|
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", search_result);
|
2021-06-15 22:28:10 +08:00
|
|
|
Ok(HttpResponse::Ok().json(search_result))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
2021-09-27 21:41:14 +08:00
|
|
|
|
|
|
|
#[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(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|