2021-02-16 22:54:07 +08:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::convert::{TryFrom, TryInto};
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
use actix_web::{get, post, web, HttpResponse};
|
2021-02-16 22:54:07 +08:00
|
|
|
use serde::Deserialize;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2020-12-22 21:02:41 +08:00
|
|
|
use crate::error::ResponseError;
|
2020-12-12 20:32:06 +08:00
|
|
|
use crate::helpers::Authentication;
|
2021-03-16 01:11:10 +08:00
|
|
|
use crate::index::{SearchQuery, DEFAULT_SEARCH_LIMIT};
|
2020-12-12 20:32:06 +08: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 22:54:07 +08: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>,
|
|
|
|
crop_length: Option<usize>,
|
|
|
|
attributes_to_highlight: Option<String>,
|
2021-05-05 00:20:56 +08:00
|
|
|
filter: Option<String>,
|
2021-02-16 22:54:07 +08:00
|
|
|
matches: Option<bool>,
|
2021-02-17 21:52:30 +08:00
|
|
|
facet_distributions: Option<String>,
|
2021-02-16 22:54:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<SearchQueryGet> for SearchQuery {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
|
|
|
|
fn try_from(other: SearchQueryGet) -> anyhow::Result<Self> {
|
|
|
|
let attributes_to_retrieve = other
|
|
|
|
.attributes_to_retrieve
|
2021-04-19 22:22:41 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect::<HashSet<_>>());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
|
|
|
let attributes_to_crop = other
|
|
|
|
.attributes_to_crop
|
2021-04-19 22:22:41 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect::<HashSet<_>>());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
|
|
|
let attributes_to_highlight = other
|
|
|
|
.attributes_to_highlight
|
2021-03-15 23:52:05 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect::<HashSet<_>>());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
2021-02-17 21:52:30 +08:00
|
|
|
let facet_distributions = other
|
|
|
|
.facet_distributions
|
2021-03-15 23:52:05 +08:00
|
|
|
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>());
|
2021-02-16 22:54:07 +08:00
|
|
|
|
2021-05-05 00:20:56 +08:00
|
|
|
let filter = match other.filter {
|
2021-02-16 22:54:07 +08:00
|
|
|
Some(ref f) => Some(serde_json::from_str(f)?),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
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,
|
2021-05-05 00:20:56 +08:00
|
|
|
filter,
|
2021-02-16 22:54:07 +08:00
|
|
|
matches: other.matches,
|
2021-02-17 21:52:30 +08:00
|
|
|
facet_distributions,
|
2021-02-16 22:54:07 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
|
|
|
async fn search_with_url_query(
|
2021-02-16 22:54:07 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
params: web::Query<SearchQueryGet>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-16 22:54:07 +08:00
|
|
|
let query: SearchQuery = match params.into_inner().try_into() {
|
|
|
|
Ok(q) => q,
|
|
|
|
Err(e) => {
|
2021-03-16 01:11:10 +08:00
|
|
|
return Ok(
|
2021-03-16 23:09:14 +08:00
|
|
|
HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))
|
2021-03-16 01:11:10 +08:00
|
|
|
)
|
2021-02-16 22:54:07 +08:00
|
|
|
}
|
|
|
|
};
|
2021-03-15 23:52:05 +08:00
|
|
|
let search_result = data.search(path.into_inner().index_uid, query).await;
|
2021-02-16 22:54:07 +08:00
|
|
|
match search_result {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(docs) => Ok(HttpResponse::Ok().json(docs)),
|
2021-02-16 22:54:07 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2021-02-16 22:54:07 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
|
|
|
async fn search_with_post(
|
2020-12-24 19:58:34 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
params: web::Json<SearchQuery>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-16 01:11:10 +08:00
|
|
|
let search_result = data
|
|
|
|
.search(path.into_inner().index_uid, params.into_inner())
|
|
|
|
.await;
|
2020-12-24 19:58:34 +08:00
|
|
|
match search_result {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(docs) => Ok(HttpResponse::Ok().json(docs)),
|
2020-12-24 19:58:34 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2020-12-24 19:58:34 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|