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

102 lines
3.3 KiB
Rust
Raw Normal View History

2021-06-16 16:18:55 +02:00
use std::collections::{BTreeSet, HashSet};
2021-02-16 15:54:07 +01:00
2020-12-12 13:32:06 +01:00
use actix_web::{get, post, web, HttpResponse};
2021-06-23 12:18:34 +02:00
use log::debug;
2021-02-16 15:54:07 +01:00
use serde::Deserialize;
use serde_json::Value;
2020-12-12 13:32:06 +01:00
2020-12-22 14:02:41 +01:00
use crate::error::ResponseError;
2020-12-12 13:32:06 +01:00
use crate::helpers::Authentication;
2021-06-23 14:48:33 +02:00
use crate::index::{default_crop_length, SearchQuery, DEFAULT_SEARCH_LIMIT};
2020-12-12 13:32:06 +01:00
use crate::routes::IndexParam;
use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(search_with_post).service(search_with_url_query);
}
2021-02-16 15:54:07 +01:00
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SearchQueryGet {
q: Option<String>,
offset: Option<usize>,
limit: Option<usize>,
attributes_to_retrieve: Option<String>,
attributes_to_crop: Option<String>,
2021-06-22 14:22:36 +02:00
#[serde(default = "default_crop_length")]
crop_length: usize,
2021-02-16 15:54:07 +01:00
attributes_to_highlight: Option<String>,
filter: Option<String>,
2021-06-22 14:22:36 +02:00
#[serde(default = "Default::default")]
matches: bool,
2021-06-22 20:07:23 +02:00
facets_distribution: Option<String>,
2021-02-16 15:54:07 +01:00
}
impl From<SearchQueryGet> for SearchQuery {
fn from(other: SearchQueryGet) -> Self {
2021-02-16 15:54:07 +01:00
let attributes_to_retrieve = other
.attributes_to_retrieve
2021-06-16 16:18:55 +02:00
.map(|attrs| attrs.split(',').map(String::from).collect::<BTreeSet<_>>());
2021-02-16 15:54:07 +01:00
let attributes_to_crop = other
.attributes_to_crop
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>());
2021-02-16 15:54:07 +01:00
let attributes_to_highlight = other
.attributes_to_highlight
2021-03-15 16:52:05 +01:00
.map(|attrs| attrs.split(',').map(String::from).collect::<HashSet<_>>());
2021-02-16 15:54:07 +01:00
2021-06-22 20:07:23 +02:00
let facets_distribution = other
.facets_distribution
2021-03-15 16:52:05 +01:00
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>());
2021-02-16 15:54:07 +01:00
let filter = match other.filter {
Some(f) => match serde_json::from_str(&f) {
Ok(v) => Some(v),
_ => Some(Value::String(f)),
2021-05-04 18:22:48 +02:00
},
2021-02-16 15:54:07 +01:00
None => None,
};
Self {
2021-02-16 15:54:07 +01:00
q: other.q,
offset: other.offset,
limit: other.limit.unwrap_or(DEFAULT_SEARCH_LIMIT),
attributes_to_retrieve,
attributes_to_crop,
crop_length: other.crop_length,
attributes_to_highlight,
filter,
matches: other.matches,
2021-06-22 20:07:23 +02:00
facets_distribution,
}
2021-02-16 15:54:07 +01:00
}
}
2020-12-12 13:32:06 +01:00
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_url_query(
2021-02-16 15:54:07 +01:00
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<SearchQueryGet>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 12:18:34 +02:00
debug!("called with params: {:?}", params);
let query = params.into_inner().into();
let search_result = data.search(path.into_inner().index_uid, query).await?;
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", search_result);
Ok(HttpResponse::Ok().json(search_result))
2020-12-12 13:32:06 +01:00
}
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_post(
2020-12-24 12:58:34 +01:00
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Json<SearchQuery>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 12:18:34 +02:00
debug!("search called with params: {:?}", params);
2021-03-15 18:11:10 +01:00
let search_result = data
.search(path.into_inner().index_uid, params.into_inner())
.await?;
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", search_result);
Ok(HttpResponse::Ok().json(search_result))
2020-12-12 13:32:06 +01:00
}