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

142 lines
4.8 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
2020-04-03 01:53:51 +08:00
use log::warn;
use actix_web::web;
use actix_web_macros::get;
use serde::Deserialize;
2019-10-31 22:00:36 +08:00
2020-04-08 01:34:57 +08:00
use crate::error::ResponseError;
use crate::helpers::meilisearch::{IndexSearchExt, SearchResult};
use crate::helpers::Authentication;
use crate::routes::IndexParam;
2020-04-11 01:05:05 +08:00
use crate::Data;
2019-10-31 22:00:36 +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)]
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>,
}
#[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>,
path: web::Path<IndexParam>,
2020-04-08 01:34:57 +08:00
params: web::Query<SearchQuery>,
) -> Result<web::Json<SearchResult>, ResponseError> {
2020-04-11 01:05:05 +08:00
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
2020-04-08 01:34:57 +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
.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 &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(matches) = params.matches {
2019-10-31 22:00:36 +08:00
if matches {
search_builder.get_matches();
}
}
Ok(web::Json(search_builder.search(&reader)?))
2019-10-31 22:00:36 +08:00
}