MeiliSearch/meilisearch/src/routes/indexes/mod.rs

375 lines
12 KiB
Rust
Raw Normal View History

use std::convert::Infallible;
use std::num::ParseIntError;
2022-09-27 16:33:37 +02:00
use actix_web::web::Data;
2021-10-13 20:56:28 +02:00
use actix_web::{web, HttpRequest, HttpResponse};
use deserr::{
DeserializeError, DeserializeFromValue, ErrorKind, IntoValue, MergeWithError, ValuePointerRef,
};
use index_scheduler::IndexScheduler;
2021-06-23 12:18:34 +02:00
use log::debug;
use meilisearch_types::error::{unwrap_any, Code, ErrorCode, ResponseError};
use meilisearch_types::index_uid::IndexUid;
use meilisearch_types::milli::{self, FieldDistribution, Index};
use meilisearch_types::tasks::KindWithContent;
2021-05-31 16:03:39 +02:00
use serde::{Deserialize, Serialize};
2021-10-12 14:46:35 +02:00
use serde_json::json;
use time::OffsetDateTime;
2020-12-12 13:32:06 +01:00
2022-10-20 18:00:07 +02:00
use super::{Pagination, SummarizedTaskView};
2021-10-12 14:46:35 +02:00
use crate::analytics::Analytics;
2022-10-20 18:00:07 +02:00
use crate::extractors::authentication::policies::*;
use crate::extractors::authentication::{AuthenticationError, GuardedData};
use crate::extractors::json::ValidatedJson;
use crate::extractors::query_parameters::QueryParameter;
2022-03-04 20:12:44 +01:00
use crate::extractors::sequential_extractor::SeqHandler;
2020-12-12 13:32:06 +01:00
2021-07-07 16:20:22 +02:00
pub mod documents;
pub mod search;
pub mod settings;
2021-07-05 14:29:20 +02:00
pub fn configure(cfg: &mut web::ServiceConfig) {
2021-06-24 15:33:21 +02:00
cfg.service(
2021-07-05 14:29:20 +02:00
web::resource("")
2021-06-24 15:33:21 +02:00
.route(web::get().to(list_indexes))
2022-03-04 20:12:44 +01:00
.route(web::post().to(SeqHandler(create_index))),
2021-06-24 15:33:21 +02:00
)
.service(
2021-07-05 14:29:20 +02:00
web::scope("/{index_uid}")
.service(
web::resource("")
2022-03-04 20:12:44 +01:00
.route(web::get().to(SeqHandler(get_index)))
.route(web::patch().to(SeqHandler(update_index)))
2022-03-04 20:12:44 +01:00
.route(web::delete().to(SeqHandler(delete_index))),
2021-07-05 14:29:20 +02:00
)
2022-03-04 20:12:44 +01:00
.service(web::resource("/stats").route(web::get().to(SeqHandler(get_index_stats))))
2021-07-05 14:29:20 +02:00
.service(web::scope("/documents").configure(documents::configure))
.service(web::scope("/search").configure(search::configure))
2021-09-24 14:55:57 +02:00
.service(web::scope("/settings").configure(settings::configure)),
2021-06-24 15:33:21 +02:00
);
2020-12-12 13:32:06 +01:00
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct IndexView {
pub uid: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
pub primary_key: Option<String>,
}
impl IndexView {
fn new(uid: String, index: &Index) -> Result<IndexView, milli::Error> {
let rtxn = index.read_txn()?;
Ok(IndexView {
uid,
created_at: index.created_at(&rtxn)?,
updated_at: index.updated_at(&rtxn)?,
primary_key: index.primary_key(&rtxn)?.map(String::from),
})
}
}
2021-09-28 22:22:59 +02:00
pub async fn list_indexes(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, Data<IndexScheduler>>,
paginate: QueryParameter<Pagination, ListIndexesDeserrError>,
2021-09-28 22:22:59 +02:00
) -> Result<HttpResponse, ResponseError> {
2022-09-27 16:33:37 +02:00
let search_rules = &index_scheduler.filters().search_rules;
let indexes: Vec<_> = index_scheduler.indexes()?;
let indexes = indexes
.into_iter()
.filter(|(name, _)| search_rules.is_index_authorized(name))
.map(|(name, index)| IndexView::new(name, &index))
.collect::<Result<Vec<_>, _>>()?;
let ret = paginate.auto_paginate_sized(indexes.into_iter());
debug!("returns: {:?}", ret);
Ok(HttpResponse::Ok().json(ret))
2021-07-05 14:29:20 +02:00
}
#[derive(Debug)]
pub struct ListIndexesDeserrError {
error: String,
code: Code,
}
impl std::fmt::Display for ListIndexesDeserrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.error)
}
}
impl std::error::Error for ListIndexesDeserrError {}
impl ErrorCode for ListIndexesDeserrError {
fn error_code(&self) -> Code {
self.code
}
}
impl MergeWithError<ListIndexesDeserrError> for ListIndexesDeserrError {
fn merge(
_self_: Option<Self>,
other: ListIndexesDeserrError,
_merge_location: ValuePointerRef,
) -> Result<Self, Self> {
Err(other)
}
}
impl deserr::DeserializeError for ListIndexesDeserrError {
fn error<V: IntoValue>(
_self_: Option<Self>,
error: ErrorKind<V>,
location: ValuePointerRef,
) -> Result<Self, Self> {
let code = match location.last_field() {
Some("offset") => Code::InvalidIndexLimit,
Some("limit") => Code::InvalidIndexOffset,
_ => Code::BadRequest,
};
let error = unwrap_any(deserr::serde_json::JsonError::error(None, error, location)).0;
Err(ListIndexesDeserrError { error, code })
}
}
impl MergeWithError<ParseIntError> for ListIndexesDeserrError {
fn merge(
_self_: Option<Self>,
other: ParseIntError,
merge_location: ValuePointerRef,
) -> Result<Self, Self> {
ListIndexesDeserrError::error::<Infallible>(
None,
ErrorKind::Unexpected { msg: other.to_string() },
merge_location,
)
}
}
#[derive(DeserializeFromValue, Debug)]
#[deserr(rename_all = camelCase, deny_unknown_fields)]
2021-07-07 16:20:22 +02:00
pub struct IndexCreateRequest {
2021-02-18 17:48:37 +01:00
uid: String,
2020-12-12 13:32:06 +01:00
primary_key: Option<String>,
}
2021-09-28 18:10:09 +02:00
pub async fn create_index(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_CREATE }>, Data<IndexScheduler>>,
body: ValidatedJson<IndexCreateRequest, CreateIndexesDeserrError>,
2021-10-13 20:56:28 +02:00
req: HttpRequest,
2021-10-29 16:10:58 +02:00
analytics: web::Data<dyn Analytics>,
2021-09-28 18:10:09 +02:00
) -> Result<HttpResponse, ResponseError> {
let IndexCreateRequest { primary_key, uid } = body.into_inner();
let uid = IndexUid::try_from(uid)?.into_inner();
2021-10-12 14:46:35 +02:00
let allow_index_creation = index_scheduler.filters().search_rules.is_index_authorized(&uid);
if allow_index_creation {
analytics.publish(
"Index Created".to_string(),
json!({ "primary_key": primary_key }),
Some(&req),
);
2022-10-20 18:00:07 +02:00
let task = KindWithContent::IndexCreation { index_uid: uid, primary_key };
2022-10-13 15:02:59 +02:00
let task: SummarizedTaskView =
2022-10-20 18:00:07 +02:00
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??.into();
Ok(HttpResponse::Accepted().json(task))
} else {
Err(AuthenticationError::InvalidToken.into())
}
2021-09-28 18:10:09 +02:00
}
2021-07-05 14:29:20 +02:00
#[derive(Debug)]
pub struct CreateIndexesDeserrError {
error: String,
code: Code,
}
impl std::fmt::Display for CreateIndexesDeserrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.error)
}
}
impl std::error::Error for CreateIndexesDeserrError {}
impl ErrorCode for CreateIndexesDeserrError {
fn error_code(&self) -> Code {
self.code
}
}
impl MergeWithError<CreateIndexesDeserrError> for CreateIndexesDeserrError {
fn merge(
_self_: Option<Self>,
other: CreateIndexesDeserrError,
_merge_location: ValuePointerRef,
) -> Result<Self, Self> {
Err(other)
}
}
impl deserr::DeserializeError for CreateIndexesDeserrError {
fn error<V: IntoValue>(
_self_: Option<Self>,
error: ErrorKind<V>,
location: ValuePointerRef,
) -> Result<Self, Self> {
let code = match location.last_field() {
Some("uid") => Code::InvalidIndexUid,
Some("primaryKey") => Code::InvalidIndexPrimaryKey,
None if matches!(error, ErrorKind::MissingField { field } if field == "uid") => {
Code::MissingIndexUid
}
_ => Code::BadRequest,
};
let error = unwrap_any(deserr::serde_json::JsonError::error(None, error, location)).0;
Err(CreateIndexesDeserrError { error, code })
}
}
#[derive(DeserializeFromValue, Debug)]
#[deserr(rename_all = camelCase, deny_unknown_fields)]
2021-07-07 16:20:22 +02:00
pub struct UpdateIndexRequest {
2020-12-12 13:32:06 +01:00
primary_key: Option<String>,
}
2021-07-07 16:20:22 +02:00
pub async fn get_index(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, Data<IndexScheduler>>,
index_uid: web::Path<String>,
2021-06-24 15:33:21 +02:00
) -> Result<HttpResponse, ResponseError> {
let index = index_scheduler.index(&index_uid)?;
let index_view = IndexView::new(index_uid.into_inner(), &index)?;
debug!("returns: {:?}", index_view);
Ok(HttpResponse::Ok().json(index_view))
2021-06-24 15:33:21 +02:00
}
2021-07-07 16:20:22 +02:00
pub async fn update_index(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_UPDATE }>, Data<IndexScheduler>>,
path: web::Path<String>,
body: ValidatedJson<UpdateIndexRequest, UpdateIndexesDeserrError>,
2021-10-13 20:56:28 +02:00
req: HttpRequest,
2021-10-29 16:10:58 +02:00
analytics: web::Data<dyn Analytics>,
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-15 16:52:05 +01:00
let body = body.into_inner();
2021-10-12 15:00:04 +02:00
analytics.publish(
"Index Updated".to_string(),
json!({ "primary_key": body.primary_key }),
2021-10-13 20:56:28 +02:00
Some(&req),
2021-10-12 15:00:04 +02:00
);
let task = KindWithContent::IndexUpdate {
index_uid: path.into_inner(),
primary_key: body.primary_key,
};
2022-10-13 15:02:59 +02:00
let task: SummarizedTaskView =
2022-10-20 18:00:07 +02:00
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??.into();
debug!("returns: {:?}", task);
Ok(HttpResponse::Accepted().json(task))
2020-12-12 13:32:06 +01:00
}
#[derive(Debug)]
pub struct UpdateIndexesDeserrError {
error: String,
code: Code,
}
impl std::fmt::Display for UpdateIndexesDeserrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.error)
}
}
impl std::error::Error for UpdateIndexesDeserrError {}
impl ErrorCode for UpdateIndexesDeserrError {
fn error_code(&self) -> Code {
self.code
}
}
impl MergeWithError<UpdateIndexesDeserrError> for UpdateIndexesDeserrError {
fn merge(
_self_: Option<Self>,
other: UpdateIndexesDeserrError,
_merge_location: ValuePointerRef,
) -> Result<Self, Self> {
Err(other)
}
}
impl deserr::DeserializeError for UpdateIndexesDeserrError {
fn error<V: IntoValue>(
_self_: Option<Self>,
error: ErrorKind<V>,
location: ValuePointerRef,
) -> Result<Self, Self> {
let code = match location.last_field() {
Some("primaryKey") => Code::InvalidIndexPrimaryKey,
_ => Code::BadRequest,
};
let error = unwrap_any(deserr::serde_json::JsonError::error(None, error, location)).0;
Err(UpdateIndexesDeserrError { error, code })
}
}
2021-09-28 18:10:09 +02:00
pub async fn delete_index(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_DELETE }>, Data<IndexScheduler>>,
index_uid: web::Path<String>,
2021-09-28 18:10:09 +02:00
) -> Result<HttpResponse, ResponseError> {
2022-10-20 18:00:07 +02:00
let task = KindWithContent::IndexDeletion { index_uid: index_uid.into_inner() };
2022-10-13 15:02:59 +02:00
let task: SummarizedTaskView =
2022-10-20 18:00:07 +02:00
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??.into();
Ok(HttpResponse::Accepted().json(task))
2021-09-28 18:10:09 +02:00
}
2020-12-12 13:32:06 +01:00
2021-07-07 16:20:22 +02:00
pub async fn get_index_stats(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::STATS_GET }>, Data<IndexScheduler>>,
index_uid: web::Path<String>,
2022-08-17 16:12:26 +02:00
req: HttpRequest,
analytics: web::Data<dyn Analytics>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2022-10-20 18:00:07 +02:00
analytics.publish("Stats Seen".to_string(), json!({ "per_index_uid": true }), Some(&req));
let stats = IndexStats::new((*index_scheduler).clone(), index_uid.into_inner())?;
debug!("returns: {:?}", stats);
Ok(HttpResponse::Ok().json(stats))
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct IndexStats {
pub number_of_documents: u64,
pub is_indexing: bool,
pub field_distribution: FieldDistribution,
}
impl IndexStats {
pub fn new(
index_scheduler: Data<IndexScheduler>,
index_uid: String,
) -> Result<Self, ResponseError> {
// we check if there is currently a task processing associated with this index.
let is_processing = index_scheduler.is_index_processing(&index_uid)?;
let index = index_scheduler.index(&index_uid)?;
let rtxn = index.read_txn()?;
Ok(IndexStats {
number_of_documents: index.number_of_documents(&rtxn)?,
is_indexing: is_processing,
field_distribution: index.field_distribution(&rtxn)?,
})
}
2020-12-12 13:32:06 +01:00
}