MeiliSearch/meilisearch-http/src/routes/document.rs

218 lines
6.9 KiB
Rust
Raw Normal View History

2020-12-12 13:32:06 +01:00
use actix_web::{web, HttpResponse};
2021-06-24 15:02:35 +02:00
use log::debug;
2021-02-26 17:14:11 +01:00
use milli::update::{IndexDocumentsMethod, UpdateFormat};
2020-12-12 13:32:06 +01:00
use serde::Deserialize;
2020-12-23 16:12:37 +01:00
use serde_json::Value;
2020-12-12 13:32:06 +01:00
2020-12-22 14:02:41 +01:00
use crate::error::ResponseError;
2021-06-24 15:02:35 +02:00
use crate::extractors::authentication::{policies::*, GuardedData};
2021-06-23 14:56:02 +02:00
use crate::extractors::payload::Payload;
2020-12-22 14:02:41 +01:00
use crate::routes::IndexParam;
2021-06-23 14:56:02 +02:00
use crate::Data;
2020-12-12 13:32:06 +01:00
2021-02-10 17:08:37 +01:00
const DEFAULT_RETRIEVE_DOCUMENTS_OFFSET: usize = 0;
const DEFAULT_RETRIEVE_DOCUMENTS_LIMIT: usize = 20;
2021-06-29 11:57:47 +02:00
/*
2020-12-23 16:12:37 +01: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-15 18:11:10 +01:00
content_type
.to_str()
.map(|v| v.contains($guard_value))
.unwrap_or(false)
2020-12-23 16:12:37 +01:00
} else {
false
}
}
};
}
guard_content_type!(guard_json, "application/json");
2021-06-29 11:57:47 +02:00
*/
fn guard_json(head: &actix_web::dev::RequestHead) -> bool {
2021-06-30 17:05:59 +02: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
/*
content_type
.to_str()
.map(|v| v.contains("application/json"))
.unwrap_or(false)
2021-06-30 17:05:59 +02:00
*/
} else {
// if no content-type is specified we still accept the data as json!
true
}
}
2020-12-23 16:12:37 +01:00
2020-12-12 13:32:06 +01:00
#[derive(Deserialize)]
struct DocumentParam {
2021-02-13 10:44:20 +01:00
index_uid: String,
document_id: String,
2020-12-12 13:32:06 +01:00
}
pub fn services(cfg: &mut web::ServiceConfig) {
2021-06-24 15:02:35 +02:00
cfg.service(
2021-06-24 19:02:28 +02:00
web::scope("/indexes/{index_uid}/documents")
.service(
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))
.route(web::delete().to(clear_all_documents)),
)
// this route needs to be before the /documents/{document_id} to match properly
.service(web::resource("/delete-batch").route(web::post().to(delete_documents)))
.service(
web::resource("/{document_id}")
.route(web::get().to(get_document))
.route(web::delete().to(delete_document)),
),
2021-06-24 15:02:35 +02:00
);
2020-12-12 13:32:06 +01:00
}
async fn get_document(
2021-06-24 15:02:35 +02:00
data: GuardedData<Public, Data>,
2021-02-11 10:59:23 +01:00
path: web::Path<DocumentParam>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-03-04 15:09:00 +01:00
let index = path.index_uid.clone();
let id = path.document_id.clone();
let document = data
2021-03-15 18:11:10 +01:00
.retrieve_document(index, id, None as Option<Vec<String>>)
.await?;
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", document);
Ok(HttpResponse::Ok().json(document))
2020-12-12 13:32:06 +01:00
}
async fn delete_document(
2021-06-24 15:02:35 +02:00
data: GuardedData<Private, Data>,
2021-02-13 10:44:20 +01:00
path: web::Path<DocumentParam>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
let update_status = data
2021-03-15 18:11:10 +01:00
.delete_documents(path.index_uid.clone(), vec![path.document_id.clone()])
.await?;
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", update_status);
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
2020-12-12 13:32:06 +01:00
}
2021-06-23 12:18:34 +02:00
#[derive(Deserialize, Debug)]
2020-12-12 13:32:06 +01:00
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BrowseQuery {
2021-02-10 17:08:37 +01:00
offset: Option<usize>,
limit: Option<usize>,
attributes_to_retrieve: Option<String>,
2020-12-12 13:32:06 +01:00
}
async fn get_all_documents(
2021-06-24 15:02:35 +02:00
data: GuardedData<Public, Data>,
2021-02-10 17:08:37 +01:00
path: web::Path<IndexParam>,
params: web::Query<BrowseQuery>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 12:18:34 +02:00
debug!("called with params: {:?}", params);
2021-04-22 10:14:29 +02:00
let attributes_to_retrieve = params.attributes_to_retrieve.as_ref().and_then(|attrs| {
2021-04-17 17:33:36 +02:00
let mut names = Vec::new();
for name in attrs.split(',').map(String::from) {
if name == "*" {
2021-04-22 10:14:29 +02:00
return None;
2021-04-17 17:33:36 +02:00
}
names.push(name);
}
Some(names)
});
2021-02-10 17:08:37 +01:00
let documents = data
2021-03-15 18:11:10 +01:00
.retrieve_documents(
path.index_uid.clone(),
params.offset.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_OFFSET),
params.limit.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_LIMIT),
attributes_to_retrieve,
)
.await?;
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", documents);
Ok(HttpResponse::Ok().json(documents))
2020-12-12 13:32:06 +01:00
}
2021-06-23 12:18:34 +02:00
#[derive(Deserialize, Debug)]
2020-12-12 13:32:06 +01:00
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateDocumentsQuery {
2021-02-13 12:22:59 +01:00
primary_key: Option<String>,
2020-12-12 13:32:06 +01:00
}
2020-12-23 16:12:37 +01:00
/// Route used when the payload type is "application/json"
/// Used to add or replace documents
2021-03-16 12:04:32 +01:00
async fn add_documents(
2021-06-24 15:02:35 +02:00
data: GuardedData<Private, Data>,
2020-12-23 16:12:37 +01:00
path: web::Path<IndexParam>,
2021-02-13 12:22:59 +01:00
params: web::Query<UpdateDocumentsQuery>,
2021-06-23 13:55:16 +02:00
body: Payload,
2020-12-23 16:12:37 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 12:18:34 +02:00
debug!("called with params: {:?}", params);
let update_status = data
2021-02-26 17:14:11 +01:00
.add_documents(
path.into_inner().index_uid,
IndexDocumentsMethod::ReplaceDocuments,
UpdateFormat::Json,
body,
params.primary_key.clone(),
2021-03-15 18:11:10 +01:00
)
.await?;
2021-02-26 17:14:11 +01:00
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", update_status);
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
2020-12-23 16:12:37 +01:00
}
2021-06-29 11:57:47 +02:00
/// Route used when the payload type is "application/json"
/// Used to add or replace documents
2020-12-12 13:32:06 +01:00
async fn update_documents(
2021-06-24 15:02:35 +02:00
data: GuardedData<Private, Data>,
2020-12-12 13:32:06 +01:00
path: web::Path<IndexParam>,
params: web::Query<UpdateDocumentsQuery>,
2021-06-23 13:55:16 +02:00
body: Payload,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 12:18:34 +02:00
debug!("called with params: {:?}", params);
let update = data
2021-03-04 15:10:58 +01:00
.add_documents(
path.into_inner().index_uid,
IndexDocumentsMethod::UpdateDocuments,
UpdateFormat::Json,
body,
params.primary_key.clone(),
2021-03-15 18:11:10 +01:00
)
.await?;
2021-03-04 15:10:58 +01:00
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", update);
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update.id() })))
2020-12-12 13:32:06 +01:00
}
async fn delete_documents(
2021-06-24 15:02:35 +02:00
data: GuardedData<Private, Data>,
2021-02-12 17:39:14 +01:00
path: web::Path<IndexParam>,
body: web::Json<Vec<Value>>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 12:18:34 +02:00
debug!("called with params: {:?}", body);
2021-03-04 15:59:18 +01:00
let ids = body
.iter()
2021-03-15 18:11:10 +01:00
.map(|v| {
v.as_str()
.map(String::from)
.unwrap_or_else(|| v.to_string())
})
2021-03-04 15:59:18 +01:00
.collect();
2021-02-12 17:39:14 +01:00
let update_status = data.delete_documents(path.index_uid.clone(), ids).await?;
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", update_status);
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
2020-12-12 13:32:06 +01:00
}
async fn clear_all_documents(
2021-06-24 15:02:35 +02:00
data: GuardedData<Private, Data>,
2021-02-11 12:03:00 +01:00
path: web::Path<IndexParam>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
let update_status = data.clear_documents(path.index_uid.clone()).await?;
2021-06-23 12:18:34 +02:00
debug!("returns: {:?}", update_status);
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
2020-12-12 13:32:06 +01:00
}