Merge pull request #40 from meilisearch/search-get

search get
This commit is contained in:
marin 2021-02-16 16:49:56 +01:00 committed by GitHub
commit 22c204fea6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 25 deletions

View File

@ -1,7 +1,7 @@
mod search; mod search;
mod updates; mod updates;
pub use search::{SearchQuery, SearchResult}; pub use search::{SearchQuery, SearchResult, DEFAULT_SEARCH_LIMIT};
use std::fs::create_dir_all; use std::fs::create_dir_all;
use std::ops::Deref; use std::ops::Deref;
@ -28,7 +28,7 @@ impl Deref for Data {
#[derive(Clone)] #[derive(Clone)]
pub struct DataInner { pub struct DataInner {
pub index_controller: Arc<LocalIndexController>, pub index_controller: Arc<LocalIndexController>,
api_keys: ApiKeys, pub api_keys: ApiKeys,
options: Opt, options: Opt,
} }

View File

@ -11,7 +11,7 @@ use serde_json::{Value, Map};
use crate::index_controller::IndexController; use crate::index_controller::IndexController;
use super::Data; use super::Data;
const DEFAULT_SEARCH_LIMIT: usize = 20; pub const DEFAULT_SEARCH_LIMIT: usize = 20;
const fn default_search_limit() -> usize { DEFAULT_SEARCH_LIMIT } const fn default_search_limit() -> usize { DEFAULT_SEARCH_LIMIT }

View File

@ -17,7 +17,7 @@ struct KeysResponse {
} }
#[get("/keys", wrap = "Authentication::Admin")] #[get("/keys", wrap = "Authentication::Admin")]
async fn list(_data: web::Data<Data>) -> HttpResponse { async fn list(data: web::Data<Data>) -> HttpResponse {
let api_keys = data.api_keys.clone(); let api_keys = data.api_keys.clone();
HttpResponse::Ok().json(KeysResponse { HttpResponse::Ok().json(KeysResponse {
private: api_keys.private, private: api_keys.private,

View File

@ -1,38 +1,99 @@
use actix_web::{get, post, web, HttpResponse}; use std::collections::HashSet;
use std::convert::{TryFrom, TryInto};
use actix_web::{get, post, web, HttpResponse};
use serde::Deserialize;
use crate::data::{SearchQuery, DEFAULT_SEARCH_LIMIT};
use crate::error::ResponseError; use crate::error::ResponseError;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::IndexParam; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
use crate::data::SearchQuery;
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(search_with_post).service(search_with_url_query); cfg.service(search_with_post).service(search_with_url_query);
} }
//#[derive(Serialize, Deserialize)] #[derive(Deserialize, Debug)]
//#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
//pub struct SearchQuery { pub struct SearchQueryGet {
//q: Option<String>, q: Option<String>,
//offset: Option<usize>, offset: Option<usize>,
//limit: Option<usize>, limit: Option<usize>,
//attributes_to_retrieve: Option<String>, attributes_to_retrieve: Option<String>,
//attributes_to_crop: Option<String>, attributes_to_crop: Option<String>,
//crop_length: Option<usize>, crop_length: Option<usize>,
//attributes_to_highlight: Option<String>, attributes_to_highlight: Option<String>,
//filters: Option<String>, filters: Option<String>,
//matches: Option<bool>, matches: Option<bool>,
//facet_filters: Option<String>, facet_filters: Option<String>,
//facets_distribution: Option<String>, facets_distribution: Option<String>,
//} }
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
.map(|attrs| attrs.split(",").map(String::from).collect::<Vec<_>>());
let attributes_to_crop = other
.attributes_to_crop
.map(|attrs| attrs.split(",").map(String::from).collect::<Vec<_>>());
let attributes_to_highlight = other
.attributes_to_highlight
.map(|attrs| attrs.split(",").map(String::from).collect::<HashSet<_>>());
let facets_distribution = other
.facets_distribution
.map(|attrs| attrs.split(",").map(String::from).collect::<Vec<_>>());
let facet_filters = match other.facet_filters {
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,
filters: other.filters,
matches: other.matches,
facet_filters,
facets_distribution,
facet_condition: None,
})
}
}
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")] #[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_url_query( async fn search_with_url_query(
_data: web::Data<Data>, data: web::Data<Data>,
_path: web::Path<IndexParam>, path: web::Path<IndexParam>,
_params: web::Query<SearchQuery>, params: web::Query<SearchQueryGet>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
todo!() let query: SearchQuery = match params.into_inner().try_into() {
Ok(q) => q,
Err(e) => {
return Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
}
};
let search_result = data.search(&path.index_uid, query);
match search_result {
Ok(docs) => {
let docs = serde_json::to_string(&docs).unwrap();
Ok(HttpResponse::Ok().body(docs))
}
Err(e) => {
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")] #[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]