Merge pull request #601 from meilisearch/tide-to-actix-web

Change tide to actix-web
This commit is contained in:
Clément Renault 2020-04-28 18:43:06 +02:00 committed by GitHub
commit 899559a060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 2730 additions and 2216 deletions

1281
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,13 @@ name = "meilisearch"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
async-std = { version = "1.5.0", features = ["attributes"] } actix-cors = "0.2.0"
actix-files = "0.2.1"
actix-http = "1"
actix-rt = "1"
actix-service = "1.0.5"
actix-web = "2"
actix-web-macros = "0.1.0"
chrono = { version = "0.4.11", features = ["serde"] } chrono = { version = "0.4.11", features = ["serde"] }
crossbeam-channel = "0.4.2" crossbeam-channel = "0.4.2"
env_logger = "0.7.1" env_logger = "0.7.1"
@ -30,25 +36,24 @@ meilisearch-tokenizer = {path = "../meilisearch-tokenizer", version = "0.10.1"}
mime = "0.3.16" mime = "0.3.16"
pretty-bytes = "0.2.2" pretty-bytes = "0.2.2"
rand = "0.7.3" rand = "0.7.3"
rayon = "1.3.0"
serde = { version = "1.0.105", features = ["derive"] } serde = { version = "1.0.105", features = ["derive"] }
serde_json = { version = "1.0.50", features = ["preserve_order"] } serde_json = { version = "1.0.50", features = ["preserve_order"] }
serde_qs = "0.5.2" serde_qs = "0.5.2"
sha2 = "0.8.1" sha2 = "0.8.1"
siphasher = "0.3.2" siphasher = "0.3.2"
slice-group-by = "0.2.6"
structopt = "0.3.12" structopt = "0.3.12"
sysinfo = "0.12.0" sysinfo = "0.12.0"
tide = "0.6.0" tokio = { version = "0.2.18", features = ["macros"] }
ureq = { version = "0.12.0", features = ["tls"], default-features = false } ureq = { version = "0.12.0", features = ["tls"], default-features = false }
walkdir = "2.3.1" walkdir = "2.3.1"
whoami = "0.8.1" whoami = "0.8.1"
slice-group-by = "0.2.6"
[dev-dependencies] [dev-dependencies]
http-service = "0.4.0" http-service = "0.4.0"
http-service-mock = "0.4.0" http-service-mock = "0.4.0"
tempdir = "0.3.7" tempdir = "0.3.7"
once_cell = "1.3.1" tokio = { version = "0.2.18", features = ["macros", "time"] }
[dev-dependencies.assert-json-diff] [dev-dependencies.assert-json-diff]
git = "https://github.com/qdequele/assert-json-diff" git = "https://github.com/qdequele/assert-json-diff"

View File

@ -9,8 +9,8 @@ use meilisearch_core::{Database, Error as MError, MResult, MainT, UpdateT};
use sha2::Digest; use sha2::Digest;
use sysinfo::Pid; use sysinfo::Pid;
use crate::index_update_callback;
use crate::option::Opt; use crate::option::Opt;
use crate::routes::index::index_update_callback;
const LAST_UPDATE_KEY: &str = "last-update"; const LAST_UPDATE_KEY: &str = "last-update";
@ -37,7 +37,7 @@ pub struct DataInner {
pub server_pid: Pid, pub server_pid: Pid,
} }
#[derive(Default, Clone)] #[derive(Clone)]
pub struct ApiKeys { pub struct ApiKeys {
pub public: Option<String>, pub public: Option<String>,
pub private: Option<String>, pub private: Option<String>,
@ -135,7 +135,7 @@ impl Data {
let db = Arc::new(Database::open_or_create(opt.db_path).unwrap()); let db = Arc::new(Database::open_or_create(opt.db_path).unwrap());
let mut api_keys = ApiKeys { let mut api_keys = ApiKeys {
master: opt.master_key.clone(), master: opt.master_key,
private: None, private: None,
public: None, public: None,
}; };

View File

@ -1,191 +1,182 @@
use std::fmt::Display; use std::fmt;
use http::status::StatusCode; use actix_http::ResponseBuilder;
use log::{error, warn}; use actix_web as aweb;
use meilisearch_core::{FstError, HeedError}; use actix_web::http::StatusCode;
use serde::{Deserialize, Serialize}; use serde_json::json;
use tide::IntoResponse;
use tide::Response;
use crate::helpers::meilisearch::Error as SearchError;
pub type SResult<T> = Result<T, ResponseError>;
#[derive(Debug)]
pub enum ResponseError { pub enum ResponseError {
Internal(String),
BadRequest(String),
InvalidToken(String),
NotFound(String),
IndexNotFound(String),
DocumentNotFound(String),
MissingHeader(String),
FilterParsing(String),
BadParameter(String, String), BadParameter(String, String),
OpenIndex(String), BadRequest(String),
CreateIndex(String), CreateIndex(String),
DocumentNotFound(String),
IndexNotFound(String),
Internal(String),
InvalidIndexUid, InvalidIndexUid,
InvalidToken(String),
Maintenance, Maintenance,
MissingAuthorizationHeader,
MissingHeader(String),
NotFound(String),
OpenIndex(String),
FilterParsing(String),
RetrieveDocument(u64, String),
SearchDocuments(String),
} }
impl ResponseError { impl ResponseError {
pub fn internal(message: impl Display) -> ResponseError { pub fn internal(err: impl fmt::Display) -> ResponseError {
ResponseError::Internal(message.to_string()) ResponseError::Internal(err.to_string())
} }
pub fn bad_request(message: impl Display) -> ResponseError { pub fn bad_request(err: impl fmt::Display) -> ResponseError {
ResponseError::BadRequest(message.to_string()) ResponseError::BadRequest(err.to_string())
} }
pub fn invalid_token(message: impl Display) -> ResponseError { pub fn missing_authorization_header() -> ResponseError {
ResponseError::InvalidToken(message.to_string()) ResponseError::MissingAuthorizationHeader
} }
pub fn not_found(message: impl Display) -> ResponseError { pub fn invalid_token(err: impl fmt::Display) -> ResponseError {
ResponseError::NotFound(message.to_string()) ResponseError::InvalidToken(err.to_string())
} }
pub fn index_not_found(message: impl Display) -> ResponseError { pub fn not_found(err: impl fmt::Display) -> ResponseError {
ResponseError::IndexNotFound(message.to_string()) ResponseError::NotFound(err.to_string())
} }
pub fn document_not_found(message: impl Display) -> ResponseError { pub fn index_not_found(err: impl fmt::Display) -> ResponseError {
ResponseError::DocumentNotFound(message.to_string()) ResponseError::IndexNotFound(err.to_string())
} }
pub fn missing_header(message: impl Display) -> ResponseError { pub fn document_not_found(err: impl fmt::Display) -> ResponseError {
ResponseError::MissingHeader(message.to_string()) ResponseError::DocumentNotFound(err.to_string())
} }
pub fn bad_parameter(name: impl Display, message: impl Display) -> ResponseError { pub fn missing_header(err: impl fmt::Display) -> ResponseError {
ResponseError::BadParameter(name.to_string(), message.to_string()) ResponseError::MissingHeader(err.to_string())
} }
pub fn open_index(message: impl Display) -> ResponseError { pub fn bad_parameter(param: impl fmt::Display, err: impl fmt::Display) -> ResponseError {
ResponseError::OpenIndex(message.to_string()) ResponseError::BadParameter(param.to_string(), err.to_string())
} }
pub fn create_index(message: impl Display) -> ResponseError { pub fn open_index(err: impl fmt::Display) -> ResponseError {
ResponseError::CreateIndex(message.to_string()) ResponseError::OpenIndex(err.to_string())
}
pub fn create_index(err: impl fmt::Display) -> ResponseError {
ResponseError::CreateIndex(err.to_string())
}
pub fn invalid_index_uid() -> ResponseError {
ResponseError::InvalidIndexUid
}
pub fn maintenance() -> ResponseError {
ResponseError::Maintenance
}
pub fn retrieve_document(doc_id: u64, err: impl fmt::Display) -> ResponseError {
ResponseError::RetrieveDocument(doc_id, err.to_string())
}
pub fn search_documents(err: impl fmt::Display) -> ResponseError {
ResponseError::SearchDocuments(err.to_string())
} }
} }
impl IntoResponse for ResponseError { impl fmt::Display for ResponseError {
fn into_response(self) -> Response { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
ResponseError::Internal(err) => { Self::BadParameter(param, err) => write!(f, "Url parameter {} error: {}", param, err),
error!("internal server error: {}", err); Self::BadRequest(err) => f.write_str(err),
error("Internal server error".to_string(), Self::CreateIndex(err) => write!(f, "Impossible to create index; {}", err),
StatusCode::INTERNAL_SERVER_ERROR, Self::DocumentNotFound(document_id) => write!(f, "Document with id {} not found", document_id),
) Self::IndexNotFound(index_uid) => write!(f, "Index {} not found", index_uid),
} Self::Internal(err) => f.write_str(err),
ResponseError::FilterParsing(err) => { Self::InvalidIndexUid => f.write_str("Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_)."),
warn!("error paring filter: {}", err); Self::InvalidToken(err) => write!(f, "Invalid API key: {}", err),
error(format!("parsing error: {}", err), Self::Maintenance => f.write_str("Server is in maintenance, please try again later"),
StatusCode::BAD_REQUEST) Self::FilterParsing(err) => write!(f, "parsing error: {}", err),
} Self::MissingAuthorizationHeader => f.write_str("You must have an authorization token"),
ResponseError::BadRequest(err) => { Self::MissingHeader(header) => write!(f, "Header {} is missing", header),
warn!("bad request: {}", err); Self::NotFound(err) => write!(f, "{} not found", err),
error(err, StatusCode::BAD_REQUEST) Self::OpenIndex(err) => write!(f, "Impossible to open index; {}", err),
} Self::RetrieveDocument(id, err) => write!(f, "impossible to retrieve the document with id: {}; {}", id, err),
ResponseError::InvalidToken(err) => { Self::SearchDocuments(err) => write!(f, "impossible to search documents; {}", err),
error(format!("Invalid API key: {}", err), StatusCode::FORBIDDEN)
}
ResponseError::NotFound(err) => error(err, StatusCode::NOT_FOUND),
ResponseError::IndexNotFound(index) => {
error(format!("Index {} not found", index), StatusCode::NOT_FOUND)
}
ResponseError::DocumentNotFound(id) => error(
format!("Document with id {} not found", id),
StatusCode::NOT_FOUND,
),
ResponseError::MissingHeader(header) => error(
format!("Header {} is missing", header),
StatusCode::UNAUTHORIZED,
),
ResponseError::BadParameter(param, e) => error(
format!("Url parameter {} error: {}", param, e),
StatusCode::BAD_REQUEST,
),
ResponseError::CreateIndex(err) => error(
format!("Impossible to create index; {}", err),
StatusCode::BAD_REQUEST,
),
ResponseError::OpenIndex(err) => error(
format!("Impossible to open index; {}", err),
StatusCode::BAD_REQUEST,
),
ResponseError::InvalidIndexUid => error(
"Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).".to_string(),
StatusCode::BAD_REQUEST,
),
ResponseError::Maintenance => error(
String::from("Server is in maintenance, please try again later"),
StatusCode::SERVICE_UNAVAILABLE,
),
} }
} }
} }
#[derive(Serialize, Deserialize)] impl aweb::error::ResponseError for ResponseError {
struct ErrorMessage { fn error_response(&self) -> aweb::HttpResponse {
message: String, ResponseBuilder::new(self.status_code()).json(json!({
"message": self.to_string(),
}))
}
fn status_code(&self) -> StatusCode {
match *self {
Self::BadParameter(_, _)
| Self::BadRequest(_)
| Self::CreateIndex(_)
| Self::InvalidIndexUid
| Self::OpenIndex(_)
| Self::RetrieveDocument(_, _)
| Self::SearchDocuments(_)
| Self::FilterParsing(_) => StatusCode::BAD_REQUEST,
Self::DocumentNotFound(_)
| Self::IndexNotFound(_)
| Self::NotFound(_) => StatusCode::NOT_FOUND,
Self::InvalidToken(_)
| Self::MissingHeader(_) => StatusCode::UNAUTHORIZED,
Self::MissingAuthorizationHeader => StatusCode::FORBIDDEN,
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::Maintenance => StatusCode::SERVICE_UNAVAILABLE,
}
}
} }
fn error(message: String, status: StatusCode) -> Response { impl From<meilisearch_core::HeedError> for ResponseError {
let message = ErrorMessage { message }; fn from(err: meilisearch_core::HeedError) -> ResponseError {
tide::Response::new(status.as_u16()) ResponseError::Internal(err.to_string())
.body_json(&message) }
.unwrap()
} }
impl From<serde_json::Error> for ResponseError { impl From<meilisearch_core::FstError> for ResponseError {
fn from(err: serde_json::Error) -> ResponseError { fn from(err: meilisearch_core::FstError) -> ResponseError {
ResponseError::internal(err) ResponseError::Internal(err.to_string())
} }
} }
impl From<meilisearch_core::Error> for ResponseError { impl From<meilisearch_core::Error> for ResponseError {
fn from(err: meilisearch_core::Error) -> ResponseError { fn from(err: meilisearch_core::Error) -> ResponseError {
ResponseError::internal(err) use meilisearch_core::pest_error::LineColLocation::*;
}
}
impl From<HeedError> for ResponseError {
fn from(err: HeedError) -> ResponseError {
ResponseError::internal(err)
}
}
impl From<FstError> for ResponseError {
fn from(err: FstError) -> ResponseError {
ResponseError::internal(err)
}
}
impl From<SearchError> for ResponseError {
fn from(err: SearchError) -> ResponseError {
match err { match err {
SearchError::FilterParsing(s) => ResponseError::FilterParsing(s), meilisearch_core::Error::FilterParseError(e) => {
_ => ResponseError::internal(err), let (line, column) = match e.line_col {
Span((line, _), (column, _)) => (line, column),
Pos((line, column)) => (line, column),
};
let message = format!("parsing error on line {} at column {}: {}", line, column, e.variant.message());
ResponseError::FilterParsing(message)
},
_ => ResponseError::Internal(err.to_string()),
} }
} }
} }
impl From<meilisearch_core::settings::RankingRuleConversionError> for ResponseError { impl From<meilisearch_schema::Error> for ResponseError {
fn from(err: meilisearch_core::settings::RankingRuleConversionError) -> ResponseError { fn from(err: meilisearch_schema::Error) -> ResponseError {
ResponseError::internal(err) ResponseError::Internal(err.to_string())
} }
} }
pub trait IntoInternalError<T> { impl From<actix_http::Error> for ResponseError {
fn into_internal_error(self) -> SResult<T>; fn from(err: actix_http::Error) -> ResponseError {
} ResponseError::Internal(err.to_string())
impl<T> IntoInternalError<T> for Option<T> {
fn into_internal_error(self) -> SResult<T> {
match self {
Some(value) => Ok(value),
None => Err(ResponseError::internal("Heed cannot find requested value")),
}
} }
} }

View File

@ -0,0 +1,103 @@
use std::cell::RefCell;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};
use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use futures::future::{err, ok, Future, Ready};
use crate::error::ResponseError;
use crate::Data;
#[derive(Clone)]
pub enum Authentication {
Public,
Private,
Admin,
}
impl<S: 'static, B> Transform<S> for Authentication
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = LoggingMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ok(LoggingMiddleware {
acl: self.clone(),
service: Rc::new(RefCell::new(service)),
})
}
}
pub struct LoggingMiddleware<S> {
acl: Authentication,
service: Rc<RefCell<S>>,
}
impl<S, B> Service for LoggingMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, req: ServiceRequest) -> Self::Future {
let mut svc = self.service.clone();
// This unwrap is left because this error should never appear. If that's the case, then
// it means that actix-web has an issue or someone changes the type `Data`.
let data = req.app_data::<Data>().unwrap();
if data.api_keys.master.is_none() {
return Box::pin(svc.call(req));
}
let auth_header = match req.headers().get("X-Meili-API-Key") {
Some(auth) => match auth.to_str() {
Ok(auth) => auth,
Err(_) => return Box::pin(err(ResponseError::MissingAuthorizationHeader.into())),
},
None => {
return Box::pin(err(ResponseError::MissingAuthorizationHeader.into()));
}
};
let authenticated = match self.acl {
Authentication::Admin => data.api_keys.master.as_deref() == Some(auth_header),
Authentication::Private => {
data.api_keys.master.as_deref() == Some(auth_header)
|| data.api_keys.private.as_deref() == Some(auth_header)
}
Authentication::Public => {
data.api_keys.master.as_deref() == Some(auth_header)
|| data.api_keys.private.as_deref() == Some(auth_header)
|| data.api_keys.public.as_deref() == Some(auth_header)
}
};
if authenticated {
Box::pin(svc.call(req))
} else {
Box::pin(err(
ResponseError::InvalidToken(auth_header.to_string()).into()
))
}
}
}

View File

