2020-04-16 14:22:56 +02:00
|
|
|
use std::collections::{HashSet, HashMap};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-02 19:53:51 +02:00
|
|
|
use log::warn;
|
2020-04-22 17:43:51 +02:00
|
|
|
use actix_web::web;
|
2020-04-24 15:00:52 +02:00
|
|
|
use actix_web::HttpResponse;
|
2020-09-12 04:29:17 +02:00
|
|
|
use actix_web::{get, post};
|
2020-06-01 20:22:18 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-05-07 19:25:18 +02:00
|
|
|
use serde_json::Value;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-22 12:03:57 +02:00
|
|
|
use crate::error::{Error, FacetCountError, ResponseError};
|
2020-05-28 19:35:34 +02:00
|
|
|
use crate::helpers::meilisearch::{IndexSearchExt, SearchResult};
|
2020-04-22 17:43:51 +02:00
|
|
|
use crate::helpers::Authentication;
|
2020-04-09 11:11:48 +02:00
|
|
|
use crate::routes::IndexParam;
|
2020-04-10 19:05:05 +02:00
|
|
|
use crate::Data;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-05 22:29:35 +02:00
|
|
|
use meilisearch_core::facets::FacetFilter;
|
2020-05-07 19:25:18 +02:00
|
|
|
use meilisearch_schema::{Schema, FieldId};
|
2020-05-05 22:29:35 +02:00
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
2020-05-29 17:36:55 +02:00
|
|
|
cfg.service(search_with_post)
|
|
|
|
.service(search_with_url_query);
|
2020-04-22 17:43:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 20:22:18 +02:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-10-31 15:00:36 +01:00
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2020-06-01 20:22:18 +02:00
|
|
|
pub struct SearchQuery {
|
2020-05-28 19:35:34 +02:00
|
|
|
q: Option<String>,
|
2019-10-31 15:00:36 +01:00
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
2020-06-01 20:22:18 +02:00
|
|
|
attributes_to_retrieve: Option<String>,
|
|
|
|
attributes_to_crop: Option<String>,
|
|
|
|
crop_length: Option<usize>,
|
|
|
|
attributes_to_highlight: Option<String>,
|
2019-10-31 15:00:36 +01:00
|
|
|
filters: Option<String>,
|
|
|
|
matches: Option<bool>,
|
2020-05-05 22:29:35 +02:00
|
|
|
facet_filters: Option<String>,
|
2020-05-26 17:56:07 +02:00
|
|
|
facets_distribution: Option<String>,
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 19:37:54 +02:00
|
|
|
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
|
|
|
async fn search_with_url_query(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
params: web::Query<SearchQuery>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let search_result = params.search(&path.index_uid, data)?;
|
|
|
|
Ok(HttpResponse::Ok().json(search_result))
|
|
|
|
}
|
|
|
|
|
2020-06-01 20:22:18 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
pub struct SearchQueryPost {
|
2020-05-28 19:35:34 +02:00
|
|
|
q: Option<String>,
|
2020-06-01 20:22:18 +02:00
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
|
|
|
attributes_to_retrieve: Option<Vec<String>>,
|
|
|
|
attributes_to_crop: Option<Vec<String>>,
|
|
|
|
crop_length: Option<usize>,
|
|
|
|
attributes_to_highlight: Option<Vec<String>>,
|
|
|
|
filters: Option<String>,
|
|
|
|
matches: Option<bool>,
|
|
|
|
facet_filters: Option<Value>,
|
|
|
|
facets_distribution: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SearchQueryPost> for SearchQuery {
|
|
|
|
fn from(other: SearchQueryPost) -> SearchQuery {
|
|
|
|
SearchQuery {
|
|
|
|
q: other.q,
|
|
|
|
offset: other.offset,
|
|
|
|
limit: other.limit,
|
|
|
|
attributes_to_retrieve: other.attributes_to_retrieve.map(|attrs| attrs.join(",")),
|
|
|
|
attributes_to_crop: other.attributes_to_crop.map(|attrs| attrs.join(",")),
|
|
|
|
crop_length: other.crop_length,
|
|
|
|
attributes_to_highlight: other.attributes_to_highlight.map(|attrs| attrs.join(",")),
|
|
|
|
filters: other.filters,
|
|
|
|
matches: other.matches,
|
|
|
|
facet_filters: other.facet_filters.map(|f| f.to_string()),
|
|
|
|
facets_distribution: other.facets_distribution.map(|f| format!("{:?}", f)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:37:54 +02:00
|
|
|
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
|
|
|
async fn search_with_post(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-06-01 20:22:18 +02:00
|
|
|
params: web::Json<SearchQueryPost>,
|
2020-05-28 19:37:54 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-06-01 20:22:18 +02:00
|
|
|
let query: SearchQuery = params.0.into();
|
|
|
|
let search_result = query.search(&path.index_uid, data)?;
|
2020-05-28 19:37:54 +02:00
|
|
|
Ok(HttpResponse::Ok().json(search_result))
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
impl SearchQuery {
|
|
|
|
fn search(&self, index_uid: &str, data: web::Data<Data>) -> Result<SearchResult, ResponseError> {
|
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(index_uid)
|
|
|
|
.ok_or(Error::index_not_found(index_uid))?;
|
2020-04-07 19:34:57 +02:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
let reader = data.db.main_read_txn()?;
|
|
|
|
let schema = index
|
|
|
|
.main
|
|
|
|
.schema(&reader)?
|
|
|
|
.ok_or(Error::internal("Impossible to retrieve the schema"))?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
let mut search_builder = index.new_search(self.q.clone());
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
if let Some(offset) = self.offset {
|
|
|
|
search_builder.offset(offset);
|
|
|
|
}
|
|
|
|
if let Some(limit) = self.limit {
|
|
|
|
search_builder.limit(limit);
|
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
let available_attributes = schema.displayed_name();
|
|
|
|
let mut restricted_attributes: HashSet<&str>;
|
|
|
|
match &self.attributes_to_retrieve {
|
|
|
|
Some(attributes_to_retrieve) => {
|
|
|
|
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-09 16:57:08 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 19:35:34 +02:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
restricted_attributes = available_attributes.clone();
|
2020-04-09 16:57:08 +02:00
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
2020-01-03 10:00:04 +01:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
if let Some(ref facet_filters) = self.facet_filters {
|
|
|
|
let attrs = index.main.attributes_for_faceting(&reader)?.unwrap_or_default();
|
|
|
|
search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, &attrs)?);
|
2020-05-07 19:25:18 +02:00
|
|
|
}
|
2020-04-02 19:53:51 +02:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
if let Some(facets) = &self.facets_distribution {
|
|
|
|
match index.main.attributes_for_faceting(&reader)? {
|
|
|
|
Some(ref attrs) => {
|
|
|
|
let field_ids = prepare_facet_list(&facets, &schema, attrs)?;
|
|
|
|
search_builder.add_facets(field_ids);
|
2020-04-02 19:53:51 +02:00
|
|
|
},
|
2020-05-28 19:35:34 +02:00
|
|
|
None => return Err(FacetCountError::NoFacetSet.into()),
|
2020-04-02 19:53:51 +02:00
|
|
|
}
|
2019-11-15 12:04:46 +01:00
|
|
|
}
|
2020-04-02 19:53:51 +02:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
if let Some(attributes_to_crop) = &self.attributes_to_crop {
|
|
|
|
let default_length = self.crop_length.unwrap_or(200);
|
|
|
|
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-02 19:53:51 +02:00
|
|
|
}
|
2020-05-28 19:35:34 +02:00
|
|
|
}
|
|
|
|
search_builder.attributes_to_crop(final_attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(attributes_to_highlight) = &self.attributes_to_highlight {
|
|
|
|
let mut final_attributes: HashSet<String> = HashSet::new();
|
|
|
|
for attribute in attributes_to_highlight.split(',') {
|
|
|
|
if attribute == "*" {
|
|
|
|
for attr in &restricted_attributes {
|
|
|
|
final_attributes.insert(attr.to_string());
|
|
|
|
}
|
2020-06-27 15:10:39 +02:00
|
|
|
} else if available_attributes.contains(attribute) {
|
|
|
|
final_attributes.insert(attribute.to_string());
|
2020-04-02 19:53:51 +02:00
|
|
|
} else {
|
2020-06-27 15:10:39 +02:00
|
|
|
warn!("The attributes {:?} present in attributesToHighlight parameter doesn't exist", attribute);
|
2020-04-02 19:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
search_builder.attributes_to_highlight(final_attributes);
|
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
if let Some(filters) = &self.filters {
|
|
|
|
search_builder.filters(filters.to_string());
|
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
if let Some(matches) = self.matches {
|
|
|
|
if matches {
|
|
|
|
search_builder.get_matches();
|
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
2020-05-28 19:35:34 +02:00
|
|
|
search_builder.search(&reader)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
2020-05-28 19:35:34 +02:00
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-12 11:22:09 +02: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) {
|
2020-05-12 12:19:44 +02:00
|
|
|
let attrs = facet_attrs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|&id| schema.name(id).map(|n| (id, n.to_string())))
|
|
|
|
.collect();
|
|
|
|
return Ok(attrs);
|
2020-05-07 19:25:18 +02:00
|
|
|
}
|
2020-05-12 12:19:44 +02:00
|
|
|
let mut field_ids = Vec::with_capacity(facet_attrs.len());
|
2020-05-12 11:22:09 +02:00
|
|
|
for facet in vals {
|
|
|
|
match facet {
|
|
|
|
Value::String(facet) => {
|
|
|
|
if let Some(id) = schema.id(&facet) {
|
|
|
|
if !facet_attrs.contains(&id) {
|
2020-05-12 14:26:43 +02:00
|
|
|
return Err(FacetCountError::AttributeNotSet(facet));
|
2020-05-12 11:22:09 +02:00
|
|
|
}
|
2020-05-12 12:19:44 +02:00
|
|
|
field_ids.push((id, facet));
|
2020-05-12 11:22:09 +02:00
|
|
|
}
|
2020-05-07 19:25:18 +02:00
|
|
|
}
|
2020-05-12 14:26:43 +02:00
|
|
|
bad_val => return Err(FacetCountError::unexpected_token(bad_val, &["String"])),
|
2020-05-07 19:25:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 11:22:09 +02:00
|
|
|
Ok(field_ids)
|
2020-05-07 19:25:18 +02:00
|
|
|
}
|
2020-06-27 15:10:39 +02:00
|
|
|
bad_val => Err(FacetCountError::unexpected_token(bad_val, &["[String]"]))
|
2020-05-07 19:25:18 +02:00
|
|
|
}
|
|
|
|
}
|