2020-04-16 20:22:56 +08:00
|
|
|
use std::collections::{HashSet, HashMap};
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-04-03 01:53:51 +08:00
|
|
|
use log::warn;
|
2020-04-22 23:43:51 +08:00
|
|
|
use actix_web::web;
|
|
|
|
use actix_web_macros::get;
|
2020-04-17 20:52:13 +08:00
|
|
|
use serde::Deserialize;
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
use crate::error::ResponseError;
|
2020-04-17 20:52:13 +08:00
|
|
|
use crate::helpers::meilisearch::{IndexSearchExt, SearchResult};
|
2020-04-22 23:43:51 +08:00
|
|
|
use crate::helpers::Authentication;
|
2020-04-09 17:11:48 +08:00
|
|
|
use crate::routes::IndexParam;
|
2020-04-11 01:05:05 +08:00
|
|
|
use crate::Data;
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-04-22 23:43:51 +08:00
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(search_with_url_query);
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:00:36 +08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2020-04-22 23:43:51 +08:00
|
|
|
struct SearchQuery {
|
2019-10-31 22:00:36 +08:00
|
|
|
q: String,
|
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
|
|
|
attributes_to_retrieve: Option<String>,
|
|
|
|
attributes_to_crop: Option<String>,
|
|
|
|
crop_length: Option<usize>,
|
|
|
|
attributes_to_highlight: Option<String>,
|
|
|
|
filters: Option<String>,
|
|
|
|
matches: Option<bool>,
|
|
|
|
}
|
|
|
|
|
2020-04-22 23:43:51 +08:00
|
|
|
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
|
|
|
async fn search_with_url_query(
|
2020-04-08 01:34:57 +08:00
|
|
|
data: web::Data<Data>,
|
2020-04-09 17:11:48 +08:00
|
|
|
path: web::Path<IndexParam>,
|
2020-04-08 01:34:57 +08:00
|
|
|
params: web::Query<SearchQuery>,
|
2020-04-17 20:52:13 +08:00
|
|
|
) -> Result<web::Json<SearchResult>, ResponseError> {
|
2020-04-11 01:05:05 +08:00
|
|
|
let index = data
|
|
|
|
.db
|
2020-04-17 20:52:13 +08:00
|
|
|
.open_index(&path.index_uid)
|
|
|
|
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
|
2020-04-08 01:34:57 +08:00
|
|
|
|
2020-04-17 20:52:13 +08:00
|
|
|
let reader = data.db.main_read_txn()?;
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let schema = index
|
|
|
|
.main
|
2020-04-17 20:52:13 +08:00
|
|
|
.schema(&reader)?
|
|
|
|
.ok_or(ResponseError::internal("Impossible to retrieve the schema"))?;
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
let mut search_builder = index.new_search(params.q.clone());
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
if let Some(offset) = params.offset {
|
2019-10-31 22:00:36 +08:00
|
|
|
search_builder.offset(offset);
|
|
|
|
}
|
2020-04-08 01:34:57 +08:00
|
|
|
if let Some(limit) = params.limit {
|
2019-10-31 22:00:36 +08:00
|
|
|
search_builder.limit(limit);
|
|
|
|
}
|
|
|
|
|
2020-04-03 01:53:51 +08:00
|
|
|
let available_attributes = schema.displayed_name();
|
|
|
|
let mut restricted_attributes: HashSet<&str>;
|
2020-04-08 01:34:57 +08:00
|
|
|
match ¶ms.attributes_to_retrieve {
|
2020-04-03 01:53:51 +08:00
|
|
|
Some(attributes_to_retrieve) => {
|
2020-04-09 22:57:08 +08:00
|
|
|
let attributes_to_retrieve: HashSet<&str> = attributes_to_retrieve.split(',').collect();
|
|
|
|
if attributes_to_retrieve.contains("*") {
|
|
|
|
restricted_attributes = available_attributes.clone();
|
|
|
|
} else {
|
|
|
|
restricted_attributes = HashSet::new();
|
|
|
|
for attr in attributes_to_retrieve {
|
|
|
|
if available_attributes.contains(attr) {
|
|
|
|
restricted_attributes.insert(attr);
|
|
|
|
search_builder.add_retrievable_field(attr.to_string());
|
|
|
|
} else {
|
|
|
|
warn!("The attributes {:?} present in attributesToCrop parameter doesn't exist", attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 01:53:51 +08:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
restricted_attributes = available_attributes.clone();
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 17:00:04 +08:00
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
if let Some(attributes_to_crop) = ¶ms.attributes_to_crop {
|
|
|
|
let default_length = params.crop_length.unwrap_or(200);
|
2020-04-03 01:53:51 +08:00
|
|
|
let mut final_attributes: HashMap<String, usize> = HashMap::new();
|
|
|
|
|
|
|
|
for attribute in attributes_to_crop.split(',') {
|
|
|
|
let mut attribute = attribute.split(':');
|
|
|
|
let attr = attribute.next();
|
|
|
|
let length = attribute.next().and_then(|s| s.parse().ok()).unwrap_or(default_length);
|
|
|
|
match attr {
|
|
|
|
Some("*") => {
|
|
|
|
for attr in &restricted_attributes {
|
|
|
|
final_attributes.insert(attr.to_string(), length);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(attr) => {
|
|
|
|
if available_attributes.contains(attr) {
|
|
|
|
final_attributes.insert(attr.to_string(), length);
|
|
|
|
} else {
|
|
|
|
warn!("The attributes {:?} present in attributesToCrop parameter doesn't exist", attr);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
}
|
2019-11-15 19:04:46 +08:00
|
|
|
}
|
2020-04-03 01:53:51 +08:00
|
|
|
|
|
|
|
search_builder.attributes_to_crop(final_attributes);
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
if let Some(attributes_to_highlight) = ¶ms.attributes_to_highlight {
|
2020-04-03 01:53:51 +08:00
|
|
|
let mut final_attributes: HashSet<String> = HashSet::new();
|
2020-04-09 22:57:08 +08:00
|
|
|
for attribute in attributes_to_highlight.split(',') {
|
2020-04-03 01:53:51 +08:00
|
|
|
if attribute == "*" {
|
|
|
|
for attr in &restricted_attributes {
|
|
|
|
final_attributes.insert(attr.to_string());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if available_attributes.contains(attribute) {
|
|
|
|
final_attributes.insert(attribute.to_string());
|
|
|
|
} else {
|
|
|
|
warn!("The attributes {:?} present in attributesToHighlight parameter doesn't exist", attribute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
search_builder.attributes_to_highlight(final_attributes);
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
if let Some(filters) = ¶ms.filters {
|
|
|
|
search_builder.filters(filters.to_string());
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
if let Some(matches) = params.matches {
|
2019-10-31 22:00:36 +08:00
|
|
|
if matches {
|
|
|
|
search_builder.get_matches();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:52:13 +08:00
|
|
|
Ok(web::Json(search_builder.search(&reader)?))
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|