@ -1,10 +1,7 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::convert::From;
use std::error;
use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::time::{Duration, Instant}; use std::time::Instant;
use indexmap::IndexMap; use indexmap::IndexMap;
use log::error; use log::error;
@ -19,74 +16,7 @@ use serde_json::Value;
use siphasher::sip::SipHasher; use siphasher::sip::SipHasher;
use slice_group_by::GroupBy; use slice_group_by::GroupBy;
#[derive(Debug)] use crate::error::ResponseError;
pub enum Error {
SearchDocuments(String),
RetrieveDocument(u64, String),
DocumentNotFound(u64),
CropFieldWrongType(String),
FilterParsing(String),
AttributeNotFoundOnDocument(String),
AttributeNotFoundOnSchema(String),
MissingFilterValue,
UnknownFilteredAttribute,
Internal(String),
}
impl error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
match self {
SearchDocuments(err) => write!(f, "impossible to search documents; {}", err),
RetrieveDocument(id, err) => write!(
f,
"impossible to retrieve the document with id: {}; {}",
id, err
),
DocumentNotFound(id) => write!(f, "document {} not found", id),
CropFieldWrongType(field) => {
write!(f, "the field {} cannot be cropped it's not a string", field)
}
AttributeNotFoundOnDocument(field) => {
write!(f, "field {} is not found on document", field)
}
AttributeNotFoundOnSchema(field) => write!(f, "field {} is not found on schema", field),
MissingFilterValue => f.write_str("a filter doesn't have a value to compare it with"),
UnknownFilteredAttribute => {
f.write_str("a filter is specifying an unknown schema attribute")
}
Internal(err) => write!(f, "internal error; {}", err),
FilterParsing(err) => write!(f, "filter parsing error: {}", err),
}
}
}
impl From<meilisearch_core::Error> for Error {
fn from(error: meilisearch_core::Error) -> Self {
use meilisearch_core::pest_error::LineColLocation::*;
match error {
meilisearch_core::Error::FilterParseError(e) => {
let (line, column) = match e.line_col {
Span((line, _), (column, _)) => (line, column),
Pos((line, column)) => (line, column),
};
let message = format!("parsing error on line {} at column {}: {}", line, column, e.variant.message());
Error::FilterParsing(message)
},
_ => Error::Internal(error.to_string()),
}
}
}
impl From<heed::Error> for Error {
fn from(error: heed::Error) -> Self {
Error::Internal(error.to_string())
}
}
pub trait IndexSearchExt { pub trait IndexSearchExt {
fn new_search(&self, query: String) -> SearchBuilder; fn new_search(&self, query: String) -> SearchBuilder;
@ -103,7 +33,6 @@ impl IndexSearchExt for Index {
attributes_to_retrieve: None, attributes_to_retrieve: None,
attributes_to_highlight: None, attributes_to_highlight: None,
filters: None, filters: None,
timeout: Duration::from_millis(30),
matches: false, matches: false,
} }
} }
@ -118,7 +47,6 @@ pub struct SearchBuilder<'a> {
attributes_to_retrieve: Option<HashSet<String>>, attributes_to_retrieve: Option<HashSet<String>>,
attributes_to_highlight: Option<HashSet<String>>, attributes_to_highlight: Option<HashSet<String>>,
filters: Option<String>, filters: Option<String>,
timeout: Duration,
matches: bool, matches: bool,
} }
@ -159,27 +87,19 @@ impl<'a> SearchBuilder<'a> {
self self
} }
pub fn timeout(&mut self, value: Duration) -> &SearchBuilder {
self.timeout = value;
self
}
pub fn get_matches(&mut self) -> &SearchBuilder { pub fn get_matches(&mut self) -> &SearchBuilder {
self.matches = true; self.matches = true;
self self
} }
pub fn search(&self, reader: &heed::RoTxn<MainT>) -> Result<SearchResult, Error> { pub fn search(&self, reader: &heed::RoTxn<MainT>) -> Result<SearchResult, ResponseError> {
let schema = self.index.main.schema(reader); let schema = self
let schema = schema.map_err(|e| Error::Internal(e.to_string()))?; .index
let schema = match schema { .main
Some(schema) => schema, .schema(reader)?
None => return Err(Error::Internal(String::from("missing schema"))), .ok_or(ResponseError::internal("missing schema"))?;
};
let ranked_map = self.index.main.ranked_map(reader); let ranked_map = self.index.main.ranked_map(reader)?.unwrap_or_default();
let ranked_map = ranked_map.map_err(|e| Error::Internal(e.to_string()))?;
let ranked_map = ranked_map.unwrap_or_default();
// Change criteria // Change criteria
let mut query_builder = match self.get_criteria(reader, &ranked_map, &schema)? { let mut query_builder = match self.get_criteria(reader, &ranked_map, &schema)? {
@ -203,8 +123,6 @@ impl<'a> SearchBuilder<'a> {
}); });
} }
query_builder.with_fetch_timeout(self.timeout);
if let Some(field) = self.index.main.distinct_attribute(reader)? { if let Some(field) = self.index.main.distinct_attribute(reader)? {
if let Some(field_id) = schema.id(&field) { if let Some(field_id) = schema.id(&field) {
query_builder.with_distinct(1, move |id| { query_builder.with_distinct(1, move |id| {
@ -221,9 +139,8 @@ impl<'a> SearchBuilder<'a> {
} }
let start = Instant::now(); let start = Instant::now();
let result = let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit));
query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit)); let (docs, nb_hits) = result.map_err(ResponseError::search_documents)?;
let (docs, nb_hits) = result.map_err(|e| Error::SearchDocuments(e.to_string()))?;
let time_ms = start.elapsed().as_millis() as usize; let time_ms = start.elapsed().as_millis() as usize;
let mut all_attributes: HashSet<&str> = HashSet::new(); let mut all_attributes: HashSet<&str> = HashSet::new();
@ -258,8 +175,10 @@ impl<'a> SearchBuilder<'a> {
let mut document: IndexMap<String, Value> = self let mut document: IndexMap<String, Value> = self
.index .index
.document(reader, Some(&all_attributes), doc.id) .document(reader, Some(&all_attributes), doc.id)
.map_err(|e| Error::RetrieveDocument(doc.id.0, e.to_string()))? .map_err(|e| ResponseError::retrieve_document(doc.id.0, e))?
.ok_or(Error::DocumentNotFound(doc.id.0))?; .ok_or(ResponseError::internal(
"Impossible to retrieve the document; Corrupted data",
))?;
let mut formatted = document.iter() let mut formatted = document.iter()
.filter(|(key, _)| all_formatted.contains(key.as_str())) .filter(|(key, _)| all_formatted.contains(key.as_str()))
@ -320,7 +239,7 @@ impl<'a> SearchBuilder<'a> {
reader: &heed::RoTxn<MainT>, reader: &heed::RoTxn<MainT>,
ranked_map: &'a RankedMap, ranked_map: &'a RankedMap,
schema: &Schema, schema: &Schema,
) -> Result<Option<Criteria<'a>>, Error> { ) -> Result<Option<Criteria<'a>>, ResponseError> {
let ranking_rules = self.index.main.ranking_rules(reader)?; let ranking_rules = self.index.main.ranking_rules(reader)?;
if let Some(ranking_rules) = ranking_rules { if let Some(ranking_rules) = ranking_rules {

View File

@ -1,2 +1,4 @@
pub mod authentication;
pub mod meilisearch; pub mod meilisearch;
pub mod tide;
pub use authentication::Authentication;

View File

@ -1,83 +0,0 @@
use crate::error::{ResponseError, SResult};
use crate::Data;
use meilisearch_core::Index;
use tide::Request;
pub enum ACL {
Admin,
Private,
Public,
}
pub trait RequestExt {
fn is_allowed(&self, acl: ACL) -> SResult<()>;
fn url_param(&self, name: &str) -> SResult<String>;
fn index(&self) -> SResult<Index>;
fn document_id(&self) -> SResult<String>;
}
impl RequestExt for Request<Data> {
fn is_allowed(&self, acl: ACL) -> SResult<()> {
let user_api_key = self.header("X-Meili-API-Key");
if self.state().api_keys.master.is_none() {
return Ok(())
}
match acl {
ACL::Admin => {
if user_api_key == self.state().api_keys.master.as_deref() {
return Ok(());
}
}
ACL::Private => {
if user_api_key == self.state().api_keys.master.as_deref() {
return Ok(());
}
if user_api_key == self.state().api_keys.private.as_deref() {
return Ok(());
}
}
ACL::Public => {
if user_api_key == self.state().api_keys.master.as_deref() {
return Ok(());
}
if user_api_key == self.state().api_keys.private.as_deref() {
return Ok(());
}
if user_api_key == self.state().api_keys.public.as_deref() {
return Ok(());
}
}
}
Err(ResponseError::InvalidToken(
user_api_key.unwrap_or("Need a token").to_owned(),
))
}
fn url_param(&self, name: &str) -> SResult<String> {
let param = self
.param::<String>(name)
.map_err(|e| ResponseError::bad_parameter(name, e))?;
Ok(param)
}
fn index(&self) -> SResult<Index> {
let index_uid = self.url_param("index")?;
let index = self
.state()
.db
.open_index(&index_uid)
.ok_or(ResponseError::index_not_found(index_uid))?;
Ok(index)
}
fn document_id(&self) -> SResult<String> {
let name = self
.param::<String>("document_id")
.map_err(|_| ResponseError::bad_parameter("documentId", "primaryKey"))?;
Ok(name)
}
}

View File

@ -8,3 +8,69 @@ pub mod option;
pub mod routes; pub mod routes;
pub use self::data::Data; pub use self::data::Data;
use actix_http::Error;
use actix_service::ServiceFactory;
use actix_web::{dev, web, App};
use log::error;
use meilisearch_core::ProcessedUpdateResult;
pub fn create_app(
data: &Data,
) -> App<
impl ServiceFactory<
Config = (),
Request = dev::ServiceRequest,
Response = dev::ServiceResponse<actix_http::body::Body>,
Error = Error,
InitError = (),
>,
actix_http::body::Body,
> {
App::new()
.app_data(web::Data::new(data.clone()))
.app_data(web::JsonConfig::default().limit(1024 * 1024 * 10)) // Json Limit of 10Mb
.service(routes::load_html)
.service(routes::load_css)
.configure(routes::document::services)
.configure(routes::index::services)
.configure(routes::search::services)
.configure(routes::setting::services)
.configure(routes::stop_words::services)
.configure(routes::synonym::services)
.configure(routes::health::services)
.configure(routes::stats::services)
.configure(routes::key::services)
}
pub fn index_update_callback(index_uid: &str, data: &Data, status: ProcessedUpdateResult) {
if status.error.is_some() {
return;
}
if let Some(index) = data.db.open_index(&index_uid) {
let db = &data.db;
let mut writer = match db.main_write_txn() {
Ok(writer) => writer,
Err(e) => {
error!("Impossible to get write_txn; {}", e);
return;
}
};
if let Err(e) = data.compute_stats(&mut writer, &index_uid) {
error!("Impossible to compute stats; {}", e)
}
if let Err(e) = data.set_last_update(&mut writer) {
error!("Impossible to update last_update; {}", e)
}
if let Err(e) = index.main.put_updated_at(&mut writer) {
error!("Impossible to update updated_at; {}", e)
}
if let Err(e) = writer.commit() {
error!("Impossible to get write_txn; {}", e);
}
}
}

View File

@ -1,16 +1,13 @@
use std::{env, thread}; use std::{env, thread};
use async_std::task; use actix_cors::Cors;
use actix_web::{middleware, HttpServer};
use log::info; use log::info;
use main_error::MainError; use main_error::MainError;
use structopt::StructOpt;
use tide::middleware::{Cors, RequestLogger, Origin};
use http::header::HeaderValue;
use meilisearch_http::data::Data; use meilisearch_http::data::Data;
use meilisearch_http::option::Opt; use meilisearch_http::option::Opt;
use meilisearch_http::routes; use meilisearch_http::{create_app, index_update_callback};
use meilisearch_http::routes::index::index_update_callback; use structopt::StructOpt;
mod analytics; mod analytics;
@ -18,7 +15,8 @@ mod analytics;
#[global_allocator] #[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
pub fn main() -> Result<(), MainError> { #[actix_rt::main]
async fn main() -> Result<(), MainError> {
let opt = Opt::from_args(); let opt = Opt::from_args();
match opt.env.as_ref() { match opt.env.as_ref() {
@ -29,7 +27,6 @@ pub fn main() -> Result<(), MainError> {
.into(), .into(),
); );
} }
env_logger::init();
} }
"development" => { "development" => {
env_logger::from_env(env_logger::Env::default().default_filter_or("info")).init(); env_logger::from_env(env_logger::Env::default().default_filter_or("info")).init();
@ -50,17 +47,21 @@ pub fn main() -> Result<(), MainError> {
print_launch_resume(&opt, &data); print_launch_resume(&opt, &data);
let mut app = tide::with_state(data); HttpServer::new(move || {
create_app(&data)
.wrap(
Cors::new()
.send_wildcard()
.allowed_header("x-meili-api-key")
.finish(),
)
.wrap(middleware::Logger::default())
.wrap(middleware::Compress::default())
})
.bind(opt.http_addr)?
.run()
.await?;
app.middleware(Cors::new()
.allow_methods(HeaderValue::from_static("GET, POST, PUT, DELETE, OPTIONS"))
.allow_headers(HeaderValue::from_static("X-Meili-API-Key"))
.allow_origin(Origin::from("*")));
app.middleware(RequestLogger::new());
routes::load_routes(&mut app);
task::block_on(app.listen(opt.http_addr))?;
Ok(()) Ok(())
} }
@ -76,7 +77,7 @@ pub fn print_launch_resume(opt: &Opt, data: &Data) {
888 888 "Y8888 888 888 888 "Y8888P" "Y8888 "Y888888 888 "Y8888P 888 888 888 888 "Y8888 888 888 888 "Y8888P" "Y8888 "Y888888 888 "Y8888P 888 888
"#; "#;
println!("{}", ascii_name); info!("{}", ascii_name);
info!("Database path: {:?}", opt.db_path); info!("Database path: {:?}", opt.db_path);
info!("Start server on: {:?}", opt.http_addr); info!("Start server on: {:?}", opt.http_addr);

View File

@ -1,62 +1,85 @@
use std::collections::{BTreeSet, HashSet}; use std::collections::{BTreeSet, HashSet};
use actix_web::{web, HttpResponse};
use actix_web_macros::{delete, get, post, put};
use indexmap::IndexMap; use indexmap::IndexMap;
use serde::{Deserialize, Serialize}; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use tide::{Request, Response};
use crate::error::{ResponseError, SResult}; use crate::error::ResponseError;
use crate::helpers::tide::RequestExt; use crate::helpers::Authentication;
use crate::helpers::tide::ACL::*; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::Data; use crate::Data;
pub async fn get_document(ctx: Request<Data>) -> SResult<Response> { type Document = IndexMap<String, Value>;
ctx.is_allowed(Public)?;
let index = ctx.index()?; #[derive(Deserialize)]
struct DocumentParam {
let original_document_id = ctx.document_id()?; index_uid: String,
let document_id = meilisearch_core::serde::compute_document_id(original_document_id.clone()); document_id: String,
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let response = index
.document::<IndexMap<String, Value>>(&reader, None, document_id)?
.ok_or(ResponseError::document_not_found(&original_document_id))?;
if response.is_empty() {
return Err(ResponseError::document_not_found(&original_document_id));
}
Ok(tide::Response::new(200).body_json(&response)?)
} }
#[derive(Default, Serialize)] pub fn services(cfg: &mut web::ServiceConfig) {
#[serde(rename_all = "camelCase")] cfg.service(get_document)
pub struct IndexUpdateResponse { .service(delete_document)
pub update_id: u64, .service(get_all_documents)
.service(add_documents)
.service(update_documents)
.service(delete_documents)
.service(clear_all_documents);
} }
pub async fn delete_document(ctx: Request<Data>) -> SResult<Response> { #[get(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/documents/{document_id}",
wrap = "Authentication::Public"
)]
async fn get_document(
data: web::Data<Data>,
path: web::Path<DocumentParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let document_id = meilisearch_core::serde::compute_document_id(&path.document_id);
let reader = data.db.main_read_txn()?;
let response: Document = index
.document(&reader, None, document_id)?
.ok_or(ResponseError::document_not_found(&path.document_id))?;
Ok(HttpResponse::Ok().json(response))
}
#[delete(
"/indexes/{index_uid}/documents/{document_id}",
wrap = "Authentication::Private"
)]
async fn delete_document(
data: web::Data<Data>,
path: web::Path<DocumentParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let document_id = meilisearch_core::serde::compute_document_id(&path.document_id);
let mut update_writer = data.db.update_write_txn()?;
let index = ctx.index()?;
let document_id = ctx.document_id()?;
let document_id = meilisearch_core::serde::compute_document_id(document_id);
let db = &ctx.state().db;
let mut update_writer = db.update_write_txn()?;
let mut documents_deletion = index.documents_deletion(); let mut documents_deletion = index.documents_deletion();
documents_deletion.delete_document_by_id(document_id); documents_deletion.delete_document_by_id(document_id);
let update_id = documents_deletion.finalize(&mut update_writer)?; let update_id = documents_deletion.finalize(&mut update_writer)?;
update_writer.commit()?; update_writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
#[derive(Default, Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BrowseQuery { struct BrowseQuery {
offset: Option<usize>, offset: Option<usize>,
@ -64,17 +87,21 @@ struct BrowseQuery {
attributes_to_retrieve: Option<String>, attributes_to_retrieve: Option<String>,
} }
pub async fn get_all_documents(ctx: Request<Data>) -> SResult<Response> { #[get("/indexes/{index_uid}/documents", wrap = "Authentication::Public")]
ctx.is_allowed(Private)?; async fn get_all_documents(
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<BrowseQuery>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let index = ctx.index()?; let offset = params.offset.unwrap_or(0);
let query: BrowseQuery = ctx.query().unwrap_or_default(); let limit = params.limit.unwrap_or(20);
let offset = query.offset.unwrap_or(0); let reader = data.db.main_read_txn()?;
let limit = query.limit.unwrap_or(20);
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let documents_ids: Result<BTreeSet<_>, _> = index let documents_ids: Result<BTreeSet<_>, _> = index
.documents_fields_counts .documents_fields_counts
@ -83,29 +110,23 @@ pub async fn get_all_documents(ctx: Request<Data>) -> SResult<Response> {
.take(limit) .take(limit)
.collect(); .collect();
let documents_ids = match documents_ids { let documents_ids = documents_ids?;
Ok(documents_ids) => documents_ids,
Err(e) => return Err(ResponseError::internal(e)),
};
let mut response_body = Vec::<IndexMap<String, Value>>::new(); let attributes: Option<HashSet<&str>> = params
.attributes_to_retrieve
.as_ref()
.map(|a| a.split(',').collect());
if let Some(attributes) = query.attributes_to_retrieve { let mut response = Vec::new();
let attributes = attributes.split(',').collect::<HashSet<&str>>(); for document_id in documents_ids {
for document_id in documents_ids { if let Ok(Some(document)) =
if let Ok(Some(document)) = index.document(&reader, Some(&attributes), document_id) { index.document::<Document>(&reader, attributes.as_ref(), document_id)
response_body.push(document); {
} response.push(document);
}
} else {
for document_id in documents_ids {
if let Ok(Some(document)) = index.document(&reader, None, document_id) {
response_body.push(document);
}
} }
} }
Ok(tide::Response::new(200).body_json(&response_body)?) Ok(HttpResponse::Ok().json(response))
} }
fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> { fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> {
@ -117,40 +138,45 @@ fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> {
None None
} }
#[derive(Default, Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateDocumentsQuery { struct UpdateDocumentsQuery {
primary_key: Option<String>, primary_key: Option<String>,
} }
async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) -> SResult<Response> { async fn update_multiple_documents(
ctx.is_allowed(Private)?; data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>,
is_partial: bool,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let index = ctx.index()?; let reader = data.db.main_read_txn()?;
let data: Vec<IndexMap<String, Value>> =
ctx.body_json().await.map_err(ResponseError::bad_request)?;
let query: UpdateDocumentsQuery = ctx.query().unwrap_or_default();
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let mut schema = index let mut schema = index
.main .main
.schema(&reader)? .schema(&reader)?
.ok_or(ResponseError::internal("schema not found"))?; .ok_or(ResponseError::internal("Impossible to retrieve the schema"))?;
if schema.primary_key().is_none() { if schema.primary_key().is_none() {
let id = match query.primary_key { let id = match &params.primary_key {
Some(id) => id, Some(id) => id.to_string(),
None => match data.first().and_then(|docs| find_primary_key(docs)) { None => body
Some(id) => id, .first()
None => return Err(ResponseError::bad_request("Could not infer a primary key")), .and_then(find_primary_key)
}, .ok_or(ResponseError::bad_request("Could not infer a primary key"))?,
}; };
let mut writer = db.main_write_txn()?; let mut writer = data.db.main_write_txn()?;
schema.set_primary_key(&id).map_err(ResponseError::bad_request)?;
schema
.set_primary_key(&id)
.map_err(ResponseError::bad_request)?;
index.main.put_schema(&mut writer, &schema)?; index.main.put_schema(&mut writer, &schema)?;
writer.commit()?; writer.commit()?;
} }
@ -161,38 +187,56 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
index.documents_addition() index.documents_addition()
}; };
for document in data { for document in body.into_inner() {
document_addition.update_document(document); document_addition.update_document(document);
} }
let mut update_writer = db.update_write_txn()?; let mut update_writer = data.db.update_write_txn()?;
let update_id = document_addition.finalize(&mut update_writer)?; let update_id = document_addition.finalize(&mut update_writer)?;
update_writer.commit()?; update_writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn add_or_replace_multiple_documents(ctx: Request<Data>) -> SResult<Response> { #[post("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
update_multiple_documents(ctx, false).await async fn add_documents(
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>,
) -> Result<HttpResponse, ResponseError> {
update_multiple_documents(data, path, params, body, false).await
} }
pub async fn add_or_update_multiple_documents(ctx: Request<Data>) -> SResult<Response> { #[put("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
update_multiple_documents(ctx, true).await async fn update_documents(
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>,
) -> Result<HttpResponse, ResponseError> {
update_multiple_documents(data, path, params, body, true).await
} }
pub async fn delete_multiple_documents(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/documents/delete-batch",
wrap = "Authentication::Private"
)]
async fn delete_documents(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Vec<Value>>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let data: Vec<Value> = ctx.body_json().await.map_err(ResponseError::bad_request)?; let mut writer = data.db.update_write_txn()?;
let index = ctx.index()?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn()?;
let mut documents_deletion = index.documents_deletion(); let mut documents_deletion = index.documents_deletion();
for document_id in data { for document_id in body.into_inner() {
if let Some(document_id) = meilisearch_core::serde::value_to_string(&document_id) { if let Some(document_id) = meilisearch_core::serde::value_to_string(&document_id) {
documents_deletion documents_deletion
.delete_document_by_id(meilisearch_core::serde::compute_document_id(document_id)); .delete_document_by_id(meilisearch_core::serde::compute_document_id(document_id));
@ -203,21 +247,24 @@ pub async fn delete_multiple_documents(mut ctx: Request<Data>) -> SResult<Respon
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn clear_all_documents(ctx: Request<Data>) -> SResult<Response> { #[delete("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
ctx.is_allowed(Private)?; async fn clear_all_documents(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let index = ctx.index()?; let mut writer = data.db.update_write_txn()?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn()?;
let update_id = index.clear_all(&mut writer)?; let update_id = index.clear_all(&mut writer)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -1,47 +1,47 @@
use crate::error::{ResponseError, SResult}; use actix_web::{web, HttpResponse};
use crate::helpers::tide::RequestExt; use actix_web_macros::{get, put};
use crate::helpers::tide::ACL::*;
use crate::Data;
use heed::types::{Str, Unit}; use heed::types::{Str, Unit};
use serde::Deserialize; use serde::Deserialize;
use tide::{Request, Response};
use crate::error::ResponseError;
use crate::helpers::Authentication;
use crate::Data;
const UNHEALTHY_KEY: &str = "_is_unhealthy"; const UNHEALTHY_KEY: &str = "_is_unhealthy";
pub async fn get_health(ctx: Request<Data>) -> SResult<Response> { pub fn services(cfg: &mut web::ServiceConfig) {
let db = &ctx.state().db; cfg.service(get_health).service(change_healthyness);
let reader = db.main_read_txn()?; }
let common_store = ctx.state().db.common_store(); #[get("/health", wrap = "Authentication::Private")]
async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let reader = data.db.main_read_txn()?;
let common_store = data.db.common_store();
if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) { if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) {
return Err(ResponseError::Maintenance); return Err(ResponseError::Maintenance);
} }
Ok(tide::Response::new(200)) Ok(HttpResponse::Ok().finish())
} }
pub async fn set_healthy(ctx: Request<Data>) -> SResult<Response> { async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
ctx.is_allowed(Admin)?; let mut writer = data.db.main_write_txn()?;
let db = &ctx.state().db; let common_store = data.db.common_store();
let mut writer = db.main_write_txn()?;
let common_store = ctx.state().db.common_store();
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?; common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?;
writer.commit()?; writer.commit()?;
Ok(tide::Response::new(200)) Ok(HttpResponse::Ok().finish())
} }
pub async fn set_unhealthy(ctx: Request<Data>) -> SResult<Response> { async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
ctx.is_allowed(Admin)?; let mut writer = data.db.main_write_txn()?;
let db = &ctx.state().db; let common_store = data.db.common_store();
let mut writer = db.main_write_txn()?;
let common_store = ctx.state().db.common_store();
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?; common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?;
writer.commit()?; writer.commit()?;
Ok(tide::Response::new(200)) Ok(HttpResponse::Ok().finish())
} }
#[derive(Deserialize, Clone)] #[derive(Deserialize, Clone)]
@ -49,12 +49,14 @@ struct HealtBody {
health: bool, health: bool,
} }
pub async fn change_healthyness(mut ctx: Request<Data>) -> SResult<Response> { #[put("/health", wrap = "Authentication::Private")]
let body: HealtBody = ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn change_healthyness(
data: web::Data<Data>,
body: web::Json<HealtBody>,
) -> Result<HttpResponse, ResponseError> {
if body.health { if body.health {
set_healthy(ctx).await set_healthy(data).await
} else { } else {
set_unhealthy(ctx).await set_unhealthy(data).await
} }
} }

View File

@ -1,16 +1,25 @@
use actix_web::{web, HttpResponse};
use actix_web_macros::{delete, get, post, put};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use log::error; use log::error;
use meilisearch_core::ProcessedUpdateResult;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json;
use tide::{Request, Response};
use crate::error::{IntoInternalError, ResponseError, SResult}; use crate::error::ResponseError;
use crate::helpers::tide::RequestExt; use crate::helpers::Authentication;
use crate::helpers::tide::ACL::*; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(list_indexes)
.service(get_index)
.service(create_index)
.service(update_index)
.service(delete_index)
.service(get_update_status)
.service(get_all_updates_status);
}
fn generate_uid() -> String { fn generate_uid() -> String {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let sample = b"abcdefghijklmnopqrstuvwxyz0123456789"; let sample = b"abcdefghijklmnopqrstuvwxyz0123456789";
@ -20,24 +29,42 @@ fn generate_uid() -> String {
.collect() .collect()
} }
pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> { #[derive(Debug, Serialize)]
ctx.is_allowed(Private)?; #[serde(rename_all = "camelCase")]
struct IndexResponse {
name: String,
uid: String,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
primary_key: Option<String>,
}
let indexes_uids = ctx.state().db.indexes_uids(); #[get("/indexes", wrap = "Authentication::Private")]
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let reader = data.db.main_read_txn()?;
let db = &ctx.state().db; let mut response = Vec::new();
let reader = db.main_read_txn()?;
let mut response_body = Vec::new(); for index_uid in data.db.indexes_uids() {
let index = data.db.open_index(&index_uid);
for index_uid in indexes_uids {
let index = ctx.state().db.open_index(&index_uid);
match index { match index {
Some(index) => { Some(index) => {
let name = index.main.name(&reader)?.into_internal_error()?; let name = index.main.name(&reader)?.ok_or(ResponseError::internal(
let created_at = index.main.created_at(&reader)?.into_internal_error()?; "Impossible to get the name of an index",
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?; ))?;
let created_at = index
.main
.created_at(&reader)?
.ok_or(ResponseError::internal(
"Impossible to get the create date of an index",
))?;
let updated_at = index
.main
.updated_at(&reader)?
.ok_or(ResponseError::internal(
"Impossible to get the last update date of an index",
))?;
let primary_key = match index.main.schema(&reader) { let primary_key = match index.main.schema(&reader) {
Ok(Some(schema)) => match schema.primary_key() { Ok(Some(schema)) => match schema.primary_key() {
@ -54,7 +81,7 @@ pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> {
updated_at, updated_at,
primary_key, primary_key,
}; };
response_body.push(index_response); response.push(index_response);
} }
None => error!( None => error!(
"Index {} is referenced in the indexes list but cannot be found", "Index {} is referenced in the indexes list but cannot be found",
@ -63,31 +90,36 @@ pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> {
} }
} }
Ok(tide::Response::new(200).body_json(&response_body)?) Ok(HttpResponse::Ok().json(response))
} }
#[derive(Debug, Serialize)] #[get("/indexes/{index_uid}", wrap = "Authentication::Private")]
#[serde(rename_all = "camelCase")] async fn get_index(
struct IndexResponse { data: web::Data<Data>,
name: String, path: web::Path<IndexParam>,
uid: String, ) -> Result<HttpResponse, ResponseError> {
created_at: DateTime<Utc>, let index = data
updated_at: DateTime<Utc>, .db
primary_key: Option<String>, .open_index(&path.index_uid)
} .ok_or(ResponseError::index_not_found(&path.index_uid))?;
pub async fn get_index(ctx: Request<Data>) -> SResult<Response> { let reader = data.db.main_read_txn()?;
ctx.is_allowed(Private)?;
let index = ctx.index()?; let name = index.main.name(&reader)?.ok_or(ResponseError::internal(
"Impossible to get the name of an index",
let db = &ctx.state().db; ))?;
let reader = db.main_read_txn()?; let created_at = index
.main
let uid = ctx.url_param("index")?; .created_at(&reader)?
let name = index.main.name(&reader)?.into_internal_error()?; .ok_or(ResponseError::internal(
let created_at = index.main.created_at(&reader)?.into_internal_error()?; "Impossible to get the create date of an index",
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?; ))?;
let updated_at = index
.main
.updated_at(&reader)?
.ok_or(ResponseError::internal(
"Impossible to get the last update date of an index",
))?;
let primary_key = match index.main.schema(&reader) { let primary_key = match index.main.schema(&reader) {
Ok(Some(schema)) => match schema.primary_key() { Ok(Some(schema)) => match schema.primary_key() {
@ -97,15 +129,13 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
_ => None, _ => None,
}; };
let response_body = IndexResponse { Ok(HttpResponse::Ok().json(IndexResponse {
name, name,
uid, uid: path.index_uid.clone(),
created_at, created_at,
updated_at, updated_at,
primary_key, primary_key,
}; }))
Ok(tide::Response::new(200).body_json(&response_body)?)
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -116,86 +146,74 @@ struct IndexCreateRequest {
primary_key: Option<String>, primary_key: Option<String>,
} }
#[derive(Debug, Serialize)] #[post("/indexes", wrap = "Authentication::Private")]
#[serde(rename_all = "camelCase")] async fn create_index(
struct IndexCreateResponse { data: web::Data<Data>,
name: String, body: web::Json<IndexCreateRequest>,
uid: String, ) -> Result<HttpResponse, ResponseError> {
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
primary_key: Option<String>,
}
pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Private)?;
let body = ctx
.body_json::<IndexCreateRequest>()
.await
.map_err(ResponseError::bad_request)?;
if let (None, None) = (body.name.clone(), body.uid.clone()) { if let (None, None) = (body.name.clone(), body.uid.clone()) {
return Err(ResponseError::bad_request( return Err(ResponseError::bad_request(
"Index creation must have an uid", "Index creation must have an uid",
)); ));
} }
let db = &ctx.state().db; let uid = match &body.uid {
let uid = match body.uid {
Some(uid) => { Some(uid) => {
if uid if uid
.chars() .chars()
.all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') .all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_')
{ {
uid uid.to_owned()
} else { } else {
return Err(ResponseError::InvalidIndexUid); return Err(ResponseError::InvalidIndexUid);
} }
} }
None => loop { None => loop {
let uid = generate_uid(); let uid = generate_uid();
if db.open_index(&uid).is_none() { if data.db.open_index(&uid).is_none() {
break uid; break uid;
} }
}, },
}; };
let created_index = match db.create_index(&uid) { let created_index = data
Ok(index) => index, .db
Err(e) => return Err(ResponseError::create_index(e)), .create_index(&uid)
}; .map_err(ResponseError::create_index)?;
let mut writer = data.db.main_write_txn()?;
let name = body.name.as_ref().unwrap_or(&uid);
created_index.main.put_name(&mut writer, name)?;
let mut writer = db.main_write_txn()?;
let name = body.name.unwrap_or(uid.clone());
created_index.main.put_name(&mut writer, &name)?;
let created_at = created_index let created_at = created_index
.main .main
.created_at(&writer)? .created_at(&writer)?
.into_internal_error()?; .ok_or(ResponseError::internal("Impossible to read created at"))?;
let updated_at = created_index let updated_at = created_index
.main .main
.updated_at(&writer)? .updated_at(&writer)?
.into_internal_error()?; .ok_or(ResponseError::internal("Impossible to read updated at"))?;
if let Some(id) = body.primary_key.clone() { if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = created_index.main.schema(&mut writer)? { if let Some(mut schema) = created_index.main.schema(&writer)? {
schema.set_primary_key(&id).map_err(ResponseError::bad_request)?; schema
.set_primary_key(&id)
.map_err(ResponseError::bad_request)?;
created_index.main.put_schema(&mut writer, &schema)?; created_index.main.put_schema(&mut writer, &schema)?;
} }
} }
writer.commit()?; writer.commit()?;
let response_body = IndexCreateResponse { Ok(HttpResponse::Created().json(IndexResponse {
name, name: name.to_string(),
uid, uid,
created_at, created_at,
updated_at, updated_at,
primary_key: body.primary_key, primary_key: body.primary_key.clone(),
}; }))
Ok(tide::Response::new(201).body_json(&response_body)?)
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -215,26 +233,25 @@ struct UpdateIndexResponse {
primary_key: Option<String>, primary_key: Option<String>,
} }
pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> { #[put("/indexes/{index_uid}", wrap = "Authentication::Private")]
ctx.is_allowed(Private)?; async fn update_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<IndexCreateRequest>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let body = ctx let mut writer = data.db.main_write_txn()?;
.body_json::<UpdateIndexRequest>()
.await
.map_err(ResponseError::bad_request)?;
let index_uid = ctx.url_param("index")?; if let Some(name) = &body.name {
let index = ctx.index()?; index.main.put_name(&mut writer, name)?;
let db = &ctx.state().db;
let mut writer = db.main_write_txn()?;
if let Some(name) = body.name {
index.main.put_name(&mut writer, &name)?;
} }
if let Some(id) = body.primary_key.clone() { if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = index.main.schema(&mut writer)? { if let Some(mut schema) = index.main.schema(&writer)? {
match schema.primary_key() { match schema.primary_key() {
Some(_) => { Some(_) => {
return Err(ResponseError::bad_request( return Err(ResponseError::bad_request(
@ -242,9 +259,7 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
)); ));
} }
None => { None => {
schema schema.set_primary_key(&id)?;
.set_primary_key(&id)
.map_err(ResponseError::bad_request)?;
index.main.put_schema(&mut writer, &schema)?; index.main.put_schema(&mut writer, &schema)?;
} }
} }
@ -254,10 +269,23 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
index.main.put_updated_at(&mut writer)?; index.main.put_updated_at(&mut writer)?;
writer.commit()?; writer.commit()?;
let reader = db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let name = index.main.name(&reader)?.into_internal_error()?;
let created_at = index.main.created_at(&reader)?.into_internal_error()?; let name = index.main.name(&reader)?.ok_or(ResponseError::internal(
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?; "Impossible to get the name of an index",
))?;
let created_at = index
.main
.created_at(&reader)?
.ok_or(ResponseError::internal(
"Impossible to get the create date of an index",
))?;
let updated_at = index
.main
.updated_at(&reader)?
.ok_or(ResponseError::internal(
"Impossible to get the last update date of an index",
))?;
let primary_key = match index.main.schema(&reader) { let primary_key = match index.main.schema(&reader) {
Ok(Some(schema)) => match schema.primary_key() { Ok(Some(schema)) => match schema.primary_key() {
@ -267,86 +295,70 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
_ => None, _ => None,
}; };
let response_body = UpdateIndexResponse { Ok(HttpResponse::Ok().json(IndexResponse {
name, name,
uid: index_uid, uid: path.index_uid.clone(),
created_at, created_at,
updated_at, updated_at,
primary_key, primary_key,
}; }))
Ok(tide::Response::new(200).body_json(&response_body)?)
} }
pub async fn get_update_status(ctx: Request<Data>) -> SResult<Response> { #[delete("/indexes/{index_uid}", wrap = "Authentication::Private")]
ctx.is_allowed(Private)?; async fn delete_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
data.db.delete_index(&path.index_uid)?;
let db = &ctx.state().db; Ok(HttpResponse::NoContent().finish())
let reader = db.update_read_txn()?;
let update_id = ctx
.param::<u64>("update_id")
.map_err(|e| ResponseError::bad_parameter("update_id", e))?;
let index = ctx.index()?;
let status = index.update_status(&reader, update_id)?;
let response = match status {
Some(status) => tide::Response::new(200).body_json(&status).unwrap(),
None => tide::Response::new(404)
.body_json(&json!({ "message": "unknown update id" }))
.unwrap(),
};
Ok(response)
} }
pub async fn get_all_updates_status(ctx: Request<Data>) -> SResult<Response> { #[derive(Deserialize)]
ctx.is_allowed(Private)?; struct UpdateParam {
let db = &ctx.state().db; index_uid: String,
let reader = db.update_read_txn()?; update_id: u64,
let index = ctx.index()?; }
#[get(
"/indexes/{index_uid}/updates/{update_id}",
wrap = "Authentication::Private"
)]
async fn get_update_status(
data: web::Data<Data>,
path: web::Path<UpdateParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.update_read_txn()?;
let status = index.update_status(&reader, path.update_id)?;
match status {
Some(status) => Ok(HttpResponse::Ok().json(status)),
None => Err(ResponseError::NotFound(format!(
"Update {} not found",
path.update_id
))),
}
}
#[get("/indexes/{index_uid}/updates", wrap = "Authentication::Private")]
async fn get_all_updates_status(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.update_read_txn()?;
let response = index.all_updates_status(&reader)?; let response = index.all_updates_status(&reader)?;
Ok(tide::Response::new(200).body_json(&response).unwrap())
} Ok(HttpResponse::Ok().json(response))
pub async fn delete_index(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Private)?;
let _ = ctx.index()?;
let index_uid = ctx.url_param("index")?;
ctx.state().db.delete_index(&index_uid)?;
Ok(tide::Response::new(204))
}
pub fn index_update_callback(index_uid: &str, data: &Data, status: ProcessedUpdateResult) {
if status.error.is_some() {
return;
}
if let Some(index) = data.db.open_index(&index_uid) {
let db = &data.db;
let mut writer = match db.main_write_txn() {
Ok(writer) => writer,
Err(e) => {
error!("Impossible to get write_txn; {}", e);
return;
}
};
if let Err(e) = data.compute_stats(&mut writer, &index_uid) {
error!("Impossible to compute stats; {}", e)
}
if let Err(e) = data.set_last_update(&mut writer) {
error!("Impossible to update last_update; {}", e)
}
if let Err(e) = index.main.put_updated_at(&mut writer) {
error!("Impossible to update updated_at; {}", e)
}
if let Err(e) = writer.commit() {
error!("Impossible to get write_txn; {}", e);
}
}
} }

View File

@ -1,17 +1,26 @@
use crate::error::SResult; use actix_web::web;
use crate::helpers::tide::RequestExt; use actix_web::HttpResponse;
use crate::helpers::tide::ACL::*; use actix_web_macros::get;
use serde::Serialize;
use crate::helpers::Authentication;
use crate::Data; use crate::Data;
use serde_json::json;
use tide::{Request, Response};
pub async fn list(ctx: Request<Data>) -> SResult<Response> { pub fn services(cfg: &mut web::ServiceConfig) {
ctx.is_allowed(Admin)?; cfg.service(list);
}
let keys = &ctx.state().api_keys;
#[derive(Serialize)]
Ok(tide::Response::new(200).body_json(&json!({ struct KeysResponse {
"private": keys.private, private: Option<String>,
"public": keys.public, public: Option<String>,
}))?) }
#[get("/keys", wrap = "Authentication::Admin")]
async fn list(data: web::Data<Data>) -> HttpResponse {
let api_keys = data.api_keys.clone();
HttpResponse::Ok().json(KeysResponse {
private: api_keys.private,
public: api_keys.public,
})
} }

View File

@ -1,7 +1,5 @@
use crate::data::Data; use actix_web::{get, HttpResponse};
use std::future::Future; use serde::{Deserialize, Serialize};
use tide::IntoResponse;
use tide::Response;
pub mod document; pub mod document;
pub mod health; pub mod health;
@ -13,118 +11,33 @@ pub mod stats;
pub mod stop_words; pub mod stop_words;
pub mod synonym; pub mod synonym;
async fn into_response<T: IntoResponse, U: IntoResponse>( #[derive(Deserialize)]
x: impl Future<Output = Result<T, U>>, pub struct IndexParam {
) -> Response { index_uid: String,
match x.await { }
Ok(resp) => resp.into_response(),
Err(resp) => resp.into_response(), #[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct IndexUpdateResponse {
pub update_id: u64,
}
impl IndexUpdateResponse {
pub fn with_id(update_id: u64) -> Self {
Self { update_id }
} }
} }
pub fn load_routes(app: &mut tide::Server<Data>) { #[get("/")]
app.at("/").get(|_| async { pub async fn load_html() -> HttpResponse {
tide::Response::new(200) HttpResponse::Ok()
.body_string(include_str!("../../public/interface.html").to_string()) .content_type("text/html; charset=utf-8")
.set_mime(mime::TEXT_HTML_UTF_8) .body(include_str!("../../public/interface.html").to_string())
}); }
app.at("/bulma.min.css").get(|_| async {
tide::Response::new(200) #[get("/bulma.min.css")]
.body_string(include_str!("../../public/bulma.min.css").to_string()) pub async fn load_css() -> HttpResponse {
.set_mime(mime::TEXT_CSS_UTF_8) HttpResponse::Ok()
}); .content_type("text/css; charset=utf-8")
.body(include_str!("../../public/bulma.min.css").to_string())
app.at("/indexes")
.get(|ctx| into_response(index::list_indexes(ctx)))
.post(|ctx| into_response(index::create_index(ctx)));
app.at("/indexes/search")
.post(|ctx| into_response(search::search_multi_index(ctx)));
app.at("/indexes/:index")
.get(|ctx| into_response(index::get_index(ctx)))
.put(|ctx| into_response(index::update_index(ctx)))
.delete(|ctx| into_response(index::delete_index(ctx)));
app.at("/indexes/:index/search")
.get(|ctx| into_response(search::search_with_url_query(ctx)));
app.at("/indexes/:index/updates")
.get(|ctx| into_response(index::get_all_updates_status(ctx)));
app.at("/indexes/:index/updates/:update_id")
.get(|ctx| into_response(index::get_update_status(ctx)));
app.at("/indexes/:index/documents")
.get(|ctx| into_response(document::get_all_documents(ctx)))
.post(|ctx| into_response(document::add_or_replace_multiple_documents(ctx)))
.put(|ctx| into_response(document::add_or_update_multiple_documents(ctx)))
.delete(|ctx| into_response(document::clear_all_documents(ctx)));
app.at("/indexes/:index/documents/:document_id")
.get(|ctx| into_response(document::get_document(ctx)))
.delete(|ctx| into_response(document::delete_document(ctx)));
app.at("/indexes/:index/documents/delete-batch")
.post(|ctx| into_response(document::delete_multiple_documents(ctx)));
app.at("/indexes/:index/settings")
.get(|ctx| into_response(setting::get_all(ctx)))
.post(|ctx| into_response(setting::update_all(ctx)))
.delete(|ctx| into_response(setting::delete_all(ctx)));
app.at("/indexes/:index/settings/ranking-rules")
.get(|ctx| into_response(setting::get_rules(ctx)))
.post(|ctx| into_response(setting::update_rules(ctx)))
.delete(|ctx| into_response(setting::delete_rules(ctx)));
app.at("/indexes/:index/settings/distinct-attribute")
.get(|ctx| into_response(setting::get_distinct(ctx)))
.post(|ctx| into_response(setting::update_distinct(ctx)))
.delete(|ctx| into_response(setting::delete_distinct(ctx)));
app.at("/indexes/:index/settings/searchable-attributes")
.get(|ctx| into_response(setting::get_searchable(ctx)))
.post(|ctx| into_response(setting::update_searchable(ctx)))
.delete(|ctx| into_response(setting::delete_searchable(ctx)));
app.at("/indexes/:index/settings/displayed-attributes")
.get(|ctx| into_response(setting::displayed(ctx)))
.post(|ctx| into_response(setting::update_displayed(ctx)))
.delete(|ctx| into_response(setting::delete_displayed(ctx)));
app.at("/indexes/:index/settings/accept-new-fields")
.get(|ctx| into_response(setting::get_accept_new_fields(ctx)))
.post(|ctx| into_response(setting::update_accept_new_fields(ctx)));
app.at("/indexes/:index/settings/synonyms")
.get(|ctx| into_response(synonym::get(ctx)))
.post(|ctx| into_response(synonym::update(ctx)))
.delete(|ctx| into_response(synonym::delete(ctx)));
app.at("/indexes/:index/settings/stop-words")
.get(|ctx| into_response(stop_words::get(ctx)))
.post(|ctx| into_response(stop_words::update(ctx)))
.delete(|ctx| into_response(stop_words::delete(ctx)));
app.at("/indexes/:index/stats")
.get(|ctx| into_response(stats::index_stats(ctx)));
app.at("/keys").get(|ctx| into_response(key::list(ctx)));
app.at("/health")
.get(|ctx| into_response(health::get_health(ctx)))
.put(|ctx| into_response(health::change_healthyness(ctx)));
app.at("/stats")
.get(|ctx| into_response(stats::get_stats(ctx)));
app.at("/version")
.get(|ctx| into_response(stats::get_version(ctx)));
app.at("/sys-info")
.get(|ctx| into_response(stats::get_sys_info(ctx)));
app.at("/sys-info/pretty")
.get(|ctx| into_response(stats::get_sys_info_pretty(ctx)));
} }

View File

@ -1,19 +1,21 @@
use std::collections::HashMap; use std::collections::{HashSet, HashMap};
use std::collections::HashSet;
use std::time::Duration;
use log::warn; use log::warn;
use meilisearch_core::Index; use actix_web::web;
use rayon::iter::{IntoParallelIterator, ParallelIterator}; use actix_web::HttpResponse;
use serde::{Deserialize, Serialize}; use actix_web_macros::get;
use tide::{Request, Response}; use serde::Deserialize;
use crate::error::{ResponseError, SResult}; use crate::error::ResponseError;
use crate::helpers::meilisearch::{Error, IndexSearchExt, SearchHit}; use crate::helpers::meilisearch::IndexSearchExt;
use crate::helpers::tide::RequestExt; use crate::helpers::Authentication;
use crate::helpers::tide::ACL::*; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(search_with_url_query);
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct SearchQuery { struct SearchQuery {
@ -25,38 +27,39 @@ struct SearchQuery {
crop_length: Option<usize>, crop_length: Option<usize>,
attributes_to_highlight: Option<String>, attributes_to_highlight: Option<String>,
filters: Option<String>, filters: Option<String>,
timeout_ms: Option<u64>,
matches: Option<bool>, matches: Option<bool>,
} }
pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> { #[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
ctx.is_allowed(Public)?; async fn search_with_url_query(
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<SearchQuery>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let index = ctx.index()?; let reader = data.db.main_read_txn()?;
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let schema = index let schema = index
.main .main
.schema(&reader)? .schema(&reader)?
.ok_or(ResponseError::open_index("No Schema found"))?; .ok_or(ResponseError::internal("Impossible to retrieve the schema"))?;
let query: SearchQuery = ctx let mut search_builder = index.new_search(params.q.clone());
.query()
.map_err(|_| ResponseError::bad_request("invalid query parameter"))?;
let mut search_builder = index.new_search(query.q.clone()); if let Some(offset) = params.offset {
if let Some(offset) = query.offset {
search_builder.offset(offset); search_builder.offset(offset);
} }
if let Some(limit) = query.limit { if let Some(limit) = params.limit {
search_builder.limit(limit); search_builder.limit(limit);
} }
let available_attributes = schema.displayed_name(); let available_attributes = schema.displayed_name();
let mut restricted_attributes: HashSet<&str>; let mut restricted_attributes: HashSet<&str>;
match &query.attributes_to_retrieve { match &params.attributes_to_retrieve {
Some(attributes_to_retrieve) => { Some(attributes_to_retrieve) => {
let attributes_to_retrieve: HashSet<&str> = attributes_to_retrieve.split(',').collect(); let attributes_to_retrieve: HashSet<&str> = attributes_to_retrieve.split(',').collect();
if attributes_to_retrieve.contains("*") { if attributes_to_retrieve.contains("*") {
@ -78,8 +81,8 @@ pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> {
} }
} }
if let Some(attributes_to_crop) = query.attributes_to_crop { if let Some(attributes_to_crop) = &params.attributes_to_crop {
let default_length = query.crop_length.unwrap_or(200); let default_length = params.crop_length.unwrap_or(200);
let mut final_attributes: HashMap<String, usize> = HashMap::new(); let mut final_attributes: HashMap<String, usize> = HashMap::new();
for attribute in attributes_to_crop.split(',') { for attribute in attributes_to_crop.split(',') {
@ -106,7 +109,7 @@ pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> {
search_builder.attributes_to_crop(final_attributes); search_builder.attributes_to_crop(final_attributes);
} }
if let Some(attributes_to_highlight) = query.attributes_to_highlight { if let Some(attributes_to_highlight) = &params.attributes_to_highlight {
let mut final_attributes: HashSet<String> = HashSet::new(); let mut final_attributes: HashSet<String> = HashSet::new();
for attribute in attributes_to_highlight.split(',') { for attribute in attributes_to_highlight.split(',') {
if attribute == "*" { if attribute == "*" {
@ -125,144 +128,15 @@ pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> {
search_builder.attributes_to_highlight(final_attributes); search_builder.attributes_to_highlight(final_attributes);
} }
if let Some(filters) = query.filters { if let Some(filters) = &params.filters {
search_builder.filters(filters); search_builder.filters(filters.to_string());
} }
if let Some(timeout_ms) = query.timeout_ms { if let Some(matches) = params.matches {
search_builder.timeout(Duration::from_millis(timeout_ms));
}
if let Some(matches) = query.matches {
if matches { if matches {
search_builder.get_matches(); search_builder.get_matches();
} }
} }
let response = match search_builder.search(&reader) { Ok(HttpResponse::Ok().json(search_builder.search(&reader)?))
Ok(response) => response,
Err(Error::Internal(message)) => return Err(ResponseError::Internal(message)),
Err(others) => return Err(ResponseError::bad_request(others)),
};
Ok(tide::Response::new(200).body_json(&response).unwrap())
}
#[derive(Clone, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct SearchMultiBody {
indexes: HashSet<String>,
query: String,
offset: Option<usize>,
limit: Option<usize>,
attributes_to_retrieve: Option<HashSet<String>>,
searchable_attributes: Option<HashSet<String>>,
attributes_to_crop: Option<HashMap<String, usize>>,
attributes_to_highlight: Option<HashSet<String>>,
filters: Option<String>,
timeout_ms: Option<u64>,
matches: Option<bool>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct SearchMultiBodyResponse {
hits: HashMap<String, Vec<SearchHit>>,
offset: usize,
hits_per_page: usize,
processing_time_ms: usize,
query: String,
}
pub async fn search_multi_index(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Public)?;
let body = ctx
.body_json::<SearchMultiBody>()
.await
.map_err(ResponseError::bad_request)?;
let mut index_list = body.clone().indexes;
for index in index_list.clone() {
if index == "*" {
index_list = ctx.state().db.indexes_uids().into_iter().collect();
break;
}
}
let mut offset = 0;
let mut count = 20;
if let Some(body_offset) = body.offset {
if let Some(limit) = body.limit {
offset = body_offset;
count = limit;
}
}
let offset = offset;
let count = count;
let db = &ctx.state().db;
let par_body = body.clone();
let responses_per_index: Vec<SResult<_>> = index_list
.into_par_iter()
.map(move |index_uid| {
let index: Index = db
.open_index(&index_uid)
.ok_or(ResponseError::index_not_found(&index_uid))?;
let mut search_builder = index.new_search(par_body.query.clone());
search_builder.offset(offset);
search_builder.limit(count);
if let Some(attributes_to_retrieve) = par_body.attributes_to_retrieve.clone() {
search_builder.attributes_to_retrieve(attributes_to_retrieve);
}
if let Some(attributes_to_crop) = par_body.attributes_to_crop.clone() {
search_builder.attributes_to_crop(attributes_to_crop);
}
if let Some(attributes_to_highlight) = par_body.attributes_to_highlight.clone() {
search_builder.attributes_to_highlight(attributes_to_highlight);
}
if let Some(filters) = par_body.filters.clone() {
search_builder.filters(filters);
}
if let Some(timeout_ms) = par_body.timeout_ms {
search_builder.timeout(Duration::from_millis(timeout_ms));
}
if let Some(matches) = par_body.matches {
if matches {
search_builder.get_matches();
}
}
let reader = db.main_read_txn()?;
let response = search_builder.search(&reader)?;
Ok((index_uid, response))
})
.collect();
let mut hits_map = HashMap::new();
let mut max_query_time = 0;
for response in responses_per_index {
if let Ok((index_uid, response)) = response {
if response.processing_time_ms > max_query_time {
max_query_time = response.processing_time_ms;
}
hits_map.insert(index_uid, response.hits);
}
}
let response = SearchMultiBodyResponse {
hits: hits_map,
offset,
hits_per_page: count,
processing_time_ms: max_query_time,
query: body.query,
};
Ok(tide::Response::new(200).body_json(&response).unwrap())
} }

View File

@ -1,18 +1,66 @@
use actix_web::{web, HttpResponse};
use actix_web_macros::{delete, get, post};
use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES}; use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES};
use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashSet};
use tide::{Request, Response};
use crate::error::{ResponseError, SResult}; use crate::error::ResponseError;
use crate::helpers::tide::RequestExt; use crate::helpers::Authentication;
use crate::helpers::tide::ACL::*; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::routes::document::IndexUpdateResponse;
use crate::Data; use crate::Data;
pub async fn get_all(ctx: Request<Data>) -> SResult<Response> { pub fn services(cfg: &mut web::ServiceConfig) {
ctx.is_allowed(Private)?; cfg.service(update_all)
let index = ctx.index()?; .service(get_all)
let db = &ctx.state().db; .service(delete_all)
let reader = db.main_read_txn()?; .service(get_rules)
.service(update_rules)
.service(delete_rules)
.service(get_distinct)
.service(update_distinct)
.service(delete_distinct)
.service(get_searchable)
.service(update_searchable)
.service(delete_searchable)
.service(get_displayed)
.service(update_displayed)
.service(delete_displayed)
.service(get_accept_new_fields)
.service(update_accept_new_fields);
}
#[post("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
async fn update_all(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Settings>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?;
let settings = body
.into_inner()
.into_update()
.map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?;
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}
#[get("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
async fn get_all(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let stop_words_fst = index.main.stop_words_fst(&reader)?; let stop_words_fst = index.main.stop_words_fst(&reader)?;
let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?; let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?;
@ -46,14 +94,14 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let searchable_attributes = schema.clone().map(|s| { let searchable_attributes = schema.clone().map(|s| {
s.indexed_name() s.indexed_name()
.iter() .iter()
.map(|s| (*s).to_string()) .map(|s| s.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
}); });
let displayed_attributes = schema.clone().map(|s| { let displayed_attributes = schema.clone().map(|s| {
s.displayed_name() s.displayed_name()
.iter() .iter()
.map(|s| (*s).to_string()) .map(|s| s.to_string())
.collect::<HashSet<String>>() .collect::<HashSet<String>>()
}); });
@ -69,30 +117,19 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
accept_new_fields: Some(accept_new_fields), accept_new_fields: Some(accept_new_fields),
}; };
Ok(tide::Response::new(200).body_json(&settings).unwrap()) Ok(HttpResponse::Ok().json(settings))
} }
pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> { #[delete("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
ctx.is_allowed(Private)?; async fn delete_all(
let index = ctx.index()?; data: web::Data<Data>,
let settings: Settings = path: web::Path<IndexParam>,
ctx.body_json().await.map_err(ResponseError::bad_request)?; ) -> Result<HttpResponse, ResponseError> {
let db = &ctx.state().db; let index = data
.db
let mut writer = db.update_write_txn()?; .open_index(&path.index_uid)
let settings = settings.into_update().map_err(ResponseError::bad_request)?; .ok_or(ResponseError::index_not_found(&path.index_uid))?;
let update_id = index.settings_update(&mut writer, settings)?; let mut writer = data.db.update_write_txn()?;
writer.commit()?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
}
pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Private)?;
let index = ctx.index()?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn()?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
ranking_rules: UpdateState::Clear, ranking_rules: UpdateState::Clear,
@ -106,18 +143,24 @@ pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
}; };
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> { #[get(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/ranking-rules",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
let reader = db.main_read_txn()?; async fn get_rules(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let ranking_rules = index let ranking_rules = index
.main .main
@ -127,35 +170,49 @@ pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> {
.map(|r| r.to_string()) .map(|r| r.to_string())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap()) Ok(HttpResponse::Ok().json(ranking_rules))
} }
pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/ranking-rules",
let index = ctx.index()?; wrap = "Authentication::Private"
let ranking_rules: Option<Vec<String>> = )]
ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn update_rules(
let db = &ctx.state().db; data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Option<Vec<String>>>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
ranking_rules: Some(ranking_rules), ranking_rules: Some(body.into_inner()),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> { #[delete(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/ranking-rules",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
let mut writer = db.update_write_txn()?; async fn delete_rules(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
ranking_rules: UpdateState::Clear, ranking_rules: UpdateState::Clear,
@ -166,49 +223,67 @@ pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> { #[get(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/distinct-attribute",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
let reader = db.main_read_txn()?; async fn get_distinct(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let distinct_attribute = index.main.distinct_attribute(&reader)?; let distinct_attribute = index.main.distinct_attribute(&reader)?;
Ok(tide::Response::new(200) Ok(HttpResponse::Ok().json(distinct_attribute))
.body_json(&distinct_attribute)
.unwrap())
} }
pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/distinct-attribute",
let index = ctx.index()?; wrap = "Authentication::Private"
let distinct_attribute: Option<String> = )]
ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn update_distinct(
let db = &ctx.state().db; data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Option<String>>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
distinct_attribute: Some(distinct_attribute), distinct_attribute: Some(body.into_inner()),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> { #[delete(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/distinct-attribute",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
let mut writer = db.update_write_txn()?; async fn delete_distinct(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
distinct_attribute: UpdateState::Clear, distinct_attribute: UpdateState::Clear,
@ -219,156 +294,199 @@ pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> { #[get(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/searchable-attributes",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
let reader = db.main_read_txn()?; async fn get_searchable(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let searchable_attributes: Option<Vec<String>> = let searchable_attributes: Option<Vec<String>> =
schema.map(|s| s.indexed_name().iter().map(|i| (*i).to_string()).collect()); schema.map(|s| s.indexed_name().iter().map(|i| i.to_string()).collect());
Ok(tide::Response::new(200) Ok(HttpResponse::Ok().json(searchable_attributes))
.body_json(&searchable_attributes)
.unwrap())
} }
pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/searchable-attributes",
let index = ctx.index()?; wrap = "Authentication::Private"
let searchable_attributes: Option<Vec<String>> = )]
ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn update_searchable(
let db = &ctx.state().db; data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Option<Vec<String>>>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
searchable_attributes: Some(searchable_attributes), searchable_attributes: Some(body.into_inner()),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> { #[delete(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/searchable-attributes",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
async fn delete_searchable(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
searchable_attributes: UpdateState::Clear, searchable_attributes: UpdateState::Clear,
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn displayed(ctx: Request<Data>) -> SResult<Response> { #[get(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/displayed-attributes",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
let reader = db.main_read_txn()?; async fn get_displayed(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let displayed_attributes: Option<HashSet<String>> = schema.map(|s| { let displayed_attributes: Option<HashSet<String>> =
s.displayed_name() schema.map(|s| s.displayed_name().iter().map(|i| i.to_string()).collect());
.iter()
.map(|i| (*i).to_string())
.collect()
});
Ok(tide::Response::new(200) Ok(HttpResponse::Ok().json(displayed_attributes))
.body_json(&displayed_attributes)
.unwrap())
} }
pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/displayed-attributes",
let index = ctx.index()?; wrap = "Authentication::Private"
let displayed_attributes: Option<HashSet<String>> = )]
ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn update_displayed(
let db = &ctx.state().db; data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Option<HashSet<String>>>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
displayed_attributes: Some(displayed_attributes), displayed_attributes: Some(body.into_inner()),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> { #[delete(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/displayed-attributes",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
async fn delete_displayed(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
displayed_attributes: UpdateState::Clear, displayed_attributes: UpdateState::Clear,
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_accept_new_fields(ctx: Request<Data>) -> SResult<Response> { #[get(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/accept-new-fields",
let index = ctx.index()?; wrap = "Authentication::Private"
let db = &ctx.state().db; )]
let reader = db.main_read_txn()?; async fn get_accept_new_fields(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let accept_new_fields = schema.map(|s| s.accept_new_fields()); let accept_new_fields = schema.map(|s| s.accept_new_fields());
Ok(tide::Response::new(200) Ok(HttpResponse::Ok().json(accept_new_fields))
.body_json(&accept_new_fields)
.unwrap())
} }
pub async fn update_accept_new_fields(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/accept-new-fields",
let index = ctx.index()?; wrap = "Authentication::Private"
let accept_new_fields: Option<bool> = )]
ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn update_accept_new_fields(
let db = &ctx.state().db; data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<Option<bool>>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
accept_new_fields: Some(accept_new_fields), accept_new_fields: Some(body.into_inner()),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(ResponseError::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -1,18 +1,28 @@
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::web;
use actix_web::HttpResponse;
use actix_web_macros::get;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use log::error; use log::error;
use pretty_bytes::converter::convert; use pretty_bytes::converter::convert;
use serde::Serialize; use serde::Serialize;
use sysinfo::{NetworkExt, Pid, ProcessExt, ProcessorExt, System, SystemExt}; use sysinfo::{NetworkExt, ProcessExt, ProcessorExt, System, SystemExt};
use tide::{Request, Response};
use walkdir::WalkDir; use walkdir::WalkDir;
use crate::error::{IntoInternalError, SResult}; use crate::error::ResponseError;
use crate::helpers::tide::RequestExt; use crate::helpers::Authentication;
use crate::helpers::tide::ACL::*; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(index_stats)
.service(get_stats)
.service(get_version)
.service(get_sys_info)
.service(get_sys_info_pretty);
}
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct IndexStatsResponse { struct IndexStatsResponse {
@ -21,26 +31,35 @@ struct IndexStatsResponse {
fields_frequency: HashMap<String, usize>, fields_frequency: HashMap<String, usize>,
} }
pub async fn index_stats(ctx: Request<Data>) -> SResult<Response> { #[get("/indexes/{index_uid}/stats", wrap = "Authentication::Private")]
ctx.is_allowed(Admin)?; async fn index_stats(
let index_uid = ctx.url_param("index")?; data: web::Data<Data>,
let index = ctx.index()?; path: web::Path<IndexParam>,
let db = &ctx.state().db; ) -> Result<HttpResponse, ResponseError> {
let reader = db.main_read_txn()?; let index = data
let update_reader = db.update_read_txn()?; .db
let number_of_documents = index.main.number_of_documents(&reader)?; .open_index(&path.index_uid)
let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default(); .ok_or(ResponseError::index_not_found(&path.index_uid))?;
let is_indexing = ctx
.state()
.is_indexing(&update_reader, &index_uid)?
.into_internal_error()?;
let response = IndexStatsResponse { let reader = data.db.main_read_txn()?;
let number_of_documents = index.main.number_of_documents(&reader)?;
let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default();
let update_reader = data.db.update_read_txn()?;
let is_indexing =
data.is_indexing(&update_reader, &path.index_uid)?
.ok_or(ResponseError::internal(
"Impossible to know if the database is indexing",
))?;
Ok(HttpResponse::Ok().json(IndexStatsResponse {
number_of_documents, number_of_documents,
is_indexing, is_indexing,
fields_frequency, fields_frequency,
}; }))
Ok(tide::Response::new(200).body_json(&response).unwrap())
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -51,29 +70,25 @@ struct StatsResult {
indexes: HashMap<String, IndexStatsResponse>, indexes: HashMap<String, IndexStatsResponse>,
} }
pub async fn get_stats(ctx: Request<Data>) -> SResult<Response> { #[get("/stats", wrap = "Authentication::Private")]
ctx.is_allowed(Admin)?; async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let mut index_list = HashMap::new(); let mut index_list = HashMap::new();
let db = &ctx.state().db; let reader = data.db.main_read_txn()?;
let reader = db.main_read_txn()?; let update_reader = data.db.update_read_txn()?;
let update_reader = db.update_read_txn()?;
let indexes_set = ctx.state().db.indexes_uids(); let indexes_set = data.db.indexes_uids();
for index_uid in indexes_set { for index_uid in indexes_set {
let index = ctx.state().db.open_index(&index_uid); let index = data.db.open_index(&index_uid);
match index { match index {
Some(index) => { Some(index) => {
let number_of_documents = index.main.number_of_documents(&reader)?; let number_of_documents = index.main.number_of_documents(&reader)?;
let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default(); let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default();
let is_indexing = ctx let is_indexing = data.is_indexing(&update_reader, &index_uid)?.ok_or(
.state() ResponseError::internal("Impossible to know if the database is indexing"),
.is_indexing(&update_reader, &index_uid)? )?;
.into_internal_error()?;
let response = IndexStatsResponse { let response = IndexStatsResponse {
number_of_documents, number_of_documents,
@ -89,22 +104,20 @@ pub async fn get_stats(ctx: Request<Data>) -> SResult<Response> {
} }
} }
let database_size = WalkDir::new(ctx.state().db_path.clone()) let database_size = WalkDir::new(&data.db_path)
.into_iter() .into_iter()
.filter_map(|entry| entry.ok()) .filter_map(|entry| entry.ok())
.filter_map(|entry| entry.metadata().ok()) .filter_map(|entry| entry.metadata().ok())
.filter(|metadata| metadata.is_file()) .filter(|metadata| metadata.is_file())
.fold(0, |acc, m| acc + m.len()); .fold(0, |acc, m| acc + m.len());
let last_update = ctx.state().last_update(&reader)?; let last_update = data.last_update(&reader)?;
let response = StatsResult { Ok(HttpResponse::Ok().json(StatsResult {
database_size, database_size,
last_update, last_update,
indexes: index_list, indexes: index_list,
}; }))
Ok(tide::Response::new(200).body_json(&response).unwrap())
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -115,20 +128,18 @@ struct VersionResponse {
pkg_version: String, pkg_version: String,
} }
pub async fn get_version(ctx: Request<Data>) -> SResult<Response> { #[get("/version", wrap = "Authentication::Private")]
ctx.is_allowed(Admin)?; async fn get_version() -> HttpResponse {
let response = VersionResponse { HttpResponse::Ok().json(VersionResponse {
commit_sha: env!("VERGEN_SHA").to_string(), commit_sha: env!("VERGEN_SHA").to_string(),
build_date: env!("VERGEN_BUILD_TIMESTAMP").to_string(), build_date: env!("VERGEN_BUILD_TIMESTAMP").to_string(),
pkg_version: env!("CARGO_PKG_VERSION").to_string(), pkg_version: env!("CARGO_PKG_VERSION").to_string(),
}; })
Ok(tide::Response::new(200).body_json(&response).unwrap())
} }
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct SysGlobal { struct SysGlobal {
total_memory: u64, total_memory: u64,
used_memory: u64, used_memory: u64,
total_swap: u64, total_swap: u64,
@ -152,7 +163,7 @@ impl SysGlobal {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct SysProcess { struct SysProcess {
memory: u64, memory: u64,
cpu: f32, cpu: f32,
} }
@ -168,7 +179,7 @@ impl SysProcess {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct SysInfo { struct SysInfo {
memory_usage: f64, memory_usage: f64,
processor_usage: Vec<f32>, processor_usage: Vec<f32>,
global: SysGlobal, global: SysGlobal,
@ -186,7 +197,8 @@ impl SysInfo {
} }
} }
pub(crate) fn report(pid: Pid) -> SysInfo { #[get("/sys-info", wrap = "Authentication::Private")]
async fn get_sys_info(data: web::Data<Data>) -> HttpResponse {
let mut sys = System::new(); let mut sys = System::new();
let mut info = SysInfo::new(); let mut info = SysInfo::new();
@ -200,28 +212,29 @@ pub(crate) fn report(pid: Pid) -> SysInfo {
info.global.used_memory = sys.get_used_memory(); info.global.used_memory = sys.get_used_memory();
info.global.total_swap = sys.get_total_swap(); info.global.total_swap = sys.get_total_swap();
info.global.used_swap = sys.get_used_swap(); info.global.used_swap = sys.get_used_swap();
info.global.input_data = sys.get_networks().into_iter().map(|(_, n)| n.get_received()).sum::<u64>(); info.global.input_data = sys
info.global.output_data = sys.get_networks().into_iter().map(|(_, n)| n.get_transmitted()).sum::<u64>(); .get_networks()
.into_iter()
.map(|(_, n)| n.get_received())
.sum::<u64>();
info.global.output_data = sys
.get_networks()
.into_iter()
.map(|(_, n)| n.get_transmitted())
.sum::<u64>();
if let Some(process) = sys.get_process(pid) { if let Some(process) = sys.get_process(data.server_pid) {
info.process.memory = process.memory(); info.process.memory = process.memory();
info.process.cpu = process.cpu_usage() * 100.0; info.process.cpu = process.cpu_usage() * 100.0;
} }
sys.refresh_all(); sys.refresh_all();
HttpResponse::Ok().json(info)
info
}
pub async fn get_sys_info(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Admin)?;
let response = report(ctx.state().server_pid);
Ok(tide::Response::new(200).body_json(&response).unwrap())
} }
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct SysGlobalPretty { struct SysGlobalPretty {
total_memory: String, total_memory: String,
used_memory: String, used_memory: String,
total_swap: String, total_swap: String,
@ -245,7 +258,7 @@ impl SysGlobalPretty {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct SysProcessPretty { struct SysProcessPretty {
memory: String, memory: String,
cpu: String, cpu: String,
} }
@ -261,7 +274,7 @@ impl SysProcessPretty {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub(crate) struct SysInfoPretty { struct SysInfoPretty {
memory_usage: String, memory_usage: String,
processor_usage: Vec<String>, processor_usage: Vec<String>,
global: SysGlobalPretty, global: SysGlobalPretty,
@ -279,7 +292,8 @@ impl SysInfoPretty {
} }
} }
pub(crate) fn report_pretty(pid: Pid) -> SysInfoPretty { #[get("/sys-info/pretty", wrap = "Authentication::Private")]
async fn get_sys_info_pretty(data: web::Data<Data>) -> HttpResponse {
let mut sys = System::new(); let mut sys = System::new();
let mut info = SysInfoPretty::new(); let mut info = SysInfoPretty::new();
@ -297,21 +311,25 @@ pub(crate) fn report_pretty(pid: Pid) -> SysInfoPretty {
info.global.used_memory = convert(sys.get_used_memory() as f64 * 1024.0); info.global.used_memory = convert(sys.get_used_memory() as f64 * 1024.0);
info.global.total_swap = convert(sys.get_total_swap() as f64 * 1024.0); info.global.total_swap = convert(sys.get_total_swap() as f64 * 1024.0);
info.global.used_swap = convert(sys.get_used_swap() as f64 * 1024.0); info.global.used_swap = convert(sys.get_used_swap() as f64 * 1024.0);
info.global.input_data = convert(sys.get_networks().into_iter().map(|(_, n)| n.get_received()).sum::<u64>() as f64); info.global.input_data = convert(
info.global.output_data = convert(sys.get_networks().into_iter().map(|(_, n)| n.get_transmitted()).sum::<u64>() as f64); sys.get_networks()
.into_iter()
.map(|(_, n)| n.get_received())
.sum::<u64>() as f64,
);
info.global.output_data = convert(
sys.get_networks()
.into_iter()
.map(|(_, n)| n.get_transmitted())
.sum::<u64>() as f64,
);
if let Some(process) = sys.get_process(pid) { if let Some(process) = sys.get_process(data.server_pid) {
info.process.memory = convert(process.memory() as f64 * 1024.0); info.process.memory = convert(process.memory() as f64 * 1024.0);
info.process.cpu = format!("{:.1} %", process.cpu_usage() * 100.0); info.process.cpu = format!("{:.1} %", process.cpu_usage() * 100.0);
} }
sys.refresh_all(); sys.refresh_all();
info HttpResponse::Ok().json(info)
}
pub async fn get_sys_info_pretty(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Admin)?;
let response = report_pretty(ctx.state().server_pid);
Ok(tide::Response::new(200).body_json(&response).unwrap())
} }

View File

@ -1,63 +1,83 @@
use actix_web::{web, HttpResponse};
use actix_web_macros::{delete, get, post};
use meilisearch_core::settings::{SettingsUpdate, UpdateState};
use std::collections::BTreeSet; use std::collections::BTreeSet;
use meilisearch_core::settings::{SettingsUpdate, UpdateState}; use crate::error::ResponseError;
use tide::{Request, Response}; use crate::helpers::Authentication;
use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::error::{ResponseError, SResult};
use crate::helpers::tide::RequestExt;
use crate::helpers::tide::ACL::*;
use crate::routes::document::IndexUpdateResponse;
use crate::Data; use crate::Data;
pub async fn get(ctx: Request<Data>) -> SResult<Response> { pub fn services(cfg: &mut web::ServiceConfig) {
ctx.is_allowed(Private)?; cfg.service(get).service(update).service(delete);
let index = ctx.index()?; }
let db = &ctx.state().db;
let reader = db.main_read_txn()?; #[get(
"/indexes/{index_uid}/settings/stop-words",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let stop_words_fst = index.main.stop_words_fst(&reader)?; let stop_words_fst = index.main.stop_words_fst(&reader)?;
let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?; let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?;
Ok(tide::Response::new(200).body_json(&stop_words).unwrap()) Ok(HttpResponse::Ok().json(stop_words))
} }
pub async fn update(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/stop-words",
let index = ctx.index()?; wrap = "Authentication::Private"
)]
let data: BTreeSet<String> = ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn update(
data: web::Data<Data>,
let db = &ctx.state().db; path: web::Path<IndexParam>,
let mut writer = db.update_write_txn()?; body: web::Json<BTreeSet<String>>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
stop_words: UpdateState::Update(data), stop_words: UpdateState::Update(body.into_inner()),
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete(ctx: Request<Data>) -> SResult<Response> { #[delete(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/stop-words",
let index = ctx.index()?; wrap = "Authentication::Private"
)]
let db = &ctx.state().db; async fn delete(
let mut writer = db.update_write_txn()?; data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
stop_words: UpdateState::Clear, stop_words: UpdateState::Clear,
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -1,29 +1,39 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use actix_web::{web, HttpResponse};
use actix_web_macros::{delete, get, post};
use indexmap::IndexMap; use indexmap::IndexMap;
use meilisearch_core::settings::{SettingsUpdate, UpdateState}; use meilisearch_core::settings::{SettingsUpdate, UpdateState};
use tide::{Request, Response};
use crate::error::{ResponseError, SResult}; use crate::error::ResponseError;
use crate::helpers::tide::RequestExt; use crate::helpers::Authentication;
use crate::helpers::tide::ACL::*; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::routes::document::IndexUpdateResponse;
use crate::Data; use crate::Data;
pub async fn get(ctx: Request<Data>) -> SResult<Response> { pub fn services(cfg: &mut web::ServiceConfig) {
ctx.is_allowed(Private)?; cfg.service(get).service(update).service(delete);
let index = ctx.index()?; }
let db = &ctx.state().db; #[get(
let reader = db.main_read_txn()?; "/indexes/{index_uid}/settings/synonyms",
wrap = "Authentication::Private"
)]
async fn get(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default(); let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default();
let synonyms_list = synonyms_fst.stream().into_strs()?; let synonyms_list = synonyms_fst.stream().into_strs()?;
let mut synonyms = IndexMap::new(); let mut synonyms = IndexMap::new();
let index_synonyms = &index.synonyms; let index_synonyms = &index.synonyms;
for synonym in synonyms_list { for synonym in synonyms_list {
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?; let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
@ -33,50 +43,57 @@ pub async fn get(ctx: Request<Data>) -> SResult<Response> {
} }
} }
Ok(tide::Response::new(200).body_json(&synonyms).unwrap()) Ok(HttpResponse::Ok().json(synonyms))
} }
pub async fn update(mut ctx: Request<Data>) -> SResult<Response> { #[post(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/synonyms",
wrap = "Authentication::Private"
let data: BTreeMap<String, Vec<String>> = )]
ctx.body_json().await.map_err(ResponseError::bad_request)?; async fn update(
data: web::Data<Data>,
let index = ctx.index()?; path: web::Path<IndexParam>,
body: web::Json<BTreeMap<String, Vec<String>>>,
let db = &ctx.state().db; ) -> Result<HttpResponse, ResponseError> {
let mut writer = db.update_write_txn()?; let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
synonyms: UpdateState::Update(data), synonyms: UpdateState::Update(body.into_inner()),
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete(ctx: Request<Data>) -> SResult<Response> { #[delete(
ctx.is_allowed(Private)?; "/indexes/{index_uid}/settings/synonyms",
wrap = "Authentication::Private"
let index = ctx.index()?; )]
async fn delete(
let db = &ctx.state().db; data: web::Data<Data>,
let mut writer = db.update_write_txn()?; path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
synonyms: UpdateState::Clear, synonyms: UpdateState::Clear,
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -1,23 +1,17 @@
#![allow(dead_code)] #![allow(dead_code)]
use http::StatusCode; use serde_json::{json, Value};
use serde_json::Value;
use std::time::Duration; use std::time::Duration;
use async_std::io::prelude::*; use actix_web::{http::StatusCode, test};
use async_std::task::{block_on, sleep};
use http_service::Body;
use http_service_mock::{make_server, TestBackend};
use meilisearch_http::data::Data; use meilisearch_http::data::Data;
use meilisearch_http::option::Opt; use meilisearch_http::option::Opt;
use meilisearch_http::routes;
use serde_json::json;
use tempdir::TempDir; use tempdir::TempDir;
use tide::server::Service; use tokio::time::delay_for;
pub struct Server { pub struct Server {
uid: String, uid: String,
mock: TestBackend<Service<Data>>, data: Data,
} }
impl Server { impl Server {
@ -33,20 +27,16 @@ impl Server {
}; };
let data = Data::new(opt.clone()); let data = Data::new(opt.clone());
let mut app = tide::with_state(data);
routes::load_routes(&mut app);
let http_server = app.into_http_service();
let mock = make_server(http_server).unwrap();
Server { Server {
uid: uid.to_string(), uid: uid.to_string(),
mock, data: data,
} }
} }
pub fn wait_update_id(&mut self, update_id: u64) { pub async fn wait_update_id(&mut self, update_id: u64) {
loop { loop {
let (response, status_code) = self.get_update_status(update_id); let (response, status_code) = self.get_update_status(update_id).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
if response["status"] == "processed" || response["status"] == "error" { if response["status"] == "processed" || response["status"] == "error" {
@ -54,350 +44,365 @@ impl Server {
return; return;
} }
block_on(sleep(Duration::from_secs(1))); delay_for(Duration::from_secs(1)).await;
} }
} }
// Global Http request GET/POST/DELETE async or sync // Global Http request GET/POST/DELETE async or sync
pub fn get_request(&mut self, url: &str) -> (Value, StatusCode) { pub async fn get_request(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("get_request: {}", url); eprintln!("get_request: {}", url);
let req = http::Request::get(url).body(Body::empty()).unwrap();
let res = self.mock.simulate(req).unwrap(); let mut app = test::init_service(meilisearch_http::create_app(&self.data)).await;
let req = test::TestRequest::get().uri(url).to_request();
let res = test::call_service(&mut app, req).await;
let status_code = res.status().clone(); let status_code = res.status().clone();
let mut buf = Vec::new(); let body = test::read_body(res).await;
block_on(res.into_body().read_to_end(&mut buf)).unwrap(); let response = serde_json::from_slice(&body).unwrap_or_default();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code) (response, status_code)
} }
pub fn post_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) { pub async fn post_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("post_request: {}", url); eprintln!("post_request: {}", url);
let body_bytes = body.to_string().into_bytes();
let req = http::Request::post(url) let mut app = test::init_service(meilisearch_http::create_app(&self.data)).await;
.body(Body::from(body_bytes))
.unwrap(); let req = test::TestRequest::post()
let res = self.mock.simulate(req).unwrap(); .uri(url)
.set_json(&body)
.to_request();
let res = test::call_service(&mut app, req).await;
let status_code = res.status().clone(); let status_code = res.status().clone();
let mut buf = Vec::new(); let body = test::read_body(res).await;
block_on(res.into_body().read_to_end(&mut buf)).unwrap(); let response = serde_json::from_slice(&body).unwrap_or_default();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code) (response, status_code)
} }
pub fn post_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) { pub async fn post_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("post_request_async: {}", url); eprintln!("post_request_async: {}", url);
let (response, status_code) = self.post_request(url, body);
let (response, status_code) = self.post_request(url, body).await;
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
assert!(response["updateId"].as_u64().is_some()); assert!(response["updateId"].as_u64().is_some());
self.wait_update_id(response["updateId"].as_u64().unwrap()); self.wait_update_id(response["updateId"].as_u64().unwrap())
.await;
(response, status_code) (response, status_code)
} }
pub fn put_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) { pub async fn put_request(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("put_request: {}", url); eprintln!("put_request: {}", url);
let body_bytes = body.to_string().into_bytes();
let req = http::Request::put(url) let mut app = test::init_service(meilisearch_http::create_app(&self.data)).await;
.body(Body::from(body_bytes))
.unwrap(); let req = test::TestRequest::put()
let res = self.mock.simulate(req).unwrap(); .uri(url)
.set_json(&body)
.to_request();
let res = test::call_service(&mut app, req).await;
let status_code = res.status().clone(); let status_code = res.status().clone();
let mut buf = Vec::new(); let body = test::read_body(res).await;
block_on(res.into_body().read_to_end(&mut buf)).unwrap(); let response = serde_json::from_slice(&body).unwrap_or_default();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code) (response, status_code)
} }
pub fn put_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) { pub async fn put_request_async(&mut self, url: &str, body: Value) -> (Value, StatusCode) {
eprintln!("put_request_async: {}", url); eprintln!("put_request_async: {}", url);
let (response, status_code) = self.put_request(url, body);
let (response, status_code) = self.put_request(url, body).await;
assert!(response["updateId"].as_u64().is_some()); assert!(response["updateId"].as_u64().is_some());
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
self.wait_update_id(response["updateId"].as_u64().unwrap()); self.wait_update_id(response["updateId"].as_u64().unwrap())
.await;
(response, status_code) (response, status_code)
} }
pub fn delete_request(&mut self, url: &str) -> (Value, StatusCode) { pub async fn delete_request(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("delete_request: {}", url); eprintln!("delete_request: {}", url);
let req = http::Request::delete(url).body(Body::empty()).unwrap();
let res = self.mock.simulate(req).unwrap(); let mut app = test::init_service(meilisearch_http::create_app(&self.data)).await;
let req = test::TestRequest::delete().uri(url).to_request();
let res = test::call_service(&mut app, req).await;
let status_code = res.status().clone(); let status_code = res.status().clone();
let mut buf = Vec::new(); let body = test::read_body(res).await;
block_on(res.into_body().read_to_end(&mut buf)).unwrap(); let response = serde_json::from_slice(&body).unwrap_or_default();
let response = serde_json::from_slice(&buf).unwrap_or_default();
(response, status_code) (response, status_code)
} }
pub fn delete_request_async(&mut self, url: &str) -> (Value, StatusCode) { pub async fn delete_request_async(&mut self, url: &str) -> (Value, StatusCode) {
eprintln!("delete_request_async: {}", url); eprintln!("delete_request_async: {}", url);
let (response, status_code) = self.delete_request(url);
let (response, status_code) = self.delete_request(url).await;
assert!(response["updateId"].as_u64().is_some()); assert!(response["updateId"].as_u64().is_some());
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
self.wait_update_id(response["updateId"].as_u64().unwrap()); self.wait_update_id(response["updateId"].as_u64().unwrap())
.await;
(response, status_code) (response, status_code)
} }
// All Routes // All Routes
pub fn list_indexes(&mut self) -> (Value, StatusCode) { pub async fn list_indexes(&mut self) -> (Value, StatusCode) {
self.get_request("/indexes") self.get_request("/indexes").await
} }
pub fn create_index(&mut self, body: Value) -> (Value, StatusCode) { pub async fn create_index(&mut self, body: Value) -> (Value, StatusCode) {
self.post_request("/indexes", body) self.post_request("/indexes", body).await
} }
pub fn search_multi_index(&mut self, query: &str) -> (Value, StatusCode) { pub async fn search_multi_index(&mut self, query: &str) -> (Value, StatusCode) {
let url = format!("/indexes/search?{}", query); let url = format!("/indexes/search?{}", query);
self.get_request(&url) self.get_request(&url).await
} }
pub fn get_index(&mut self) -> (Value, StatusCode) { pub async fn get_index(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}", self.uid); let url = format!("/indexes/{}", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_index(&mut self, body: Value) -> (Value, StatusCode) { pub async fn update_index(&mut self, body: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}", self.uid); let url = format!("/indexes/{}", self.uid);
self.put_request(&url, body) self.put_request(&url, body).await
} }
pub fn delete_index(&mut self) -> (Value, StatusCode) { pub async fn delete_index(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}", self.uid); let url = format!("/indexes/{}", self.uid);
self.delete_request(&url) self.delete_request(&url).await
} }
pub fn search(&mut self, query: &str) -> (Value, StatusCode) { pub async fn search(&mut self, query: &str) -> (Value, StatusCode) {
let url = format!("/indexes/{}/search?{}", self.uid, query); let url = format!("/indexes/{}/search?{}", self.uid, query);
self.get_request(&url) self.get_request(&url).await
} }
pub fn get_all_updates_status(&mut self) -> (Value, StatusCode) { pub async fn get_all_updates_status(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/updates", self.uid); let url = format!("/indexes/{}/updates", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn get_update_status(&mut self, update_id: u64) -> (Value, StatusCode) { pub async fn get_update_status(&mut self, update_id: u64) -> (Value, StatusCode) {
let url = format!("/indexes/{}/updates/{}", self.uid, update_id); let url = format!("/indexes/{}/updates/{}", self.uid, update_id);
self.get_request(&url) self.get_request(&url).await
} }
pub fn get_all_documents(&mut self) -> (Value, StatusCode) { pub async fn get_all_documents(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents", self.uid); let url = format!("/indexes/{}/documents", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn add_or_replace_multiple_documents(&mut self, body: Value) { pub async fn add_or_replace_multiple_documents(&mut self, body: Value) {
let url = format!("/indexes/{}/documents", self.uid); let url = format!("/indexes/{}/documents", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn add_or_replace_multiple_documents_sync(&mut self, body: Value) -> (Value, StatusCode) { pub async fn add_or_replace_multiple_documents_sync(
&mut self,
body: Value,
) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents", self.uid); let url = format!("/indexes/{}/documents", self.uid);
self.post_request(&url, body) self.post_request(&url, body).await
} }
pub fn add_or_update_multiple_documents(&mut self, body: Value) { pub async fn add_or_update_multiple_documents(&mut self, body: Value) {
let url = format!("/indexes/{}/documents", self.uid); let url = format!("/indexes/{}/documents", self.uid);
self.put_request_async(&url, body); self.put_request_async(&url, body).await;
} }
pub fn clear_all_documents(&mut self) { pub async fn clear_all_documents(&mut self) {
let url = format!("/indexes/{}/documents", self.uid); let url = format!("/indexes/{}/documents", self.uid);
self.delete_request_async(&url); self.delete_request_async(&url).await;
} }
pub fn get_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) { pub async fn get_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) {
let url = format!( let url = format!(
"/indexes/{}/documents/{}", "/indexes/{}/documents/{}",
self.uid, self.uid,
document_id.to_string() document_id.to_string()
); );
self.get_request(&url) self.get_request(&url).await
} }
pub fn delete_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) { pub async fn delete_document(&mut self, document_id: impl ToString) -> (Value, StatusCode) {
let url = format!( let url = format!(
"/indexes/{}/documents/{}", "/indexes/{}/documents/{}",
self.uid, self.uid,
document_id.to_string() document_id.to_string()
); );
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn delete_multiple_documents(&mut self, body: Value) { pub async fn delete_multiple_documents(&mut self, body: Value) {
let url = format!("/indexes/{}/documents/delete-batch", self.uid); let url = format!("/indexes/{}/documents/delete-batch", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn get_all_settings(&mut self) -> (Value, StatusCode) { pub async fn get_all_settings(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings", self.uid); let url = format!("/indexes/{}/settings", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_all_settings(&mut self, body: Value) { pub async fn update_all_settings(&mut self, body: Value) {
let url = format!("/indexes/{}/settings", self.uid); let url = format!("/indexes/{}/settings", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn delete_all_settings(&mut self) -> (Value, StatusCode) { pub async fn delete_all_settings(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings", self.uid); let url = format!("/indexes/{}/settings", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn get_ranking_rules(&mut self) -> (Value, StatusCode) { pub async fn get_ranking_rules(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid); let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_ranking_rules(&mut self, body: Value) { pub async fn update_ranking_rules(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid); let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn update_ranking_rules_sync(&mut self, body: Value) -> (Value, StatusCode) { pub async fn update_ranking_rules_sync(&mut self, body: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid); let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.post_request(&url, body) self.post_request(&url, body).await
} }
pub fn delete_ranking_rules(&mut self) -> (Value, StatusCode) { pub async fn delete_ranking_rules(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/ranking-rules", self.uid); let url = format!("/indexes/{}/settings/ranking-rules", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn get_distinct_attribute(&mut self) -> (Value, StatusCode) { pub async fn get_distinct_attribute(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/distinct-attribute", self.uid); let url = format!("/indexes/{}/settings/distinct-attribute", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_distinct_attribute(&mut self, body: Value) { pub async fn update_distinct_attribute(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/distinct-attribute", self.uid); let url = format!("/indexes/{}/settings/distinct-attribute", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn delete_distinct_attribute(&mut self) -> (Value, StatusCode) { pub async fn delete_distinct_attribute(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/distinct-attribute", self.uid); let url = format!("/indexes/{}/settings/distinct-attribute", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn get_primary_key(&mut self) -> (Value, StatusCode) { pub async fn get_primary_key(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/primary_key", self.uid); let url = format!("/indexes/{}/settings/primary_key", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn get_searchable_attributes(&mut self) -> (Value, StatusCode) { pub async fn get_searchable_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/searchable-attributes", self.uid); let url = format!("/indexes/{}/settings/searchable-attributes", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_searchable_attributes(&mut self, body: Value) { pub async fn update_searchable_attributes(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/searchable-attributes", self.uid); let url = format!("/indexes/{}/settings/searchable-attributes", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn delete_searchable_attributes(&mut self) -> (Value, StatusCode) { pub async fn delete_searchable_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/searchable-attributes", self.uid); let url = format!("/indexes/{}/settings/searchable-attributes", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn get_displayed_attributes(&mut self) -> (Value, StatusCode) { pub async fn get_displayed_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/displayed-attributes", self.uid); let url = format!("/indexes/{}/settings/displayed-attributes", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_displayed_attributes(&mut self, body: Value) { pub async fn update_displayed_attributes(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/displayed-attributes", self.uid); let url = format!("/indexes/{}/settings/displayed-attributes", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn delete_displayed_attributes(&mut self) -> (Value, StatusCode) { pub async fn delete_displayed_attributes(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/displayed-attributes", self.uid); let url = format!("/indexes/{}/settings/displayed-attributes", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn get_accept_new_fields(&mut self) -> (Value, StatusCode) { pub async fn get_accept_new_fields(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/accept-new-fields", self.uid); let url = format!("/indexes/{}/settings/accept-new-fields", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_accept_new_fields(&mut self, body: Value) { pub async fn update_accept_new_fields(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/accept-new-fields", self.uid); let url = format!("/indexes/{}/settings/accept-new-fields", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn get_synonyms(&mut self) -> (Value, StatusCode) { pub async fn get_synonyms(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/synonyms", self.uid); let url = format!("/indexes/{}/settings/synonyms", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_synonyms(&mut self, body: Value) { pub async fn update_synonyms(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/synonyms", self.uid); let url = format!("/indexes/{}/settings/synonyms", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn delete_synonyms(&mut self) -> (Value, StatusCode) { pub async fn delete_synonyms(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/synonyms", self.uid); let url = format!("/indexes/{}/settings/synonyms", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn get_stop_words(&mut self) -> (Value, StatusCode) { pub async fn get_stop_words(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/stop-words", self.uid); let url = format!("/indexes/{}/settings/stop-words", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn update_stop_words(&mut self, body: Value) { pub async fn update_stop_words(&mut self, body: Value) {
let url = format!("/indexes/{}/settings/stop-words", self.uid); let url = format!("/indexes/{}/settings/stop-words", self.uid);
self.post_request_async(&url, body); self.post_request_async(&url, body).await;
} }
pub fn delete_stop_words(&mut self) -> (Value, StatusCode) { pub async fn delete_stop_words(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings/stop-words", self.uid); let url = format!("/indexes/{}/settings/stop-words", self.uid);
self.delete_request_async(&url) self.delete_request_async(&url).await
} }
pub fn get_index_stats(&mut self) -> (Value, StatusCode) { pub async fn get_index_stats(&mut self) -> (Value, StatusCode) {
let url = format!("/indexes/{}/stats", self.uid); let url = format!("/indexes/{}/stats", self.uid);
self.get_request(&url) self.get_request(&url).await
} }
pub fn list_keys(&mut self) -> (Value, StatusCode) { pub async fn list_keys(&mut self) -> (Value, StatusCode) {
self.get_request("/keys") self.get_request("/keys").await
} }
pub fn get_health(&mut self) -> (Value, StatusCode) { pub async fn get_health(&mut self) -> (Value, StatusCode) {
self.get_request("/health") self.get_request("/health").await
} }
pub fn update_health(&mut self, body: Value) -> (Value, StatusCode) { pub async fn update_health(&mut self, body: Value) -> (Value, StatusCode) {
self.put_request("/health", body) self.put_request("/health", body).await
} }
pub fn get_version(&mut self) -> (Value, StatusCode) { pub async fn get_version(&mut self) -> (Value, StatusCode) {
self.get_request("/version") self.get_request("/version").await
} }
pub fn get_sys_info(&mut self) -> (Value, StatusCode) { pub async fn get_sys_info(&mut self) -> (Value, StatusCode) {
self.get_request("/sys-info") self.get_request("/sys-info").await
} }
pub fn get_sys_info_pretty(&mut self) -> (Value, StatusCode) { pub async fn get_sys_info_pretty(&mut self) -> (Value, StatusCode) {
self.get_request("/sys-info/pretty") self.get_request("/sys-info/pretty").await
} }
// Populate routes // Populate routes
pub fn populate_movies(&mut self) { pub async fn populate_movies(&mut self) {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
self.create_index(body); self.create_index(body).await;
let body = json!({ let body = json!({
"rankingRules": [ "rankingRules": [
@ -436,12 +441,12 @@ impl Server {
"acceptNewFields": false, "acceptNewFields": false,
}); });
self.update_all_settings(body); self.update_all_settings(body).await;
let dataset = include_bytes!("assets/movies.json"); let dataset = include_bytes!("assets/movies.json");
let body: Value = serde_json::from_slice(dataset).unwrap(); let body: Value = serde_json::from_slice(dataset).unwrap();
self.add_or_replace_multiple_documents(body); self.add_or_replace_multiple_documents(body).await;
} }
} }

View File

@ -3,8 +3,8 @@ use serde_json::json;
mod common; mod common;
// Test issue https://github.com/meilisearch/MeiliSearch/issues/519 // Test issue https://github.com/meilisearch/MeiliSearch/issues/519
#[test] #[actix_rt::test]
fn check_add_documents_with_primary_key_param() { async fn check_add_documents_with_primary_key_param() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
@ -12,7 +12,7 @@ fn check_add_documents_with_primary_key_param() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -24,28 +24,28 @@ fn check_add_documents_with_primary_key_param() {
}]); }]);
let url = "/indexes/movies/documents?primaryKey=title"; let url = "/indexes/movies/documents?primaryKey=title";
let (response, status_code) = server.post_request(&url, body); let (response, status_code) = server.post_request(&url, body).await;
eprintln!("{:#?}", response); eprintln!("{:#?}", response);
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
let update_id = response["updateId"].as_u64().unwrap(); let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id); server.wait_update_id(update_id).await;
// 3 - Check update success // 3 - Check update success
let (response, status_code) = server.get_update_status(update_id); let (response, status_code) = server.get_update_status(update_id).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["status"], "processed"); assert_eq!(response["status"], "processed");
} }
// Test issue https://github.com/meilisearch/MeiliSearch/issues/568 // Test issue https://github.com/meilisearch/MeiliSearch/issues/568
#[test] #[actix_rt::test]
fn check_add_documents_with_nested_boolean() { async fn check_add_documents_with_nested_boolean() {
let mut server = common::Server::with_uid("tasks"); let mut server = common::Server::with_uid("tasks");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
let body = json!({ "uid": "tasks" }); let body = json!({ "uid": "tasks" });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -64,28 +64,28 @@ fn check_add_documents_with_nested_boolean() {
}]); }]);
let url = "/indexes/tasks/documents"; let url = "/indexes/tasks/documents";
let (response, status_code) = server.post_request(&url, body); let (response, status_code) = server.post_request(&url, body).await;
eprintln!("{:#?}", response); eprintln!("{:#?}", response);
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
let update_id = response["updateId"].as_u64().unwrap(); let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id); server.wait_update_id(update_id).await;
// 3 - Check update success // 3 - Check update success
let (response, status_code) = server.get_update_status(update_id); let (response, status_code) = server.get_update_status(update_id).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["status"], "processed"); assert_eq!(response["status"], "processed");
} }
// Test issue https://github.com/meilisearch/MeiliSearch/issues/571 // Test issue https://github.com/meilisearch/MeiliSearch/issues/571
#[test] #[actix_rt::test]
fn check_add_documents_with_nested_null() { async fn check_add_documents_with_nested_null() {
let mut server = common::Server::with_uid("tasks"); let mut server = common::Server::with_uid("tasks");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
let body = json!({ "uid": "tasks" }); let body = json!({ "uid": "tasks" });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -99,28 +99,28 @@ fn check_add_documents_with_nested_null() {
}]); }]);
let url = "/indexes/tasks/documents"; let url = "/indexes/tasks/documents";
let (response, status_code) = server.post_request(&url, body); let (response, status_code) = server.post_request(&url, body).await;
eprintln!("{:#?}", response); eprintln!("{:#?}", response);
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
let update_id = response["updateId"].as_u64().unwrap(); let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id); server.wait_update_id(update_id).await;
// 3 - Check update success // 3 - Check update success
let (response, status_code) = server.get_update_status(update_id); let (response, status_code) = server.get_update_status(update_id).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["status"], "processed"); assert_eq!(response["status"], "processed");
} }
// Test issue https://github.com/meilisearch/MeiliSearch/issues/574 // Test issue https://github.com/meilisearch/MeiliSearch/issues/574
#[test] #[actix_rt::test]
fn check_add_documents_with_nested_sequence() { async fn check_add_documents_with_nested_sequence() {
let mut server = common::Server::with_uid("tasks"); let mut server = common::Server::with_uid("tasks");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
let body = json!({ "uid": "tasks" }); let body = json!({ "uid": "tasks" });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -158,20 +158,20 @@ fn check_add_documents_with_nested_sequence() {
}]); }]);
let url = "/indexes/tasks/documents"; let url = "/indexes/tasks/documents";
let (response, status_code) = server.post_request(&url, body.clone()); let (response, status_code) = server.post_request(&url, body.clone()).await;
eprintln!("{:#?}", response); eprintln!("{:#?}", response);
assert_eq!(status_code, 202); assert_eq!(status_code, 202);
let update_id = response["updateId"].as_u64().unwrap(); let update_id = response["updateId"].as_u64().unwrap();
server.wait_update_id(update_id); server.wait_update_id(update_id).await;
// 3 - Check update success // 3 - Check update success
let (response, status_code) = server.get_update_status(update_id); let (response, status_code) = server.get_update_status(update_id).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["status"], "processed"); assert_eq!(response["status"], "processed");
let url = "/indexes/tasks/search?q=leesz"; let url = "/indexes/tasks/search?q=leesz";
let (response, status_code) = server.get_request(&url); let (response, status_code) = server.get_request(&url).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["hits"], body); assert_eq!(response["hits"], body);
} }

View File

@ -1,31 +1,31 @@
mod common; mod common;
#[test] #[actix_rt::test]
fn delete() { async fn delete() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let (_response, status_code) = server.get_document(419704); let (_response, status_code) = server.get_document(419704).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
server.delete_document(419704); server.delete_document(419704).await;
let (_response, status_code) = server.get_document(419704); let (_response, status_code) = server.get_document(419704).await;
assert_eq!(status_code, 404); assert_eq!(status_code, 404);
} }
// Resolve teh issue https://github.com/meilisearch/MeiliSearch/issues/493 // Resolve teh issue https://github.com/meilisearch/MeiliSearch/issues/493
#[test] #[actix_rt::test]
fn delete_batch() { async fn delete_batch() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let (_response, status_code) = server.get_document(419704); let (_response, status_code) = server.get_document(419704).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
let body = serde_json::json!([419704, 512200, 181812]); let body = serde_json::json!([419704, 512200, 181812]);
server.delete_multiple_documents(body); server.delete_multiple_documents(body).await;
let (_response, status_code) = server.get_document(419704); let (_response, status_code) = server.get_document(419704).await;
assert_eq!(status_code, 404); assert_eq!(status_code, 404);
} }

View File

@ -3,36 +3,36 @@ use std::convert::Into;
mod common; mod common;
#[test] #[actix_rt::test]
fn test_healthyness() { async fn test_healthyness() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// Check that the server is healthy // Check that the server is healthy
let (_response, status_code) = server.get_health(); let (_response, status_code) = server.get_health().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
// Set the serve Unhealthy // Set the serve Unhealthy
let body = json!({ let body = json!({
"health": false, "health": false,
}); });
let (_response, status_code) = server.update_health(body); let (_response, status_code) = server.update_health(body).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
// Check that the server is unhealthy // Check that the server is unhealthy
let (_response, status_code) = server.get_health(); let (_response, status_code) = server.get_health().await;
assert_eq!(status_code, 503); assert_eq!(status_code, 503);
// Set the server healthy // Set the server healthy
let body = json!({ let body = json!({
"health": true, "health": true,
}); });
let (_response, status_code) = server.update_health(body); let (_response, status_code) = server.update_health(body).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
// Check if the server is healthy // Check if the server is healthy
let (_response, status_code) = server.get_health(); let (_response, status_code) = server.get_health().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
} }

