search first iteration

This commit is contained in:
mpostma 2020-12-24 12:58:34 +01:00
parent 02ef1d41d7
commit 0cd9e62fc6
5 changed files with 329 additions and 44 deletions

View file

@ -1,31 +1,31 @@
use actix_web::{get, post, web, HttpResponse};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use log::error;
use crate::error::ResponseError;
use crate::helpers::Authentication;
use crate::routes::IndexParam;
use crate::Data;
use crate::data::SearchQuery;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(search_with_post).service(search_with_url_query);
}
#[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>,
}
//#[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>,
//}
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_url_query(
@ -36,27 +36,21 @@ async fn search_with_url_query(
todo!()
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SearchQueryPost {
_q: Option<String>,
_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>>,
}
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_post(
_data: web::Data<Data>,
_path: web::Path<IndexParam>,
_params: web::Json<SearchQueryPost>,
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Json<SearchQuery>,
) -> Result<HttpResponse, ResponseError> {
todo!()
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) => {
error!("{}", e);
todo!()
}
}
}