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;
|
2020-04-24 21:00:52 +08:00
|
|
|
use actix_web::HttpResponse;
|
2020-04-22 23:43:51 +08:00
|
|
|
use actix_web_macros::get;
|
2020-04-17 20:52:13 +08:00
|
|
|
use serde::Deserialize;
|
2020-05-08 01:25:18 +08:00
|
|
|
use serde_json::Value;
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-04-08 01:34:57 +08:00
|
|
|
use crate::error::ResponseError;
|
2020-04-24 21:00:52 +08:00
|
|
|
use crate::helpers::meilisearch::IndexSearchExt;
|
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-05-06 04:29:35 +08:00
|
|
|
use meilisearch_core::facets::FacetFilter;
|
2020-05-08 01:25:18 +08:00
|
|
|
use meilisearch_schema::{Schema, FieldId};
|
2020-05-06 04:29:35 +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-05-06 04:29:35 +08:00
|
|
|
facet_filters: Option<String>,
|
2020-05-12 17:22:09 +08:00
|
|
|
facets: Option<String>,
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|
|
|
|
|
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-24 21:00:52 +08:00
|
|
|
) -> Result<HttpResponse, 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-05-06 04:29:35 +08:00
|
|
|
if let Some(ref facet_filters) = params.facet_filters {
|
|
|
|
match index.main.attributes_for_faceting(&reader)? {
|
|
|
|
Some(ref attrs) => { search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, attrs)?); },
|
|
|
|
None => return Err(ResponseError::FacetExpression("can't filter on facets, as no facet is set".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:22:09 +08:00
|
|
|
if let Some(facets) = ¶ms.facets {
|
2020-05-08 01:25:18 +08:00
|
|
|
match index.main.attributes_for_faceting(&reader)? {
|
2020-05-12 17:22:09 +08:00
|
|
|
Some(ref attrs) => {
|
|
|
|
let field_ids = prepare_facet_list(&facets, &schema, attrs)?;
|
|
|
|
search_builder.add_facets(field_ids);
|
|
|
|
},
|
2020-05-08 01:25:18 +08:00
|
|
|
None => return Err(ResponseError::FacetExpression("can't return facets count, as no facet is set".to_string()))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-24 21:00:52 +08:00
|
|
|
Ok(HttpResponse::Ok().json(search_builder.search(&reader)?))
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|
2020-05-08 01:25:18 +08:00
|
|
|
|
2020-05-12 17:22:09 +08:00
|
|
|
/// Parses the incoming string into an array of attributes for which to return a count. It returns
|
|
|
|
/// a Vec of attribute names ascociated with their id.
|
|
|
|
///
|
|
|
|
/// An error is returned if the array is malformed, or if it contains attributes that are
|
|
|
|
/// unexisting, or not set as facets.
|
|
|
|
fn prepare_facet_list(facets: &str, schema: &Schema, facet_attrs: &[FieldId]) -> Result<Vec<(FieldId, String)>, FacetCountError> {
|
|
|
|
let json_array = serde_json::from_str(facets)?;
|
|
|
|
match json_array {
|
|
|
|
Value::Array(vals) => {
|
|
|
|
let wildcard = Value::String("*".to_string());
|
|
|
|
if vals.iter().any(|f| f == &wildcard) {
|
|
|
|
return Ok(Vec::from(facet_attrs));
|
2020-05-08 01:25:18 +08:00
|
|
|
}
|
2020-05-12 17:22:09 +08:00
|
|
|
let mut field_ids = Vec::new();
|
|
|
|
for facet in vals {
|
|
|
|
match facet {
|
|
|
|
Value::String(facet) => {
|
|
|
|
if let Some(id) = schema.id(&facet) {
|
|
|
|
if !facet_attrs.contains(&id) {
|
|
|
|
return Err(ResponseError::FacetExpression("Only attributes set as facet can be counted".to_string())); // TODO make special error
|
|
|
|
}
|
|
|
|
field_ids.push(id);
|
|
|
|
}
|
2020-05-08 01:25:18 +08:00
|
|
|
}
|
2020-05-12 17:22:09 +08:00
|
|
|
bad_val => return Err(ResponseError::FacetExpression(format!("expected String found {}", bad_val)))
|
2020-05-08 01:25:18 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 17:22:09 +08:00
|
|
|
Ok(field_ids)
|
2020-05-08 01:25:18 +08:00
|
|
|
}
|
2020-05-12 17:22:09 +08:00
|
|
|
bad_val => return Err(ResponseError::FacetExpression(format!("expected Array found {}", bad_val)))
|
2020-05-08 01:25:18 +08:00
|
|
|
}
|
|
|
|
}
|