View File

@ -4,8 +4,8 @@ use serde_json::Value;
mod common; mod common;
#[test] #[actix_rt::test]
fn create_index_with_name() { async fn create_index_with_name() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
@ -14,7 +14,7 @@ fn create_index_with_name() {
"name": "movies", "name": "movies",
}); });
let (res1_value, status_code) = server.create_index(body); let (res1_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -29,7 +29,7 @@ fn create_index_with_name() {
// 2 - Check the list of indexes // 2 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes(); let (res2_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
@ -44,8 +44,8 @@ fn create_index_with_name() {
assert_eq!(r2_updated_at.len(), r1_updated_at.len()); assert_eq!(r2_updated_at.len(), r1_updated_at.len());
} }
#[test] #[actix_rt::test]
fn create_index_with_uid() { async fn create_index_with_uid() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
@ -54,7 +54,7 @@ fn create_index_with_uid() {
"uid": "movies", "uid": "movies",
}); });
let (res1_value, status_code) = server.create_index(body); let (res1_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -69,7 +69,7 @@ fn create_index_with_uid() {
// 2 - Check the list of indexes // 2 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes(); let (res2_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
@ -84,8 +84,8 @@ fn create_index_with_uid() {
assert_eq!(r2_updated_at.len(), r1_updated_at.len()); assert_eq!(r2_updated_at.len(), r1_updated_at.len());
} }
#[test] #[actix_rt::test]
fn create_index_with_name_and_uid() { async fn create_index_with_name_and_uid() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
@ -94,7 +94,7 @@ fn create_index_with_name_and_uid() {
"name": "Films", "name": "Films",
"uid": "fr_movies", "uid": "fr_movies",
}); });
let (res1_value, status_code) = server.create_index(body); let (res1_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -109,7 +109,7 @@ fn create_index_with_name_and_uid() {
// 2 - Check the list of indexes // 2 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes(); let (res2_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
@ -124,8 +124,8 @@ fn create_index_with_name_and_uid() {
assert_eq!(r2_updated_at.len(), r1_updated_at.len()); assert_eq!(r2_updated_at.len(), r1_updated_at.len());
} }
#[test] #[actix_rt::test]
fn rename_index() { async fn rename_index() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
@ -135,7 +135,7 @@ fn rename_index() {
"uid": "movies", "uid": "movies",
}); });
let (res1_value, status_code) = server.create_index(body); let (res1_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -154,7 +154,7 @@ fn rename_index() {
"name": "TV Shows", "name": "TV Shows",
}); });
let (res2_value, status_code) = server.update_index(body); let (res2_value, status_code) = server.update_index(body).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_object().unwrap().len(), 5); assert_eq!(res2_value.as_object().unwrap().len(), 5);
@ -169,7 +169,7 @@ fn rename_index() {
// 3 - Check the list of indexes // 3 - Check the list of indexes
let (res3_value, status_code) = server.list_indexes(); let (res3_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res3_value.as_array().unwrap().len(), 1); assert_eq!(res3_value.as_array().unwrap().len(), 1);
@ -184,8 +184,8 @@ fn rename_index() {
assert_eq!(r3_updated_at.len(), r2_updated_at.len()); assert_eq!(r3_updated_at.len(), r2_updated_at.len());
} }
#[test] #[actix_rt::test]
fn delete_index_and_recreate_it() { async fn delete_index_and_recreate_it() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
@ -195,7 +195,7 @@ fn delete_index_and_recreate_it() {
"uid": "movies", "uid": "movies",
}); });
let (res1_value, status_code) = server.create_index(body); let (res1_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -210,7 +210,7 @@ fn delete_index_and_recreate_it() {
// 2 - Check the list of indexes // 2 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes(); let (res2_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
@ -226,13 +226,13 @@ fn delete_index_and_recreate_it() {
// 3- Delete an index // 3- Delete an index
let (_res2_value, status_code) = server.delete_index(); let (_res2_value, status_code) = server.delete_index().await;
assert_eq!(status_code, 204); assert_eq!(status_code, 204);
// 4 - Check the list of indexes // 4 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes(); let (res2_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 0); assert_eq!(res2_value.as_array().unwrap().len(), 0);
@ -243,7 +243,7 @@ fn delete_index_and_recreate_it() {
"name": "movies", "name": "movies",
}); });
let (res1_value, status_code) = server.create_index(body); let (res1_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -258,7 +258,7 @@ fn delete_index_and_recreate_it() {
// 6 - Check the list of indexes // 6 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes(); let (res2_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5); assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
@ -272,8 +272,8 @@ fn delete_index_and_recreate_it() {
assert_eq!(r2_updated_at.len(), r1_updated_at.len()); assert_eq!(r2_updated_at.len(), r1_updated_at.len());
} }
#[test] #[actix_rt::test]
fn check_multiples_indexes() { async fn check_multiples_indexes() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create a new index // 1 - Create a new index
@ -282,7 +282,7 @@ fn check_multiples_indexes() {
"name": "movies", "name": "movies",
}); });
let (res1_value, status_code) = server.create_index(body); let (res1_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -297,7 +297,7 @@ fn check_multiples_indexes() {
// 2 - Check the list of indexes // 2 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes(); let (res2_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res2_value.as_array().unwrap().len(), 1); assert_eq!(res2_value.as_array().unwrap().len(), 1);
@ -317,7 +317,7 @@ fn check_multiples_indexes() {
"name": "films", "name": "films",
}); });
let (res3_value, status_code) = server.create_index(body); let (res3_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res3_value.as_object().unwrap().len(), 5); assert_eq!(res3_value.as_object().unwrap().len(), 5);
@ -332,7 +332,7 @@ fn check_multiples_indexes() {
// 4 - Check the list of indexes // 4 - Check the list of indexes
let (res4_value, status_code) = server.list_indexes(); let (res4_value, status_code) = server.list_indexes().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(res4_value.as_array().unwrap().len(), 2); assert_eq!(res4_value.as_array().unwrap().len(), 2);
@ -370,15 +370,15 @@ fn check_multiples_indexes() {
} }
} }
#[test] #[actix_rt::test]
fn create_index_failed() { async fn create_index_failed() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 2 - Push index creation with empty json body // 2 - Push index creation with empty json body
let body = json!({}); let body = json!({});
let (res_value, status_code) = server.create_index(body); let (res_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
let message = res_value["message"].as_str().unwrap(); let message = res_value["message"].as_str().unwrap();
@ -392,12 +392,9 @@ fn create_index_failed() {
"active": true "active": true
}); });
let (res_value, status_code) = server.create_index(body); let (_res_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1);
assert_eq!(message, "invalid data");
// 3 - Create a index with wrong data type // 3 - Create a index with wrong data type
@ -406,17 +403,14 @@ fn create_index_failed() {
"uid": 0 "uid": 0
}); });
let (res_value, status_code) = server.create_index(body); let (_res_value, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
let message = res_value["message"].as_str().unwrap();
assert_eq!(res_value.as_object().unwrap().len(), 1);
assert_eq!(message, "invalid data");
} }
// Resolve issue https://github.com/meilisearch/MeiliSearch/issues/492 // Resolve issue https://github.com/meilisearch/MeiliSearch/issues/492
#[test] #[actix_rt::test]
fn create_index_with_primary_key_and_index() { async fn create_index_with_primary_key_and_index() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create the index // 1 - Create the index
@ -426,7 +420,7 @@ fn create_index_with_primary_key_and_index() {
"primaryKey": "id", "primaryKey": "id",
}); });
let (_response, status_code) = server.create_index(body); let (_response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
// 2 - Add content // 2 - Add content
@ -436,11 +430,11 @@ fn create_index_with_primary_key_and_index() {
"text": "The mask" "text": "The mask"
}]); }]);
server.add_or_replace_multiple_documents(body.clone()); server.add_or_replace_multiple_documents(body.clone()).await;
// 3 - Retreive document // 3 - Retreive document
let (response, _status_code) = server.get_document(123); let (response, _status_code) = server.get_document(123).await;
let expect = json!({ let expect = json!({
"id": 123, "id": 123,
@ -454,8 +448,8 @@ fn create_index_with_primary_key_and_index() {
// Test when the given index uid is not valid // Test when the given index uid is not valid
// Should have a 400 status code // Should have a 400 status code
// Should have the right error message // Should have the right error message
#[test] #[actix_rt::test]
fn create_index_with_invalid_uid() { async fn create_index_with_invalid_uid() {
let mut server = common::Server::with_uid(""); let mut server = common::Server::with_uid("");
// 1 - Create the index with invalid uid // 1 - Create the index with invalid uid
@ -464,7 +458,7 @@ fn create_index_with_invalid_uid() {
"uid": "the movies" "uid": "the movies"
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
let message = response["message"].as_str().unwrap(); let message = response["message"].as_str().unwrap();
@ -477,7 +471,7 @@ fn create_index_with_invalid_uid() {
"uid": "%$#" "uid": "%$#"
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
let message = response["message"].as_str().unwrap(); let message = response["message"].as_str().unwrap();
@ -490,7 +484,7 @@ fn create_index_with_invalid_uid() {
"uid": "the~movies" "uid": "the~movies"
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
let message = response["message"].as_str().unwrap(); let message = response["message"].as_str().unwrap();
@ -503,7 +497,7 @@ fn create_index_with_invalid_uid() {
"uid": "🎉" "uid": "🎉"
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
let message = response["message"].as_str().unwrap(); let message = response["message"].as_str().unwrap();
@ -512,8 +506,8 @@ fn create_index_with_invalid_uid() {
} }
// Test that it's possible to add primary_key if it's not already set on index creation // Test that it's possible to add primary_key if it's not already set on index creation
#[test] #[actix_rt::test]
fn create_index_and_add_indentifier_after() { async fn create_index_and_add_indentifier_after() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
@ -521,7 +515,7 @@ fn create_index_and_add_indentifier_after() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -531,21 +525,21 @@ fn create_index_and_add_indentifier_after() {
"primaryKey": "id", "primaryKey": "id",
}); });
let (response, status_code) = server.update_index(body); let (response, status_code) = server.update_index(body).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
eprintln!("response: {:#?}", response); eprintln!("response: {:#?}", response);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
// 3 - Get index to verify if the primary_key is good // 3 - Get index to verify if the primary_key is good
let (response, status_code) = server.get_index(); let (response, status_code) = server.get_index().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
} }
// Test that it's impossible to change the primary_key // Test that it's impossible to change the primary_key
#[test] #[actix_rt::test]
fn create_index_and_update_indentifier_after() { async fn create_index_and_update_indentifier_after() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
@ -554,7 +548,7 @@ fn create_index_and_update_indentifier_after() {
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
@ -564,19 +558,19 @@ fn create_index_and_update_indentifier_after() {
"primaryKey": "skuid", "primaryKey": "skuid",
}); });
let (_response, status_code) = server.update_index(body); let (_response, status_code) = server.update_index(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
// 3 - Get index to verify if the primary_key still the first one // 3 - Get index to verify if the primary_key still the first one
let (response, status_code) = server.get_index(); let (response, status_code) = server.get_index().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
} }
// Test that schema inference work well // Test that schema inference work well
#[test] #[actix_rt::test]
fn create_index_without_primary_key_and_add_document() { async fn create_index_without_primary_key_and_add_document() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
@ -584,7 +578,7 @@ fn create_index_without_primary_key_and_add_document() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -595,18 +589,18 @@ fn create_index_without_primary_key_and_add_document() {
"title": "I'm a legend", "title": "I'm a legend",
}]); }]);
server.add_or_update_multiple_documents(body); server.add_or_update_multiple_documents(body).await;
// 3 - Get index to verify if the primary_key is good // 3 - Get index to verify if the primary_key is good
let (response, status_code) = server.get_index(); let (response, status_code) = server.get_index().await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id"); assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
} }
// Test search with no primary_key // Test search with no primary_key
#[test] #[actix_rt::test]
fn create_index_without_primary_key_and_search() { async fn create_index_without_primary_key_and_search() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
@ -614,7 +608,7 @@ fn create_index_without_primary_key_and_search() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -622,15 +616,15 @@ fn create_index_without_primary_key_and_search() {
let query = "q=captain&limit=3"; let query = "q=captain&limit=3";
let (response, status_code) = server.search(&query); let (response, status_code) = server.search(&query).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_eq!(response["hits"].as_array().unwrap().len(), 0); assert_eq!(response["hits"].as_array().unwrap().len(), 0);
} }
// Test the error message when we push an document update and impossibility to find primary key // Test the error message when we push an document update and impossibility to find primary key
// Test issue https://github.com/meilisearch/MeiliSearch/issues/517 // Test issue https://github.com/meilisearch/MeiliSearch/issues/517
#[test] #[actix_rt::test]
fn check_add_documents_without_primary_key() { async fn check_add_documents_without_primary_key() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key // 1 - Create the index with no primary_key
@ -638,7 +632,7 @@ fn check_add_documents_without_primary_key() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -649,7 +643,7 @@ fn check_add_documents_without_primary_key() {
"comment": "comment test" "comment": "comment test"
}]); }]);
let (response, status_code) = server.add_or_replace_multiple_documents_sync(body); let (response, status_code) = server.add_or_replace_multiple_documents_sync(body).await;
let expected = json!({ let expected = json!({
"message": "Could not infer a primary key" "message": "Could not infer a primary key"
@ -659,8 +653,8 @@ fn check_add_documents_without_primary_key() {
assert_json_eq!(response, expected, ordered: false); assert_json_eq!(response, expected, ordered: false);
} }
#[test] #[actix_rt::test]
fn check_first_update_should_bring_up_processed_status_after_first_docs_addition(){ async fn check_first_update_should_bring_up_processed_status_after_first_docs_addition() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
@ -668,7 +662,7 @@ fn check_first_update_should_bring_up_processed_status_after_first_docs_addition
}); });
// 1. Create Index // 1. Create Index
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -677,10 +671,10 @@ fn check_first_update_should_bring_up_processed_status_after_first_docs_addition
let body: Value = serde_json::from_slice(dataset).unwrap(); let body: Value = serde_json::from_slice(dataset).unwrap();
// 2. Index the documents from movies.json, present inside of assets directory // 2. Index the documents from movies.json, present inside of assets directory
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 3. Fetch the status of the indexing done above. // 3. Fetch the status of the indexing done above.
let (response, status_code) = server.get_all_updates_status(); let (response, status_code) = server.get_all_updates_status().await;
// 4. Verify the fetch is successful and indexing status is 'processed' // 4. Verify the fetch is successful and indexing status is 'processed'
assert_eq!(status_code, 200); assert_eq!(status_code, 200);

