2021-09-15 00:39:02 +08:00
|
|
|
|
use actix_web::error::PayloadError;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
use actix_web::{web, HttpResponse};
|
2021-09-15 00:39:02 +08:00
|
|
|
|
use actix_web::web::Bytes;
|
|
|
|
|
use futures::{Stream, StreamExt};
|
2021-06-24 21:02:35 +08:00
|
|
|
|
use log::debug;
|
2021-09-21 19:23:22 +08:00
|
|
|
|
use meilisearch_lib::MeiliSearch;
|
|
|
|
|
use meilisearch_lib::index_controller::{DocumentAdditionFormat, Update};
|
2021-09-15 00:39:02 +08:00
|
|
|
|
use milli::update::IndexDocumentsMethod;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
use serde::Deserialize;
|
2021-09-15 00:39:02 +08:00
|
|
|
|
//use serde_json::Value;
|
|
|
|
|
use tokio::sync::mpsc;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
2020-12-22 21:02:41 +08:00
|
|
|
|
use crate::error::ResponseError;
|
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;
|
2020-12-22 21:02:41 +08:00
|
|
|
|
use crate::routes::IndexParam;
|
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-06-29 17:57:47 +08:00
|
|
|
|
/*
|
2020-12-23 23:12:37 +08:00
|
|
|
|
macro_rules! guard_content_type {
|
|
|
|
|
($fn_name:ident, $guard_value:literal) => {
|
|
|
|
|
fn $fn_name(head: &actix_web::dev::RequestHead) -> bool {
|
|
|
|
|
if let Some(content_type) = head.headers.get("Content-Type") {
|
2021-03-16 01:11:10 +08:00
|
|
|
|
content_type
|
|
|
|
|
.to_str()
|
|
|
|
|
.map(|v| v.contains($guard_value))
|
|
|
|
|
.unwrap_or(false)
|
2020-12-23 23:12:37 +08:00
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard_content_type!(guard_json, "application/json");
|
2021-06-29 17:57:47 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
|
/// This is required because Payload is not Sync nor Send
|
|
|
|
|
fn payload_to_stream(mut payload: Payload) -> impl Stream<Item=Result<Bytes, PayloadError>> {
|
|
|
|
|
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-06-29 17:57:47 +08:00
|
|
|
|
fn guard_json(head: &actix_web::dev::RequestHead) -> bool {
|
2021-06-30 23:05:59 +08:00
|
|
|
|
if let Some(_content_type) = head.headers.get("Content-Type") {
|
|
|
|
|
// CURRENTLY AND FOR THIS RELEASE ONLY WE DECIDED TO INTERPRET ALL CONTENT-TYPES AS JSON
|
|
|
|
|
true
|
|
|
|
|
/*
|
2021-06-29 21:25:18 +08:00
|
|
|
|
content_type
|
|
|
|
|
.to_str()
|
|
|
|
|
.map(|v| v.contains("application/json"))
|
|
|
|
|
.unwrap_or(false)
|
2021-06-30 23:05:59 +08:00
|
|
|
|
*/
|
2021-06-29 21:25:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
// if no content-type is specified we still accept the data as json!
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-23 23:12:37 +08:00
|
|
|
|
|
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))
|
|
|
|
|
.route(web::post().guard(guard_json).to(add_documents))
|
|
|
|
|
.route(web::put().guard(guard_json).to(update_documents))
|
2021-09-15 00:39:02 +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-15 00:39:02 +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-15 00:39:02 +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-09-24 18:03:16 +08:00
|
|
|
|
meilisearch: GuardedData<Public, 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-15 00:39:02 +08:00
|
|
|
|
//pub async fn delete_document(
|
2021-09-21 19:23:22 +08:00
|
|
|
|
//data: GuardedData<Private, MeiliSearch>,
|
2021-09-15 00:39:02 +08:00
|
|
|
|
//path: web::Path<DocumentParam>,
|
|
|
|
|
//) -> Result<HttpResponse, ResponseError> {
|
|
|
|
|
//let update_status = data
|
|
|
|
|
//.delete_documents(path.index_uid.clone(), vec![path.document_id.clone()])
|
|
|
|
|
//.await?;
|
|
|
|
|
//debug!("returns: {:?}", update_status);
|
|
|
|
|
//Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
|
|
|
|
//}
|
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-09-24 18:03:16 +08:00
|
|
|
|
meilisearch: GuardedData<Public, MeiliSearch>,
|
2021-02-11 00:08:37 +08:00
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
|
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-03-16 01:11:10 +08:00
|
|
|
|
path.index_uid.clone(),
|
|
|
|
|
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-02-13 19:22:59 +08:00
|
|
|
|
primary_key: Option<String>,
|
2020-12-12 20:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 23:12:37 +08:00
|
|
|
|
/// Route used when the payload type is "application/json"
|
2021-03-17 20:54:17 +08:00
|
|
|
|
/// Used to add or replace documents
|
2021-07-07 22:20:22 +08:00
|
|
|
|
pub async fn add_documents(
|
2021-09-24 18:03:16 +08:00
|
|
|
|
meilisearch: GuardedData<Private, MeiliSearch>,
|
2020-12-23 23:12:37 +08:00
|
|
|
|
path: web::Path<IndexParam>,
|
2021-02-13 19:22:59 +08:00
|
|
|
|
params: web::Query<UpdateDocumentsQuery>,
|
2021-06-23 19:55:16 +08:00
|
|
|
|
body: Payload,
|
2020-12-23 23:12:37 +08:00
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-23 18:18:34 +08:00
|
|
|
|
debug!("called with params: {:?}", params);
|
2021-09-15 00:39:02 +08:00
|
|
|
|
let update = Update::DocumentAddition {
|
|
|
|
|
payload: Box::new(payload_to_stream(body)),
|
|
|
|
|
primary_key: params.primary_key.clone(),
|
|
|
|
|
method: IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
|
format: DocumentAdditionFormat::Json,
|
|
|
|
|
};
|
2021-09-24 18:03:16 +08:00
|
|
|
|
let update_status = meilisearch
|
2021-09-24 20:55:57 +08:00
|
|
|
|
.register_update(path.into_inner().index_uid, update)
|
2021-06-15 22:22:06 +08:00
|
|
|
|
.await?;
|
2021-02-27 00:14:11 +08:00
|
|
|
|
|
2021-06-23 18:18:34 +08:00
|
|
|
|
debug!("returns: {:?}", update_status);
|
2021-06-15 22:22:06 +08:00
|
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
2020-12-23 23:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 17:57:47 +08:00
|
|
|
|
/// Route used when the payload type is "application/json"
|
|
|
|
|
/// Used to add or replace documents
|
2021-07-07 22:20:22 +08:00
|
|
|
|
pub async fn update_documents(
|
2021-09-24 18:03:16 +08:00
|
|
|
|
meilisearch: GuardedData<Private, MeiliSearch>,
|
2020-12-12 20:32:06 +08:00
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
|
params: web::Query<UpdateDocumentsQuery>,
|
2021-06-23 19:55:16 +08:00
|
|
|
|
body: Payload,
|
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-09-15 00:39:02 +08:00
|
|
|
|
let update = Update::DocumentAddition {
|
|
|
|
|
payload: Box::new(payload_to_stream(body)),
|
|
|
|
|
primary_key: params.primary_key.clone(),
|
|
|
|
|
method: IndexDocumentsMethod::UpdateDocuments,
|
|
|
|
|
format: DocumentAdditionFormat::Json,
|
|
|
|
|
};
|
2021-09-24 18:03:16 +08:00
|
|
|
|
let update_status = meilisearch
|
2021-09-24 20:55:57 +08:00
|
|
|
|
.register_update(path.into_inner().index_uid, update)
|
2021-06-15 22:22:06 +08:00
|
|
|
|
.await?;
|
2021-03-04 22:10:58 +08:00
|
|
|
|
|
2021-06-23 18:18:34 +08:00
|
|
|
|
debug!("returns: {:?}", update_status);
|
2021-06-15 22:22:06 +08:00
|
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
2020-12-12 20:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
|
//pub async fn delete_documents(
|
2021-09-21 19:23:22 +08:00
|
|
|
|
//data: GuardedData<Private, MeiliSearch>,
|
2021-09-15 00:39:02 +08:00
|
|
|
|
//path: web::Path<IndexParam>,
|
|
|
|
|
//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_status = data.delete_documents(path.index_uid.clone(), ids).await?;
|
|
|
|
|
//debug!("returns: {:?}", update_status);
|
|
|
|
|
//Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//pub async fn clear_all_documents(
|
2021-09-21 19:23:22 +08:00
|
|
|
|
//data: GuardedData<Private, MeiliSearch>,
|
2021-09-15 00:39:02 +08:00
|
|
|
|
//path: web::Path<IndexParam>,
|
|
|
|
|
//) -> Result<HttpResponse, ResponseError> {
|
|
|
|
|
//let update_status = data.clear_documents(path.index_uid.clone()).await?;
|
|
|
|
|
//debug!("returns: {:?}", update_status);
|
|
|
|
|
//Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
|
|
|
|
//}
|