meilisearch/meilisearch-http/src/routes/search.rs

156 lines
5.3 KiB
Rust
Raw Normal View History

2020-04-16 20:22:56 +08:00
use std::collections::{HashSet, HashMap};
2019-10-31 22:00:36 +08:00
use std::time::Duration;
2020-04-03 01:53:51 +08:00
use log::warn;
2019-11-26 18:06:55 +08:00
use meilisearch_core::Index;
2020-04-11 01:05:05 +08:00
use actix_web as aweb;
2020-04-16 20:22:56 +08:00
use actix_web::{get, web};
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-16 20:22:56 +08:00
use crate::helpers::meilisearch::{Error, IndexSearchExt, SearchResult};
use crate::routes::IndexParam;
2020-04-11 01:05:05 +08:00
use crate::Data;
2019-10-31 22:00:36 +08:00
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub 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>,
timeout_ms: Option<u64>,
matches: Option<bool>,
}
2020-04-08 01:34:57 +08:00
#[get("/indexes/{index_uid}/search")]
pub async fn search_with_url_query(
data: web::Data<Data>,
path: web::Path<IndexParam>,
2020-04-08 01:34:57 +08:00
params: web::Query<SearchQuery>,
2020-04-09 16:39:34 +08:00
) -> aweb::Result<web::Json<SearchResult>> {
2020-04-11 01:05:05 +08:00
let index = data
.db
.open_index(path.index_uid.clone())
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
2020-04-08 01:34:57 +08:00
2020-04-11 01:05:05 +08:00
let reader = data
.db
.main_read_txn()
2020-04-09 16:39:34 +08:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2019-10-31 22:00:36 +08:00
2020-01-23 18:30:18 +08:00
let schema = index
.main
2020-04-08 01:34:57 +08:00
.schema(&reader)
2020-04-09 16:39:34 +08:00
.map_err(|err| ResponseError::Internal(err.to_string()))?
2020-04-11 01:05:05 +08:00
.ok_or(ResponseError::Internal(
"Impossible to retrieve the schema".to_string(),
))?;
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 &params.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) = &params.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 => (),
}
}
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) = &params.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) = &params.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(timeout_ms) = params.timeout_ms {
2019-10-31 22:00:36 +08:00
search_builder.timeout(Duration::from_millis(timeout_ms));
}
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();
}
}
let response = match search_builder.search(&reader) {
Ok(response) => response,
2020-04-09 16:39:34 +08:00
Err(Error::Internal(message)) => return Err(ResponseError::Internal(message).into()),
Err(others) => return Err(ResponseError::BadRequest(others.to_string()).into()),
2019-10-31 22:00:36 +08:00
};
2020-04-08 01:34:57 +08:00
Ok(web::Json(response))
2019-10-31 22:00:36 +08:00
}