MeiliSearch/src/routes/search.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2020-12-12 13:32:06 +01:00
use actix_web::{get, post, web, HttpResponse};
2020-12-22 14:02:41 +01:00
use crate::error::ResponseError;
2020-12-12 13:32:06 +01:00
use crate::helpers::Authentication;
use crate::routes::IndexParam;
use crate::Data;
2020-12-24 12:58:34 +01:00
use crate::data::SearchQuery;
2020-12-12 13:32:06 +01:00
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(search_with_post).service(search_with_url_query);
}
2020-12-24 12:58:34 +01: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 13:32:06 +01:00
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_url_query(
2020-12-22 14:02:41 +01:00
_data: web::Data<Data>,
_path: web::Path<IndexParam>,
_params: web::Query<SearchQuery>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2020-12-12 16:04:37 +01:00
todo!()
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> {
2020-12-24 12:58:34 +01: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 15:26:13 +01:00
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
2020-12-24 12:58:34 +01:00
}
}
2020-12-12 13:32:06 +01:00
}