View File

@ -1,23 +1,18 @@
use std::convert::Into; use std::convert::Into;
use std::sync::Mutex;
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use once_cell::sync::Lazy;
use serde_json::json; use serde_json::json;
mod common; mod common;
static GLOBAL_SERVER: Lazy<Mutex<common::Server>> = Lazy::new(|| {
let mut server = common::Server::with_uid("movies");
server.populate_movies();
Mutex::new(server)
});
// Search // Search
// q: Captain // q: Captain
// limit: 3 // limit: 3
#[test] #[actix_rt::test]
fn search_with_limit() { async fn search_with_limit() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=3"; let query = "q=captain&limit=3";
let expected = json!([ let expected = json!([
@ -74,7 +69,7 @@ fn search_with_limit() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -82,8 +77,11 @@ fn search_with_limit() {
// q: Captain // q: Captain
// limit: 3 // limit: 3
// offset: 1 // offset: 1
#[test] #[actix_rt::test]
fn search_with_offset() { async fn search_with_offset() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=3&offset=1"; let query = "q=captain&limit=3&offset=1";
let expected = json!([ let expected = json!([
@ -141,7 +139,7 @@ fn search_with_offset() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -149,8 +147,11 @@ fn search_with_offset() {
// q: Captain // q: Captain
// limit: 1 // limit: 1
// attributeToHighlight: * // attributeToHighlight: *
#[test] #[actix_rt::test]
fn search_with_attribute_to_highlight_wildcard() { async fn search_with_attribute_to_highlight_wildcard() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToHighlight=*"; let query = "q=captain&limit=1&attributesToHighlight=*";
let expected = json!([ let expected = json!([
@ -190,7 +191,7 @@ fn search_with_attribute_to_highlight_wildcard() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -198,8 +199,11 @@ fn search_with_attribute_to_highlight_wildcard() {
// q: Captain // q: Captain
// limit: 1 // limit: 1
// attributeToHighlight: title // attributeToHighlight: title
#[test] #[actix_rt::test]
fn search_with_attribute_to_highlight_1() { async fn search_with_attribute_to_highlight_1() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToHighlight=title"; let query = "q=captain&limit=1&attributesToHighlight=title";
let expected = json!([ let expected = json!([
@ -239,7 +243,7 @@ fn search_with_attribute_to_highlight_1() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -247,8 +251,11 @@ fn search_with_attribute_to_highlight_1() {
// q: Captain // q: Captain
// limit: 1 // limit: 1
// attributeToHighlight: title,tagline // attributeToHighlight: title,tagline
#[test] #[actix_rt::test]
fn search_with_attribute_to_highlight_title_tagline() { async fn search_with_attribute_to_highlight_title_tagline() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToHighlight=title,tagline"; let query = "q=captain&limit=1&attributesToHighlight=title,tagline";
let expected = json!([ let expected = json!([
@ -288,7 +295,7 @@ fn search_with_attribute_to_highlight_title_tagline() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -296,8 +303,11 @@ fn search_with_attribute_to_highlight_title_tagline() {
// q: Captain // q: Captain
// limit: 1 // limit: 1
// attributeToHighlight: title,overview // attributeToHighlight: title,overview
#[test] #[actix_rt::test]
fn search_with_attribute_to_highlight_title_overview() { async fn search_with_attribute_to_highlight_title_overview() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToHighlight=title,overview"; let query = "q=captain&limit=1&attributesToHighlight=title,overview";
let expected = json!([ let expected = json!([
@ -337,7 +347,7 @@ fn search_with_attribute_to_highlight_title_overview() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -345,8 +355,11 @@ fn search_with_attribute_to_highlight_title_overview() {
// q: Captain // q: Captain
// limit: 1 // limit: 1
// matches: true // matches: true
#[test] #[actix_rt::test]
fn search_with_matches() { async fn search_with_matches() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&matches=true"; let query = "q=captain&limit=1&matches=true";
let expected = json!([ let expected = json!([
@ -383,7 +396,7 @@ fn search_with_matches() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -392,8 +405,11 @@ fn search_with_matches() {
// limit: 1 // limit: 1
// attributesToCrop: overview // attributesToCrop: overview
// cropLength: 20 // cropLength: 20
#[test] #[actix_rt::test]
fn search_witch_crop() { async fn search_witch_crop() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20"; let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20";
let expected = json!([ let expected = json!([
@ -433,7 +449,7 @@ fn search_witch_crop() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -441,8 +457,11 @@ fn search_witch_crop() {
// q: Captain // q: Captain
// limit: 1 // limit: 1
// attributesToRetrieve: [title,tagline,overview,poster_path] // attributesToRetrieve: [title,tagline,overview,poster_path]
#[test] #[actix_rt::test]
fn search_with_attributes_to_retrieve() { async fn search_with_attributes_to_retrieve() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,tagline,overview,poster_path"; let query = "q=captain&limit=1&attributesToRetrieve=title,tagline,overview,poster_path";
let expected = json!([ let expected = json!([
@ -454,7 +473,7 @@ fn search_with_attributes_to_retrieve() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -462,8 +481,11 @@ fn search_with_attributes_to_retrieve() {
// q: Captain // q: Captain
// limit: 1 // limit: 1
// attributesToRetrieve: * // attributesToRetrieve: *
#[test] #[actix_rt::test]
fn search_with_attributes_to_retrieve_wildcard() { async fn search_with_attributes_to_retrieve_wildcard() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=*"; let query = "q=captain&limit=1&attributesToRetrieve=*";
let expected = json!([ let expected = json!([
@ -486,7 +508,7 @@ fn search_with_attributes_to_retrieve_wildcard() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -494,8 +516,11 @@ fn search_with_attributes_to_retrieve_wildcard() {
// q: Captain // q: Captain
// limit: 3 // limit: 3
// filters: director:Anthony%20Russo // filters: director:Anthony%20Russo
#[test] #[actix_rt::test]
fn search_with_filter() { async fn search_with_filter() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&filters=director%20%3D%20%22Anthony%20Russo%22&limit=3"; let query = "q=captain&filters=director%20%3D%20%22Anthony%20Russo%22&limit=3";
let expected = json!([ let expected = json!([
{ {
@ -550,7 +575,7 @@ fn search_with_filter() {
"vote_count": 10497 "vote_count": 10497
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
let expected = json!([ let expected = json!([
@ -574,7 +599,7 @@ fn search_with_filter() {
// filters: title = "american pie 2" // filters: title = "american pie 2"
let query = "q=american&filters=title%20%3D%20%22american%20pie%202%22"; let query = "q=american&filters=title%20%3D%20%22american%20pie%202%22";
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
let expected = json!([ let expected = json!([
@ -615,7 +640,7 @@ fn search_with_filter() {
]); ]);
// limit: 3, director = "anthony russo" AND (title = "captain america: civil war" OR title = "Captain America: The Winter Soldier") // limit: 3, director = "anthony russo" AND (title = "captain america: civil war" OR title = "Captain America: The Winter Soldier")
let query = "q=a&limit=3&filters=director%20%3D%20%22anthony%20russo%22%20AND%20%20(title%20%3D%20%22captain%20america%3A%20civil%20war%22%20OR%20title%20%3D%20%22Captain%20America%3A%20The%20Winter%20Soldier%22)"; let query = "q=a&limit=3&filters=director%20%3D%20%22anthony%20russo%22%20AND%20%20(title%20%3D%20%22captain%20america%3A%20civil%20war%22%20OR%20title%20%3D%20%22Captain%20America%3A%20The%20Winter%20Soldier%22)";
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
let expected = json!([ let expected = json!([
@ -673,7 +698,7 @@ fn search_with_filter() {
]); ]);
// director = "anthony russo" AND (title = "captain america: civil war" OR vote_average > 8.0) // director = "anthony russo" AND (title = "captain america: civil war" OR vote_average > 8.0)
let query = "q=a&limit=3&filters=director%20%3D%20%22anthony%20russo%22%20AND%20%20(title%20%3D%20%22captain%20america%3A%20civil%20war%22%20OR%20vote_average%20%3E%208.0)"; let query = "q=a&limit=3&filters=director%20%3D%20%22anthony%20russo%22%20AND%20%20(title%20%3D%20%22captain%20america%3A%20civil%20war%22%20OR%20vote_average%20%3E%208.0)";
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
let expected = json!([ let expected = json!([
@ -730,12 +755,12 @@ fn search_with_filter() {
]); ]);
// NOT director = "anthony russo" AND vote_average > 7.5 // NOT director = "anthony russo" AND vote_average > 7.5
let query = "q=a&limit=3&filters=NOT%20director%20%3D%20%22anthony%20russo%22%20AND%20vote_average%20%3E%207.5"; let query = "q=a&limit=3&filters=NOT%20director%20%3D%20%22anthony%20russo%22%20AND%20vote_average%20%3E%207.5";
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
let expected = json!([]); let expected = json!([]);
let query = "q=a&filters=NOT%20director%20%3D%20%22anthony%20russo%22%20AND%20title%20%20%3D%20%22Avengers%3A%20Endgame%22"; let query = "q=a&filters=NOT%20director%20%3D%20%22anthony%20russo%22%20AND%20title%20%20%3D%20%22Avengers%3A%20Endgame%22";
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -744,8 +769,11 @@ fn search_with_filter() {
// limit: 1 // limit: 1
// attributesToHighlight: [title,overview] // attributesToHighlight: [title,overview]
// matches: true // matches: true
#[test] #[actix_rt::test]
fn search_with_attributes_to_highlight_and_matches() { async fn search_with_attributes_to_highlight_and_matches() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToHighlight=title,overview&matches=true"; let query = "q=captain&limit=1&attributesToHighlight=title,overview&matches=true";
let expected = json!( [ let expected = json!( [
@ -799,7 +827,7 @@ fn search_with_attributes_to_highlight_and_matches() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -810,8 +838,11 @@ fn search_with_attributes_to_highlight_and_matches() {
// matches: true // matches: true
// cropLength: 20 // cropLength: 20
// attributesToCrop: overview // attributesToCrop: overview
#[test] #[actix_rt::test]
fn search_with_attributes_to_highlight_and_matches_and_crop() { async fn search_with_attributes_to_highlight_and_matches_and_crop() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20&attributesToHighlight=title,overview&matches=true"; let query = "q=captain&limit=1&attributesToCrop=overview&cropLength=20&attributesToHighlight=title,overview&matches=true";
let expected = json!([ let expected = json!([
@ -865,7 +896,7 @@ fn search_with_attributes_to_highlight_and_matches_and_crop() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -874,8 +905,11 @@ fn search_with_attributes_to_highlight_and_matches_and_crop() {
// limit: 1 // limit: 1
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToHighlight: [title] // attributesToHighlight: [title]
#[test] #[actix_rt::test]
fn search_with_differents_attributes() { async fn search_with_differents_attributes() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToHighlight=title"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToHighlight=title";
let expected = json!([ let expected = json!([
@ -889,7 +923,7 @@ fn search_with_differents_attributes() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -899,8 +933,11 @@ fn search_with_differents_attributes() {
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToCrop: [overview] // attributesToCrop: [overview]
// cropLength: 10 // cropLength: 10
#[test] #[actix_rt::test]
fn search_with_differents_attributes_2() { async fn search_with_differents_attributes_2() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=overview&cropLength=10"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=overview&cropLength=10";
let expected = json!([ let expected = json!([
@ -914,7 +951,7 @@ fn search_with_differents_attributes_2() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -923,8 +960,11 @@ fn search_with_differents_attributes_2() {
// limit: 1 // limit: 1
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToCrop: [overview:10] // attributesToCrop: [overview:10]
#[test] #[actix_rt::test]
fn search_with_differents_attributes_3() { async fn search_with_differents_attributes_3() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=overview:10"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=overview:10";
let expected = json!([ let expected = json!([
@ -938,7 +978,7 @@ fn search_with_differents_attributes_3() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -947,8 +987,11 @@ fn search_with_differents_attributes_3() {
// limit: 1 // limit: 1
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToCrop: [overview:10,title:0] // attributesToCrop: [overview:10,title:0]
#[test] #[actix_rt::test]
fn search_with_differents_attributes_4() { async fn search_with_differents_attributes_4() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=overview:10,title:0"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=overview:10,title:0";
let expected = json!([ let expected = json!([
@ -963,7 +1006,7 @@ fn search_with_differents_attributes_4() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -972,8 +1015,11 @@ fn search_with_differents_attributes_4() {
// limit: 1 // limit: 1
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToCrop: [*,overview:10] // attributesToCrop: [*,overview:10]
#[test] #[actix_rt::test]
fn search_with_differents_attributes_5() { async fn search_with_differents_attributes_5() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10";
let expected = json!([ let expected = json!([
@ -990,7 +1036,7 @@ fn search_with_differents_attributes_5() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -1000,8 +1046,11 @@ fn search_with_differents_attributes_5() {
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToCrop: [*,overview:10] // attributesToCrop: [*,overview:10]
// attributesToHighlight: [title] // attributesToHighlight: [title]
#[test] #[actix_rt::test]
fn search_with_differents_attributes_6() { async fn search_with_differents_attributes_6() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10&attributesToHighlight=title"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10&attributesToHighlight=title";
let expected = json!([ let expected = json!([
@ -1018,7 +1067,7 @@ fn search_with_differents_attributes_6() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -1028,8 +1077,11 @@ fn search_with_differents_attributes_6() {
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToCrop: [*,overview:10] // attributesToCrop: [*,overview:10]
// attributesToHighlight: [*] // attributesToHighlight: [*]
#[test] #[actix_rt::test]
fn search_with_differents_attributes_7() { async fn search_with_differents_attributes_7() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10&attributesToHighlight=*"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10&attributesToHighlight=*";
let expected = json!([ let expected = json!([
@ -1046,7 +1098,7 @@ fn search_with_differents_attributes_7() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }
@ -1056,8 +1108,11 @@ fn search_with_differents_attributes_7() {
// attributesToRetrieve: [title,producer,director] // attributesToRetrieve: [title,producer,director]
// attributesToCrop: [*,overview:10] // attributesToCrop: [*,overview:10]
// attributesToHighlight: [*,tagline] // attributesToHighlight: [*,tagline]
#[test] #[actix_rt::test]
fn search_with_differents_attributes_8() { async fn search_with_differents_attributes_8() {
let mut server = common::Server::with_uid("movies");
server.populate_movies().await;
let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10&attributesToHighlight=*,tagline"; let query = "q=captain&limit=1&attributesToRetrieve=title,producer,director&attributesToCrop=*,overview:10&attributesToHighlight=*,tagline";
let expected = json!([ let expected = json!([
@ -1075,6 +1130,6 @@ fn search_with_differents_attributes_8() {
} }
]); ]);
let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expected, response["hits"].clone(), ordered: false); assert_json_eq!(expected, response["hits"].clone(), ordered: false);
} }

View File

@ -4,10 +4,10 @@ use std::convert::Into;
mod common; mod common;
#[test] #[actix_rt::test]
fn search_with_settings_basic() { async fn search_with_settings_basic() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -49,7 +49,7 @@ fn search_with_settings_basic() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(config); server.update_all_settings(config).await;
let query = "q=the%20avangers&limit=3"; let query = "q=the%20avangers&limit=3";
let expect = json!([ let expect = json!([
@ -106,14 +106,14 @@ fn search_with_settings_basic() {
} }
]); ]);
let (response, _status_code) = server.search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[actix_rt::test]
fn search_with_settings_stop_words() { async fn search_with_settings_stop_words() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -155,7 +155,7 @@ fn search_with_settings_stop_words() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(config); server.update_all_settings(config).await;
let query = "q=the%20avangers&limit=3"; let query = "q=the%20avangers&limit=3";
let expect = json!([ let expect = json!([
@ -212,14 +212,14 @@ fn search_with_settings_stop_words() {
} }
]); ]);
let (response, _status_code) = server.search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[actix_rt::test]
fn search_with_settings_synonyms() { async fn search_with_settings_synonyms() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -266,7 +266,7 @@ fn search_with_settings_synonyms() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(config); server.update_all_settings(config).await;
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let expect = json!([ let expect = json!([
@ -323,14 +323,14 @@ fn search_with_settings_synonyms() {
} }
]); ]);
let (response, _status_code) = server.search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[actix_rt::test]
fn search_with_settings_ranking_rules() { async fn search_with_settings_ranking_rules() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -372,7 +372,7 @@ fn search_with_settings_ranking_rules() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(config); server.update_all_settings(config).await;
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let expect = json!([ let expect = json!([
@ -429,14 +429,14 @@ fn search_with_settings_ranking_rules() {
} }
]); ]);
let (response, _status_code) = server.search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[actix_rt::test]
fn search_with_settings_searchable_attributes() { async fn search_with_settings_searchable_attributes() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -477,7 +477,7 @@ fn search_with_settings_searchable_attributes() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(config); server.update_all_settings(config).await;
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let expect = json!([ let expect = json!([
@ -534,14 +534,14 @@ fn search_with_settings_searchable_attributes() {
} }
]); ]);
let (response, _status_code) = server.search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[actix_rt::test]
fn search_with_settings_displayed_attributes() { async fn search_with_settings_displayed_attributes() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -577,7 +577,7 @@ fn search_with_settings_displayed_attributes() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(config); server.update_all_settings(config).await;
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let expect = json!([ let expect = json!([
@ -604,14 +604,14 @@ fn search_with_settings_displayed_attributes() {
} }
]); ]);
let (response, _status_code) = server.search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }
#[test] #[actix_rt::test]
fn search_with_settings_searchable_attributes_2() { async fn search_with_settings_searchable_attributes_2() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
let config = json!({ let config = json!({
"rankingRules": [ "rankingRules": [
@ -647,7 +647,7 @@ fn search_with_settings_searchable_attributes_2() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(config); server.update_all_settings(config).await;
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let expect = json!([ let expect = json!([
@ -674,6 +674,6 @@ fn search_with_settings_searchable_attributes_2() {
} }
]); ]);
let (response, _status_code) = server.search(query); let (response, _status_code) = server.search(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); assert_json_eq!(expect, response["hits"].clone(), ordered: false);
} }

View File

@ -4,10 +4,10 @@ use std::convert::Into;
mod common; mod common;
#[test] #[actix_rt::test]
fn write_all_and_delete() { async fn write_all_and_delete() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
// 2 - Send the settings // 2 - Send the settings
@ -51,21 +51,21 @@ fn write_all_and_delete() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(body.clone()); server.update_all_settings(body.clone()).await;
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
assert_json_eq!(body, response, ordered: false); assert_json_eq!(body, response, ordered: false);
// 4 - Delete all settings // 4 - Delete all settings
server.delete_all_settings(); server.delete_all_settings().await;
// 5 - Get all settings and check if they are set to default values // 5 - Get all settings and check if they are set to default values
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
let expect = json!({ let expect = json!({
"rankingRules": [ "rankingRules": [
@ -125,10 +125,10 @@ fn write_all_and_delete() {
assert_json_eq!(expect, response, ordered: false); assert_json_eq!(expect, response, ordered: false);
} }
#[test] #[actix_rt::test]
fn write_all_and_update() { async fn write_all_and_update() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
// 2 - Send the settings // 2 - Send the settings
@ -172,11 +172,11 @@ fn write_all_and_update() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(body.clone()); server.update_all_settings(body.clone()).await;
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
assert_json_eq!(body, response, ordered: false); assert_json_eq!(body, response, ordered: false);
@ -213,11 +213,11 @@ fn write_all_and_update() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(body); server.update_all_settings(body).await;
// 5 - Get all settings and check if the content is the same of (4) // 5 - Get all settings and check if the content is the same of (4)
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
let expected = json!({ let expected = json!({
"rankingRules": [ "rankingRules": [
@ -253,13 +253,13 @@ fn write_all_and_update() {
assert_json_eq!(expected, response, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }
#[test] #[actix_rt::test]
fn test_default_settings() { async fn test_default_settings() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Get all settings and compare to the previous one // 1 - Get all settings and compare to the previous one
@ -280,19 +280,19 @@ fn test_default_settings() {
"acceptNewFields": true, "acceptNewFields": true,
}); });
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
assert_json_eq!(body, response, ordered: false); assert_json_eq!(body, response, ordered: false);
} }
#[test] #[actix_rt::test]
fn test_default_settings_2() { async fn test_default_settings_2() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Get all settings and compare to the previous one // 1 - Get all settings and compare to the previous one
@ -317,19 +317,19 @@ fn test_default_settings_2() {
"acceptNewFields": true, "acceptNewFields": true,
}); });
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
assert_json_eq!(body, response, ordered: false); assert_json_eq!(body, response, ordered: false);
} }
// Test issue https://github.com/meilisearch/MeiliSearch/issues/516 // Test issue https://github.com/meilisearch/MeiliSearch/issues/516
#[test] #[actix_rt::test]
fn write_setting_and_update_partial() { async fn write_setting_and_update_partial() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
server.create_index(body); server.create_index(body).await;
// 2 - Send the settings // 2 - Send the settings
@ -352,7 +352,7 @@ fn write_setting_and_update_partial() {
] ]
}); });
server.update_all_settings(body.clone()); server.update_all_settings(body.clone()).await;
// 2 - Send the settings // 2 - Send the settings
@ -380,7 +380,7 @@ fn write_setting_and_update_partial() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(body.clone()); server.update_all_settings(body.clone()).await;
// 2 - Send the settings // 2 - Send the settings
@ -424,7 +424,7 @@ fn write_setting_and_update_partial() {
"acceptNewFields": false, "acceptNewFields": false,
}); });
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
assert_json_eq!(expected, response, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }

View File

@ -3,14 +3,14 @@ use serde_json::json;
mod common; mod common;
#[test] #[actix_rt::test]
fn index_new_fields_default() { async fn index_new_fields_default() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Add a document // 1 - Add a document
@ -19,7 +19,7 @@ fn index_new_fields_default() {
"title": "I'm a legend", "title": "I'm a legend",
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 2 - Get the complete document // 2 - Get the complete document
@ -28,7 +28,7 @@ fn index_new_fields_default() {
"title": "I'm a legend", "title": "I'm a legend",
}); });
let (response, status_code) = server.get_document(1); let (response, status_code) = server.get_document(1).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
@ -40,7 +40,7 @@ fn index_new_fields_default() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 4 - Get the complete document // 4 - Get the complete document
@ -50,23 +50,23 @@ fn index_new_fields_default() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}); });
let (response, status_code) = server.get_document(2); let (response, status_code) = server.get_document(2).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
} }
#[test] #[actix_rt::test]
fn index_new_fields_true() { async fn index_new_fields_true() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Set indexNewFields = true // 1 - Set indexNewFields = true
server.update_accept_new_fields(json!(true)); server.update_accept_new_fields(json!(true)).await;
// 2 - Add a document // 2 - Add a document
@ -75,7 +75,7 @@ fn index_new_fields_true() {
"title": "I'm a legend", "title": "I'm a legend",
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 3 - Get the complete document // 3 - Get the complete document
@ -84,7 +84,7 @@ fn index_new_fields_true() {
"title": "I'm a legend", "title": "I'm a legend",
}); });
let (response, status_code) = server.get_document(1); let (response, status_code) = server.get_document(1).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
@ -96,7 +96,7 @@ fn index_new_fields_true() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 5 - Get the complete document // 5 - Get the complete document
@ -106,23 +106,23 @@ fn index_new_fields_true() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}); });
let (response, status_code) = server.get_document(2); let (response, status_code) = server.get_document(2).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
} }
#[test] #[actix_rt::test]
fn index_new_fields_false() { async fn index_new_fields_false() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Set indexNewFields = false // 1 - Set indexNewFields = false
server.update_accept_new_fields(json!(false)); server.update_accept_new_fields(json!(false)).await;
// 2 - Add a document // 2 - Add a document
@ -131,7 +131,7 @@ fn index_new_fields_false() {
"title": "I'm a legend", "title": "I'm a legend",
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 3 - Get the complete document // 3 - Get the complete document
@ -139,7 +139,7 @@ fn index_new_fields_false() {
"id": 1, "id": 1,
}); });
let (response, status_code) = server.get_document(1); let (response, status_code) = server.get_document(1).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
@ -151,7 +151,7 @@ fn index_new_fields_false() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 5 - Get the complete document // 5 - Get the complete document
@ -159,23 +159,23 @@ fn index_new_fields_false() {
"id": 2, "id": 2,
}); });
let (response, status_code) = server.get_document(2); let (response, status_code) = server.get_document(2).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
} }
#[test] #[actix_rt::test]
fn index_new_fields_true_then_false() { async fn index_new_fields_true_then_false() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Set indexNewFields = true // 1 - Set indexNewFields = true
server.update_accept_new_fields(json!(true)); server.update_accept_new_fields(json!(true)).await;
// 2 - Add a document // 2 - Add a document
@ -184,7 +184,7 @@ fn index_new_fields_true_then_false() {
"title": "I'm a legend", "title": "I'm a legend",
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 3 - Get the complete document // 3 - Get the complete document
@ -193,13 +193,13 @@ fn index_new_fields_true_then_false() {
"title": "I'm a legend", "title": "I'm a legend",
}); });
let (response, status_code) = server.get_document(1); let (response, status_code) = server.get_document(1).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
// 4 - Set indexNewFields = false // 4 - Set indexNewFields = false
server.update_accept_new_fields(json!(false)); server.update_accept_new_fields(json!(false)).await;
// 5 - Add a document with more fields // 5 - Add a document with more fields
@ -209,7 +209,7 @@ fn index_new_fields_true_then_false() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 6 - Get the complete document // 6 - Get the complete document
@ -218,23 +218,23 @@ fn index_new_fields_true_then_false() {
"title": "I'm not a legend", "title": "I'm not a legend",
}); });
let (response, status_code) = server.get_document(2); let (response, status_code) = server.get_document(2).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
} }
#[test] #[actix_rt::test]
fn index_new_fields_false_then_true() { async fn index_new_fields_false_then_true() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Set indexNewFields = false // 1 - Set indexNewFields = false
server.update_accept_new_fields(json!(false)); server.update_accept_new_fields(json!(false)).await;
// 2 - Add a document // 2 - Add a document
@ -243,7 +243,7 @@ fn index_new_fields_false_then_true() {
"title": "I'm a legend", "title": "I'm a legend",
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 3 - Get the complete document // 3 - Get the complete document
@ -251,13 +251,13 @@ fn index_new_fields_false_then_true() {
"id": 1, "id": 1,
}); });
let (response, status_code) = server.get_document(1); let (response, status_code) = server.get_document(1).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
// 4 - Set indexNewFields = false // 4 - Set indexNewFields = false
server.update_accept_new_fields(json!(true)); server.update_accept_new_fields(json!(true)).await;
// 5 - Add a document with more fields // 5 - Add a document with more fields
@ -267,7 +267,7 @@ fn index_new_fields_false_then_true() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 6 - Get the complete document // 6 - Get the complete document
@ -275,7 +275,7 @@ fn index_new_fields_false_then_true() {
"id": 1, "id": 1,
}); });
let (response, status_code) = server.get_document(1); let (response, status_code) = server.get_document(1).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
@ -284,15 +284,14 @@ fn index_new_fields_false_then_true() {
"description": "A bad copy of the original movie I'm a lengend" "description": "A bad copy of the original movie I'm a lengend"
}); });
let (response, status_code) = server.get_document(2); let (response, status_code) = server.get_document(2).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected); assert_json_eq!(response, expected);
} }
// Fix issue https://github.com/meilisearch/MeiliSearch/issues/518 // Fix issue https://github.com/meilisearch/MeiliSearch/issues/518
#[test] #[actix_rt::test]
fn accept_new_fields_does_not_take_into_account_the_primary_key () { async fn accept_new_fields_does_not_take_into_account_the_primary_key() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
// 1 - Create an index with no primary-key // 1 - Create an index with no primary-key
@ -300,7 +299,7 @@ fn accept_new_fields_does_not_take_into_account_the_primary_key () {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
}); });
let (response, status_code) = server.create_index(body); let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null)); assert_eq!(response["primaryKey"], json!(null));
@ -312,7 +311,7 @@ fn accept_new_fields_does_not_take_into_account_the_primary_key () {
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(body); server.update_all_settings(body).await;
// 4 - Add a document // 4 - Add a document
@ -322,11 +321,11 @@ fn accept_new_fields_does_not_take_into_account_the_primary_key () {
"comment": "comment test" "comment": "comment test"
}]); }]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 5 - Get settings, they should not changed // 5 - Get settings, they should not changed
let (response, _status_code) = server.get_all_settings(); let (response, _status_code) = server.get_all_settings().await;
let expected = json!({ let expected = json!({
"rankingRules": [ "rankingRules": [

View File

@ -3,10 +3,10 @@ use serde_json::json;
mod common; mod common;
#[test] #[actix_rt::test]
fn write_all_and_delete() { async fn write_all_and_delete() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
// 2 - Send the settings // 2 - Send the settings
@ -21,21 +21,21 @@ fn write_all_and_delete() {
"desc(rank)", "desc(rank)",
]); ]);
server.update_ranking_rules(body.clone()); server.update_ranking_rules(body.clone()).await;
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let (response, _status_code) = server.get_ranking_rules(); let (response, _status_code) = server.get_ranking_rules().await;
assert_json_eq!(body, response, ordered: false); assert_json_eq!(body, response, ordered: false);
// 4 - Delete all settings // 4 - Delete all settings
server.delete_ranking_rules(); server.delete_ranking_rules().await;
// 5 - Get all settings and check if they are empty // 5 - Get all settings and check if they are empty
let (response, _status_code) = server.get_ranking_rules(); let (response, _status_code) = server.get_ranking_rules().await;
let expected = json!([ let expected = json!([
"typo", "typo",
@ -49,10 +49,10 @@ fn write_all_and_delete() {
assert_json_eq!(expected, response, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }
#[test] #[actix_rt::test]
fn write_all_and_update() { async fn write_all_and_update() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
// 2 - Send the settings // 2 - Send the settings
@ -67,11 +67,11 @@ fn write_all_and_update() {
"desc(rank)", "desc(rank)",
]); ]);
server.update_ranking_rules(body.clone()); server.update_ranking_rules(body.clone()).await;
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
let (response, _status_code) = server.get_ranking_rules(); let (response, _status_code) = server.get_ranking_rules().await;
assert_json_eq!(body, response, ordered: false); assert_json_eq!(body, response, ordered: false);
@ -87,11 +87,11 @@ fn write_all_and_update() {
"desc(release_date)", "desc(release_date)",
]); ]);
server.update_ranking_rules(body); server.update_ranking_rules(body).await;
// 5 - Get all settings and check if the content is the same of (4) // 5 - Get all settings and check if the content is the same of (4)
let (response, _status_code) = server.get_ranking_rules(); let (response, _status_code) = server.get_ranking_rules().await;
let expected = json!([ let expected = json!([
"typo", "typo",
@ -106,54 +106,51 @@ fn write_all_and_update() {
assert_json_eq!(expected, response, ordered: false); assert_json_eq!(expected, response, ordered: false);
} }
#[test] #[actix_rt::test]
fn send_undefined_rule() { async fn send_undefined_rule() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
let body = json!(["typos",]); let body = json!(["typos",]);
let (_response, status_code) = server.update_ranking_rules_sync(body); let (_response, status_code) = server.update_ranking_rules_sync(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
} }
#[test] #[actix_rt::test]
fn send_malformed_custom_rule() { async fn send_malformed_custom_rule() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
let body = json!(["dsc(truc)",]); let body = json!(["dsc(truc)",]);
let (_response, status_code) = server.update_ranking_rules_sync(body); let (_response, status_code) = server.update_ranking_rules_sync(body).await;
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
} }
// Test issue https://github.com/meilisearch/MeiliSearch/issues/521 // Test issue https://github.com/meilisearch/MeiliSearch/issues/521
#[test] #[actix_rt::test]
fn write_custom_ranking_and_index_documents() { async fn write_custom_ranking_and_index_documents() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"primaryKey": "id", "primaryKey": "id",
}); });
server.create_index(body); server.create_index(body).await;
// 1 - Add ranking rules with one custom ranking on a string // 1 - Add ranking rules with one custom ranking on a string
let body = json!([ let body = json!(["asc(title)", "typo"]);
"asc(title)",
"typo"
]);
server.update_ranking_rules(body); server.update_ranking_rules(body).await;
// 2 - Add documents // 2 - Add documents
@ -170,7 +167,7 @@ fn write_custom_ranking_and_index_documents() {
} }
]); ]);
server.add_or_replace_multiple_documents(body); server.add_or_replace_multiple_documents(body).await;
// 3 - Get the first document and compare // 3 - Get the first document and compare
@ -180,9 +177,8 @@ fn write_custom_ranking_and_index_documents() {
"author": "Exupéry" "author": "Exupéry"
}); });
let (response, status_code) = server.get_document(1); let (response, status_code) = server.get_document(1).await;
assert_eq!(status_code, 200); assert_eq!(status_code, 200);
assert_json_eq!(response, expected, ordered: false); assert_json_eq!(response, expected, ordered: false);
} }

View File

@ -3,54 +3,54 @@ use serde_json::json;
mod common; mod common;
#[test] #[actix_rt::test]
fn update_stop_words() { async fn update_stop_words() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
// 1 - Get stop words // 1 - Get stop words
let (response, _status_code) = server.get_stop_words(); let (response, _status_code) = server.get_stop_words().await;
assert_eq!(response.as_array().unwrap().is_empty(), true); assert_eq!(response.as_array().unwrap().is_empty(), true);
// 2 - Update stop words // 2 - Update stop words
let body = json!(["the", "a"]); let body = json!(["the", "a"]);
server.update_stop_words(body.clone()); server.update_stop_words(body.clone()).await;
// 3 - Get all stop words and compare to the previous one // 3 - Get all stop words and compare to the previous one
let (response, _status_code) = server.get_stop_words(); let (response, _status_code) = server.get_stop_words().await;
assert_json_eq!(body, response, ordered: false); assert_json_eq!(body, response, ordered: false);
// 4 - Delete all stop words // 4 - Delete all stop words
server.delete_stop_words(); server.delete_stop_words().await;
// 5 - Get all stop words and check if they are empty // 5 - Get all stop words and check if they are empty
let (response, _status_code) = server.get_stop_words(); let (response, _status_code) = server.get_stop_words().await;
assert_eq!(response.as_array().unwrap().is_empty(), true); assert_eq!(response.as_array().unwrap().is_empty(), true);
} }
#[test] #[actix_rt::test]
fn add_documents_and_stop_words() { async fn add_documents_and_stop_words() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies(); server.populate_movies().await;
// 2 - Update stop words // 2 - Update stop words
let body = json!(["the", "of"]); let body = json!(["the", "of"]);
server.update_stop_words(body.clone()); server.update_stop_words(body.clone()).await;
// 3 - Search for a document with stop words // 3 - Search for a document with stop words
let (response, _status_code) = server.search("q=the%20mask"); let (response, _status_code) = server.search("q=the%20mask").await;
assert!(!response["hits"].as_array().unwrap().is_empty()); assert!(!response["hits"].as_array().unwrap().is_empty());
// 4 - Search for documents with *only* stop words // 4 - Search for documents with *only* stop words
let (response, _status_code) = server.search("q=the%20of"); let (response, _status_code) = server.search("q=the%20of").await;
assert!(response["hits"].as_array().unwrap().is_empty()); assert!(response["hits"].as_array().unwrap().is_empty());
// 5 - Delete all stop words // 5 - Delete all stop words