2021-10-13 20:56:28 +02:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2021-06-29 15:25:18 +02:00
|
|
|
use log::debug;
|
2022-01-12 15:35:33 +01:00
|
|
|
use meilisearch_auth::IndexSearchRules;
|
2022-04-07 11:27:06 +02:00
|
|
|
use meilisearch_lib::index::{
|
2022-08-23 16:30:56 +02:00
|
|
|
MatchingStrategy, SearchQuery, DEFAULT_CROP_LENGTH, DEFAULT_CROP_MARKER,
|
|
|
|
DEFAULT_HIGHLIGHT_POST_TAG, DEFAULT_HIGHLIGHT_PRE_TAG, DEFAULT_SEARCH_LIMIT,
|
2022-04-07 11:27:06 +02:00
|
|
|
};
|
2021-09-21 13:23:22 +02:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2022-06-06 12:38:46 +02:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2021-02-16 15:54:07 +01:00
|
|
|
use serde::Deserialize;
|
2022-06-02 11:14:46 +02:00
|
|
|
use serde_cs::vec::CS;
|
2021-10-25 16:41:23 +02:00
|
|
|
use serde_json::Value;
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2021-10-28 16:28:41 +02:00
|
|
|
use crate::analytics::{Analytics, SearchAggregator};
|
2021-06-24 16:25:52 +02:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2022-03-04 20:12:44 +01:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2021-06-22 23:49:34 +02:00
|
|
|
|
2021-07-05 14:29:20 +02:00
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2021-06-22 23:49:34 +02:00
|
|
|
cfg.service(
|
2021-07-05 14:29:20 +02:00
|
|
|
web::resource("")
|
2022-03-04 20:12:44 +01:00
|
|
|
.route(web::get().to(SeqHandler(search_with_url_query)))
|
|
|
|
.route(web::post().to(SeqHandler(search_with_post))),
|
2021-06-22 23:49:34 +02:00
|
|
|
);
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 15:54:07 +01:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
pub struct SearchQueryGet {
|
|
|
|
q: Option<String>,
|
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
2022-06-22 12:33:15 +02:00
|
|
|
attributes_to_retrieve: Option<CS<String>>,
|
|
|
|
attributes_to_crop: Option<CS<String>>,
|
2022-06-02 10:48:02 +02:00
|
|
|
#[serde(default = "DEFAULT_CROP_LENGTH")]
|
2021-06-13 12:37:38 +02:00
|
|
|
crop_length: usize,
|
2022-06-22 12:33:15 +02:00
|
|
|
attributes_to_highlight: Option<CS<String>>,
|
2021-05-04 18:20:56 +02:00
|
|
|
filter: Option<String>,
|
2021-08-24 12:31:35 +02:00
|
|
|
sort: Option<String>,
|
2021-06-22 14:22:36 +02:00
|
|
|
#[serde(default = "Default::default")]
|
2022-05-18 13:17:56 +02:00
|
|
|
show_matches_position: bool,
|
2022-06-22 12:33:15 +02:00
|
|
|
facets: Option<CS<String>>,
|
2022-06-02 10:48:02 +02:00
|
|
|
#[serde(default = "DEFAULT_HIGHLIGHT_PRE_TAG")]
|
2022-04-07 11:27:06 +02:00
|
|
|
highlight_pre_tag: String,
|
2022-06-02 10:48:02 +02:00
|
|
|
#[serde(default = "DEFAULT_HIGHLIGHT_POST_TAG")]
|
2022-04-07 11:27:06 +02:00
|
|
|
highlight_post_tag: String,
|
2022-06-02 10:48:02 +02:00
|
|
|
#[serde(default = "DEFAULT_CROP_MARKER")]
|
2022-04-07 11:27:06 +02:00
|
|
|
crop_marker: String,
|
2022-08-23 16:30:56 +02:00
|
|
|
#[serde(default)]
|
|
|
|
matching_strategy: MatchingStrategy,
|
2021-02-16 15:54:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-15 16:28:10 +02:00
|
|
|
impl From<SearchQueryGet> for SearchQuery {
|
|
|
|
fn from(other: SearchQueryGet) -> Self {
|
2021-05-04 18:20:56 +02:00
|
|
|
let filter = match other.filter {
|
2021-06-15 16:28:10 +02:00
|
|
|
Some(f) => match serde_json::from_str(&f) {
|
|
|
|
Ok(v) => Some(v),
|
|
|
|
_ => Some(Value::String(f)),
|
2021-05-04 18:22:48 +02:00
|
|
|
},
|
2021-02-16 15:54:07 +01:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2021-06-15 16:28:10 +02:00
|
|
|
Self {
|
2021-02-16 15:54:07 +01:00
|
|
|
q: other.q,
|
|
|
|
offset: other.offset,
|
2022-06-02 10:48:02 +02:00
|
|
|
limit: other.limit.unwrap_or_else(DEFAULT_SEARCH_LIMIT),
|
2022-06-22 12:33:15 +02:00
|
|
|
attributes_to_retrieve: other
|
|
|
|
.attributes_to_retrieve
|
|
|
|
.map(|o| o.into_iter().collect()),
|
|
|
|
attributes_to_crop: other.attributes_to_crop.map(|o| o.into_iter().collect()),
|
2021-02-16 15:54:07 +01:00
|
|
|
crop_length: other.crop_length,
|
2022-06-22 12:33:15 +02:00
|
|
|
attributes_to_highlight: other
|
|
|
|
.attributes_to_highlight
|
|
|
|
.map(|o| o.into_iter().collect()),
|
2021-05-04 18:20:56 +02:00
|
|
|
filter,
|
2022-06-06 10:17:33 +02:00
|
|
|
sort: other.sort.map(|attr| fix_sort_query_parameters(&attr)),
|
2022-05-18 13:17:56 +02:00
|
|
|
show_matches_position: other.show_matches_position,
|
2022-06-22 12:33:15 +02:00
|
|
|
facets: other.facets.map(|o| o.into_iter().collect()),
|
2022-04-07 11:27:06 +02:00
|
|
|
highlight_pre_tag: other.highlight_pre_tag,
|
|
|
|
highlight_post_tag: other.highlight_post_tag,
|
|
|
|
crop_marker: other.crop_marker,
|
2022-08-23 16:30:56 +02:00
|
|
|
matching_strategy: other.matching_strategy,
|
2021-06-15 16:28:10 +02:00
|
|
|
}
|
2021-02-16 15:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2022-01-12 15:35:33 +01: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-28 18:10:45 +02:00
|
|
|
// TODO: TAMO: split on :asc, and :desc, instead of doing some weird things
|
|
|
|
|
2021-09-27 15:41:14 +02: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() {
|
2022-07-04 12:00:03 +02:00
|
|
|
let s = sort_parameters.last_mut().unwrap();
|
|
|
|
s.push(',');
|
|
|
|
s.push_str(current_sort);
|
2021-09-27 15:41:14 +02:00
|
|
|
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 16:20:22 +02:00
|
|
|
pub async fn search_with_url_query(
|
2021-11-08 18:31:27 +01:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SEARCH }>, MeiliSearch>,
|
2021-12-02 16:03:26 +01:00
|
|
|
path: web::Path<String>,
|
2021-02-16 15:54:07 +01:00
|
|
|
params: web::Query<SearchQueryGet>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2021-10-29 16:10:58 +02:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("called with params: {:?}", params);
|
2022-01-12 15:35:33 +01: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 14:54:09 +02:00
|
|
|
|
2021-10-28 16:28:41 +02:00
|
|
|
let mut aggregate = SearchAggregator::from_query(&query, &req);
|
2021-10-12 14:54:09 +02:00
|
|
|
|
2022-01-12 15:35:33 +01:00
|
|
|
let search_result = meilisearch.search(index_uid, query).await;
|
2021-11-02 12:38:01 +01: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 11:54:37 +02:00
|
|
|
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("returns: {:?}", search_result);
|
2021-06-15 16:28:10 +02:00
|
|
|
Ok(HttpResponse::Ok().json(search_result))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
2021-07-07 16:20:22 +02:00
|
|
|
pub async fn search_with_post(
|
2021-11-08 18:31:27 +01:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SEARCH }>, MeiliSearch>,
|
2021-12-02 16:03:26 +01:00
|
|
|
path: web::Path<String>,
|
2020-12-24 12:58:34 +01:00
|
|
|
params: web::Json<SearchQuery>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2021-10-29 16:10:58 +02:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-01-12 15:35:33 +01:00
|
|
|
let mut query = params.into_inner();
|
2021-10-12 14:54:09 +02:00
|
|
|
debug!("search called with params: {:?}", query);
|
|
|
|
|
2022-01-12 15:35:33 +01: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 16:28:41 +02:00
|
|
|
let mut aggregate = SearchAggregator::from_query(&query, &req);
|
2021-10-12 14:54:09 +02:00
|
|
|
|
2022-01-12 15:35:33 +01:00
|
|
|
let search_result = meilisearch.search(index_uid, query).await;
|
2021-11-02 12:38:01 +01:00
|
|
|
if let Ok(ref search_result) = search_result {
|
|
|
|
aggregate.succeed(search_result);
|
|
|
|
}
|
2021-11-04 08:03:48 +01:00
|
|
|
analytics.post_search(aggregate);
|
2021-11-02 12:38:01 +01:00
|
|
|
|
|
|
|
let search_result = search_result?;
|
2021-07-06 11:54:37 +02:00
|
|
|
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("returns: {:?}", search_result);
|
2021-06-15 16:28:10 +02:00
|
|
|
Ok(HttpResponse::Ok().json(search_result))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
2021-09-27 15:41:14 +02: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(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|