meilisearch/src/routes/search.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2020-12-12 20:32:06 +08:00
use actix_web::{get, post, web, HttpResponse};
2020-12-22 21:02:41 +08:00
use crate::error::ResponseError;
2020-12-12 20:32:06 +08:00
use crate::helpers::Authentication;
use crate::routes::IndexParam;
use crate::Data;
2020-12-24 19:58:34 +08:00
use crate::data::SearchQuery;
2020-12-12 20:32:06 +08:00
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(search_with_post).service(search_with_url_query);
}
2020-12-24 19:58:34 +08:00
//#[derive(Serialize, Deserialize)]
//#[serde(rename_all = "camelCase", deny_unknown_fields)]
//pub struct SearchQuery {
//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>,
//filters: Option<String>,
//matches: Option<bool>,
//facet_filters: Option<String>,
//facets_distribution: Option<String>,
//}
2020-12-12 20:32:06 +08:00
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_url_query(
2020-12-22 21:02:41 +08:00
_data: web::Data<Data>,
_path: web::Path<IndexParam>,
_params: web::Query<SearchQuery>,
2020-12-12 20:32:06 +08:00
) -> Result<HttpResponse, ResponseError> {
2020-12-12 23:04:37 +08:00
todo!()
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> {
2020-12-24 19:58:34 +08:00
let search_result = data.search(&path.index_uid, params.into_inner());
match search_result {
Ok(docs) => {
let docs = serde_json::to_string(&docs).unwrap();
Ok(HttpResponse::Ok().body(docs))
}
Err(e) => {
2021-02-16 22:26:13 +08:00
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
2020-12-24 19:58:34 +08:00
}
}
2020-12-12 20:32:06 +08:00
}