2021-09-15 00:39:02 +08:00
|
|
|
use actix_web::error::PayloadError;
|
2021-12-02 23:03:26 +08:00
|
|
|
use actix_web::http::header::CONTENT_TYPE;
|
2021-09-15 00:39:02 +08:00
|
|
|
use actix_web::web::Bytes;
|
2021-12-02 23:03:26 +08:00
|
|
|
use actix_web::HttpMessage;
|
2021-09-30 17:17:42 +08:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2021-12-02 23:03:26 +08:00
|
|
|
use bstr::ByteSlice;
|
2021-09-15 00:39:02 +08:00
|
|
|
use futures::{Stream, StreamExt};
|
2021-06-24 21:02:35 +08:00
|
|
|
use log::debug;
|
2021-12-02 23:03:26 +08:00
|
|
|
use meilisearch_error::ResponseError;
|
2021-09-21 19:23:22 +08:00
|
|
|
use meilisearch_lib::index_controller::{DocumentAdditionFormat, Update};
|
2021-09-29 04:08:03 +08:00
|
|
|
use meilisearch_lib::milli::update::IndexDocumentsMethod;
|
2021-09-29 04:22:59 +08:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2021-12-02 23:03:26 +08:00
|
|
|
use mime::Mime;
|
2021-10-06 17:49:34 +08:00
|
|
|
use once_cell::sync::Lazy;
|
2020-12-12 20:32:06 +08:00
|
|
|
use serde::Deserialize;
|
2021-10-26 01:28:30 +08:00
|
|
|
use serde_json::Value;
|
2021-09-15 00:39:02 +08:00
|
|
|
use tokio::sync::mpsc;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-10-12 21:23:31 +08:00
|
|
|
use crate::analytics::Analytics;
|
2021-12-02 23:03:26 +08:00
|
|
|
use crate::error::MeilisearchHttpError;
|
2021-06-24 21:02:35 +08:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2021-06-23 20:56:02 +08:00
|
|
|
use crate::extractors::payload::Payload;
|
2021-12-02 23:03:26 +08:00
|
|
|
use crate::task::SummarizedTaskView;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-02-11 00:08:37 +08:00
|
|
|
const DEFAULT_RETRIEVE_DOCUMENTS_OFFSET: usize = 0;
|
|
|
|
const DEFAULT_RETRIEVE_DOCUMENTS_LIMIT: usize = 20;
|
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
static ACCEPTED_CONTENT_TYPE: Lazy<Vec<String>> = Lazy::new(|| {
|
|
|
|
vec![
|
|
|
|
"application/json".to_string(),
|
|
|
|
"application/x-ndjson".to_string(),
|
|
|
|
"text/csv".to_string(),
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
/// This is required because Payload is not Sync nor Send
|
2021-09-29 04:22:59 +08:00
|
|
|
fn payload_to_stream(mut payload: Payload) -> impl Stream<Item = Result<Bytes, PayloadError>> {
|
2021-09-15 00:39:02 +08:00
|
|
|
let (snd, recv) = mpsc::channel(1);
|
|
|
|
tokio::task::spawn_local(async move {
|
|
|
|
while let Some(data) = payload.next().await {
|
|
|
|
let _ = snd.send(data).await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tokio_stream::wrappers::ReceiverStream::new(recv)
|
|
|
|
}
|
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
/// Extracts the mime type from the content type and return
|
|
|
|
/// a meilisearch error if anyhthing bad happen.
|
|
|
|
fn extract_mime_type(req: &HttpRequest) -> Result<Option<Mime>, MeilisearchHttpError> {
|
|
|
|
match req.mime_type() {
|
|
|
|
Ok(Some(mime)) => Ok(Some(mime)),
|
|
|
|
Ok(None) => Ok(None),
|
|
|
|
Err(_) => match req.headers().get(CONTENT_TYPE) {
|
|
|
|
Some(content_type) => Err(MeilisearchHttpError::InvalidContentType(
|
|
|
|
content_type.as_bytes().as_bstr().to_string(),
|
|
|
|
ACCEPTED_CONTENT_TYPE.clone(),
|
|
|
|
)),
|
|
|
|
None => Err(MeilisearchHttpError::MissingContentType(
|
|
|
|
ACCEPTED_CONTENT_TYPE.clone(),
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
#[derive(Deserialize)]
|
2021-07-07 22:20:22 +08:00
|
|
|
pub struct DocumentParam {
|
2021-02-13 17:44:20 +08:00
|
|
|
index_uid: String,
|
|
|
|
document_id: String,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-07-05 20:29:20 +08:00
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2021-06-24 21:02:35 +08:00
|
|
|
cfg.service(
|
2021-07-05 20:29:20 +08:00
|
|
|
web::resource("")
|
|
|
|
.route(web::get().to(get_all_documents))
|
2021-09-30 17:17:42 +08:00
|
|
|
.route(web::post().to(add_documents))
|
|
|
|
.route(web::put().to(update_documents))
|
2021-09-24 21:21:07 +08:00
|
|
|
.route(web::delete().to(clear_all_documents)),
|
2021-07-05 20:29:20 +08:00
|
|
|
)
|
|
|
|
// this route needs to be before the /documents/{document_id} to match properly
|
2021-09-24 21:21:07 +08:00
|
|
|
.service(web::resource("/delete-batch").route(web::post().to(delete_documents)))
|
2021-07-05 20:29:20 +08:00
|
|
|
.service(
|
|
|
|
web::resource("/{document_id}")
|
|
|
|
.route(web::get().to(get_document))
|
2021-09-24 21:21:07 +08:00
|
|
|
.route(web::delete().to(delete_document)),
|
2021-06-24 21:02:35 +08:00
|
|
|
);
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub async fn get_document(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_GET }>, MeiliSearch>,
|
2021-02-11 17:59:23 +08:00
|
|
|
path: web::Path<DocumentParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-04 22:09:00 +08:00
|
|
|
let index = path.index_uid.clone();
|
|
|
|
let id = path.document_id.clone();
|
2021-09-24 18:03:16 +08:00
|
|
|
let document = meilisearch
|
2021-09-21 19:23:22 +08:00
|
|
|
.document(index, id, None as Option<Vec<String>>)
|
2021-06-15 22:22:06 +08:00
|
|
|
.await?;
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", document);
|
2021-06-15 22:22:06 +08:00
|
|
|
Ok(HttpResponse::Ok().json(document))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:21:07 +08:00
|
|
|
pub async fn delete_document(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_DELETE }>, MeiliSearch>,
|
2021-09-24 21:21:07 +08:00
|
|
|
path: web::Path<DocumentParam>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-09-29 04:22:59 +08:00
|
|
|
let DocumentParam {
|
|
|
|
document_id,
|
|
|
|
index_uid,
|
|
|
|
} = path.into_inner();
|
2021-09-24 21:21:07 +08:00
|
|
|
let update = Update::DeleteDocuments(vec![document_id]);
|
2021-12-02 23:03:26 +08:00
|
|
|
let task: SummarizedTaskView = meilisearch.register_update(index_uid, update).await?.into();
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 21:21:07 +08:00
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-06-23 18:18:34 +08:00
|
|
|
#[derive(Deserialize, Debug)]
|
2020-12-12 20:32:06 +08:00
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2021-07-07 22:20:22 +08:00
|
|
|
pub struct BrowseQuery {
|
2021-02-11 00:08:37 +08:00
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
|
|
|
attributes_to_retrieve: Option<String>,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 22:20:22 +08:00
|
|
|
pub async fn get_all_documents(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_GET }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-02-11 00:08:37 +08:00
|
|
|
params: web::Query<BrowseQuery>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("called with params: {:?}", params);
|
2021-04-22 16:14:29 +08:00
|
|
|
let attributes_to_retrieve = params.attributes_to_retrieve.as_ref().and_then(|attrs| {
|
2021-04-17 23:33:36 +08:00
|
|
|
let mut names = Vec::new();
|
|
|
|
for name in attrs.split(',').map(String::from) {
|
|
|
|
if name == "*" {
|
2021-04-22 16:14:29 +08:00
|
|
|
return None;
|
2021-04-17 23:33:36 +08:00
|
|
|
}
|
|
|
|
names.push(name);
|
|
|
|
}
|
|
|
|
Some(names)
|
|
|
|
});
|
2021-02-11 00:08:37 +08:00
|
|
|
|
2021-09-24 18:03:16 +08:00
|
|
|
let documents = meilisearch
|
2021-09-21 19:23:22 +08:00
|
|
|
.documents(
|
2021-12-02 23:03:26 +08:00
|
|
|
path.into_inner(),
|
2021-03-16 01:11:10 +08:00
|
|
|
params.offset.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_OFFSET),
|
|
|
|
params.limit.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_LIMIT),
|
|
|
|
attributes_to_retrieve,
|
|
|
|
)
|
2021-06-15 22:22:06 +08:00
|
|
|
.await?;
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", documents);
|
2021-06-15 22:22:06 +08:00
|
|
|
Ok(HttpResponse::Ok().json(documents))
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-23 18:18:34 +08:00
|
|
|
#[derive(Deserialize, Debug)]
|
2020-12-12 20:32:06 +08:00
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
2021-07-07 22:20:22 +08:00
|
|
|
pub struct UpdateDocumentsQuery {
|
2021-10-25 22:41:23 +08:00
|
|
|
pub primary_key: Option<String>,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-30 17:17:42 +08:00
|
|
|
pub async fn add_documents(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_ADD }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-09-29 06:12:25 +08:00
|
|
|
params: web::Query<UpdateDocumentsQuery>,
|
|
|
|
body: Payload,
|
2021-09-30 17:17:42 +08:00
|
|
|
req: HttpRequest,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2021-09-29 06:12:25 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-09-30 17:17:42 +08:00
|
|
|
debug!("called with params: {:?}", params);
|
2021-10-12 21:23:31 +08:00
|
|
|
let params = params.into_inner();
|
2021-12-02 23:03:26 +08:00
|
|
|
let index_uid = path.into_inner();
|
2021-10-12 21:23:31 +08:00
|
|
|
|
2021-10-26 01:28:30 +08:00
|
|
|
analytics.add_documents(
|
|
|
|
¶ms,
|
2021-12-02 23:03:26 +08:00
|
|
|
meilisearch.get_index(index_uid.clone()).await.is_err(),
|
2021-10-26 01:28:30 +08:00
|
|
|
&req,
|
2021-10-12 21:23:31 +08:00
|
|
|
);
|
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
let task = document_addition(
|
|
|
|
extract_mime_type(&req)?,
|
2021-09-29 16:17:52 +08:00
|
|
|
meilisearch,
|
2021-12-02 23:03:26 +08:00
|
|
|
index_uid,
|
2021-10-12 21:23:31 +08:00
|
|
|
params.primary_key,
|
2021-09-29 16:17:52 +08:00
|
|
|
body,
|
2021-09-30 17:29:27 +08:00
|
|
|
IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
)
|
2021-12-02 23:03:26 +08:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-29 16:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-09-30 17:17:42 +08:00
|
|
|
pub async fn update_documents(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_ADD }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-09-29 16:17:52 +08:00
|
|
|
params: web::Query<UpdateDocumentsQuery>,
|
|
|
|
body: Payload,
|
2021-09-30 17:17:42 +08:00
|
|
|
req: HttpRequest,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2021-09-29 16:17:52 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-09-30 17:17:42 +08:00
|
|
|
debug!("called with params: {:?}", params);
|
2021-12-02 23:03:26 +08:00
|
|
|
let index_uid = path.into_inner();
|
2021-10-12 21:31:59 +08:00
|
|
|
|
2021-10-26 01:28:30 +08:00
|
|
|
analytics.update_documents(
|
|
|
|
¶ms,
|
2021-12-02 23:03:26 +08:00
|
|
|
meilisearch.get_index(index_uid.clone()).await.is_err(),
|
2021-10-26 01:28:30 +08:00
|
|
|
&req,
|
2021-10-12 21:31:59 +08:00
|
|
|
);
|
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
let task = document_addition(
|
|
|
|
extract_mime_type(&req)?,
|
2021-09-29 16:17:52 +08:00
|
|
|
meilisearch,
|
2021-12-02 23:03:26 +08:00
|
|
|
index_uid,
|
2021-09-30 17:17:42 +08:00
|
|
|
params.into_inner().primary_key,
|
2021-09-29 16:17:52 +08:00
|
|
|
body,
|
2021-09-30 17:29:27 +08:00
|
|
|
IndexDocumentsMethod::UpdateDocuments,
|
|
|
|
)
|
2021-12-02 23:03:26 +08:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-29 06:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn document_addition(
|
2021-12-02 23:03:26 +08:00
|
|
|
mime_type: Option<Mime>,
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_ADD }>, MeiliSearch>,
|
2021-09-30 17:17:42 +08:00
|
|
|
index_uid: String,
|
|
|
|
primary_key: Option<String>,
|
2021-06-23 19:55:16 +08:00
|
|
|
body: Payload,
|
2021-09-29 06:12:25 +08:00
|
|
|
method: IndexDocumentsMethod,
|
2021-12-02 23:03:26 +08:00
|
|
|
) -> Result<SummarizedTaskView, ResponseError> {
|
|
|
|
let format = match mime_type
|
|
|
|
.as_ref()
|
|
|
|
.map(|m| (m.type_().as_str(), m.subtype().as_str()))
|
|
|
|
{
|
|
|
|
Some(("application", "json")) => DocumentAdditionFormat::Json,
|
|
|
|
Some(("application", "x-ndjson")) => DocumentAdditionFormat::Ndjson,
|
|
|
|
Some(("text", "csv")) => DocumentAdditionFormat::Csv,
|
|
|
|
Some((type_, subtype)) => {
|
2021-10-05 19:30:53 +08:00
|
|
|
return Err(MeilisearchHttpError::InvalidContentType(
|
2021-12-02 23:03:26 +08:00
|
|
|
format!("{}/{}", type_, subtype),
|
2021-10-06 17:49:34 +08:00
|
|
|
ACCEPTED_CONTENT_TYPE.clone(),
|
2021-10-05 19:30:53 +08:00
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
None => {
|
2021-10-06 17:49:34 +08:00
|
|
|
return Err(
|
|
|
|
MeilisearchHttpError::MissingContentType(ACCEPTED_CONTENT_TYPE.clone()).into(),
|
|
|
|
)
|
2021-09-30 17:29:27 +08:00
|
|
|
}
|
2021-09-30 17:17:42 +08:00
|
|
|
};
|
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
let update = Update::DocumentAddition {
|
|
|
|
payload: Box::new(payload_to_stream(body)),
|
2021-09-30 17:17:42 +08:00
|
|
|
primary_key,
|
2021-09-29 06:12:25 +08:00
|
|
|
method,
|
|
|
|
format,
|
2021-09-15 00:39:02 +08:00
|
|
|
};
|
2021-09-30 17:17:42 +08:00
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
let task = meilisearch.register_update(index_uid, update).await?.into();
|
2021-03-04 22:10:58 +08:00
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(task)
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:21:07 +08:00
|
|
|
pub async fn delete_documents(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_DELETE }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-09-24 21:21:07 +08:00
|
|
|
body: web::Json<Vec<Value>>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
debug!("called with params: {:?}", body);
|
|
|
|
let ids = body
|
|
|
|
.iter()
|
|
|
|
.map(|v| {
|
|
|
|
v.as_str()
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| v.to_string())
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let update = Update::DeleteDocuments(ids);
|
2021-12-02 23:03:26 +08:00
|
|
|
let task: SummarizedTaskView = meilisearch
|
|
|
|
.register_update(path.into_inner(), update)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 21:21:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn clear_all_documents(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_DELETE }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
path: web::Path<String>,
|
2021-09-24 21:21:07 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let update = Update::ClearDocuments;
|
2021-12-02 23:03:26 +08:00
|
|
|
let task: SummarizedTaskView = meilisearch
|
|
|
|
.register_update(path.into_inner(), update)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 21:21:07 +08:00
|
|
|
}
|