mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-26 12:05:05 +08:00
enable response error for search routes
This commit is contained in:
parent
8afbb9c462
commit
439db1aae0
@ -2,8 +2,8 @@ use std::collections::{BTreeSet, HashSet};
|
|||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::{TryFrom, TryInto};
|
||||||
|
|
||||||
use actix_web::{get, post, web, HttpResponse};
|
use actix_web::{get, post, web, HttpResponse};
|
||||||
use serde_json::Value;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::error::ResponseError;
|
use crate::error::ResponseError;
|
||||||
use crate::helpers::Authentication;
|
use crate::helpers::Authentication;
|
||||||
@ -30,10 +30,8 @@ pub struct SearchQueryGet {
|
|||||||
facet_distributions: Option<String>,
|
facet_distributions: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<SearchQueryGet> for SearchQuery {
|
impl From<SearchQueryGet> for SearchQuery {
|
||||||
type Error = Box<dyn std::error::Error>;
|
fn from(other: SearchQueryGet) -> Self {
|
||||||
|
|
||||||
fn try_from(other: SearchQueryGet) -> Result<Self, Self::Error> {
|
|
||||||
let attributes_to_retrieve = other
|
let attributes_to_retrieve = other
|
||||||
.attributes_to_retrieve
|
.attributes_to_retrieve
|
||||||
.map(|attrs| attrs.split(',').map(String::from).collect::<BTreeSet<_>>());
|
.map(|attrs| attrs.split(',').map(String::from).collect::<BTreeSet<_>>());
|
||||||
@ -51,16 +49,14 @@ impl TryFrom<SearchQueryGet> for SearchQuery {
|
|||||||
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>());
|
.map(|attrs| attrs.split(',').map(String::from).collect::<Vec<_>>());
|
||||||
|
|
||||||
let filter = match other.filter {
|
let filter = match other.filter {
|
||||||
Some(f) => {
|
Some(f) => match serde_json::from_str(&f) {
|
||||||
match serde_json::from_str(&f) {
|
Ok(v) => Some(v),
|
||||||
Ok(v) => Some(v),
|
_ => Some(Value::String(f)),
|
||||||
_ => Some(Value::String(f)),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self {
|
Self {
|
||||||
q: other.q,
|
q: other.q,
|
||||||
offset: other.offset,
|
offset: other.offset,
|
||||||
limit: other.limit.unwrap_or(DEFAULT_SEARCH_LIMIT),
|
limit: other.limit.unwrap_or(DEFAULT_SEARCH_LIMIT),
|
||||||
@ -71,7 +67,7 @@ impl TryFrom<SearchQueryGet> for SearchQuery {
|
|||||||
filter,
|
filter,
|
||||||
matches: other.matches,
|
matches: other.matches,
|
||||||
facet_distributions,
|
facet_distributions,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,21 +77,9 @@ async fn search_with_url_query(
|
|||||||
path: web::Path<IndexParam>,
|
path: web::Path<IndexParam>,
|
||||||
params: web::Query<SearchQueryGet>,
|
params: web::Query<SearchQueryGet>,
|
||||||
) -> Result<HttpResponse, ResponseError> {
|
) -> Result<HttpResponse, ResponseError> {
|
||||||
let query: SearchQuery = match params.into_inner().try_into() {
|
let query = params.into_inner().into();
|
||||||
Ok(q) => q,
|
let search_result = data.search(path.into_inner().index_uid, query).await?;
|
||||||
Err(e) => {
|
Ok(HttpResponse::Ok().json(search_result))
|
||||||
return Ok(
|
|
||||||
HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let search_result = data.search(path.into_inner().index_uid, query).await;
|
|
||||||
match search_result {
|
|
||||||
Ok(docs) => Ok(HttpResponse::Ok().json(docs)),
|
|
||||||
Err(e) => {
|
|
||||||
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
||||||
@ -106,11 +90,6 @@ async fn search_with_post(
|
|||||||
) -> Result<HttpResponse, ResponseError> {
|
) -> Result<HttpResponse, ResponseError> {
|
||||||
let search_result = data
|
let search_result = data
|
||||||
.search(path.into_inner().index_uid, params.into_inner())
|
.search(path.into_inner().index_uid, params.into_inner())
|
||||||
.await;
|
.await?;
|
||||||
match search_result {
|
Ok(HttpResponse::Ok().json(search_result))
|
||||||
Ok(docs) => Ok(HttpResponse::Ok().json(docs)),
|
|
||||||
Err(e) => {
|
|
||||||
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user