2023-01-12 09:35:56 +01:00
|
|
|
use std::convert::Infallible;
|
|
|
|
|
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};
|
2023-02-14 13:12:42 +01:00
|
|
|
use deserr::actix_web::{AwebJson, AwebQueryParameter};
|
2023-02-13 18:45:13 +01:00
|
|
|
use deserr::{DeserializeError, Deserr, ValuePointerRef};
|
2022-10-27 11:17:50 +02:00
|
|
|
use index_scheduler::IndexScheduler;
|
2023-01-16 16:59:26 +01:00
|
|
|
use meilisearch_types::deserr::query_params::Param;
|
2023-02-13 19:34:47 +01:00
|
|
|
use meilisearch_types::deserr::{immutable_field_error, DeserrJsonError, DeserrQueryParamError};
|
2023-01-17 11:05:01 +01:00
|
|
|
use meilisearch_types::error::deserr_codes::*;
|
2023-02-14 13:58:33 +01:00
|
|
|
use meilisearch_types::error::{Code, ResponseError};
|
2022-10-22 14:47:46 +02:00
|
|
|
use meilisearch_types::index_uid::IndexUid;
|
2022-10-11 17:42:43 +02:00
|
|
|
use meilisearch_types::milli::{self, FieldDistribution, Index};
|
2022-10-27 11:17:50 +02:00
|
|
|
use meilisearch_types::tasks::KindWithContent;
|
2023-01-17 13:51:07 +01:00
|
|
|
use serde::Serialize;
|
2021-10-12 14:46:35 +02:00
|
|
|
use serde_json::json;
|
2022-02-14 15:32:41 +01:00
|
|
|
use time::OffsetDateTime;
|
2024-02-08 10:14:50 +01:00
|
|
|
use tracing::debug;
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2023-09-07 11:16:51 +02:00
|
|
|
use super::{get_task_id, Pagination, SummarizedTaskView, PAGINATION_DEFAULT_LIMIT};
|
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};
|
2022-03-04 20:12:44 +01:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2024-02-20 11:24:44 +01:00
|
|
|
use crate::Opt;
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2021-07-07 16:20:22 +02:00
|
|
|
pub mod documents;
|
2023-04-13 18:16:33 +02:00
|
|
|
pub mod facet_search;
|
2021-07-07 16:20:22 +02:00
|
|
|
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)))
|
2022-06-02 11:48:59 +02:00
|
|
|
.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))
|
2023-04-13 18:16:33 +02:00
|
|
|
.service(web::scope("/facet-search").configure(facet_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
|
|
|
}
|
|
|
|
|
2023-01-17 13:51:07 +01:00
|
|
|
#[derive(Debug, Serialize, Clone)]
|
2022-09-27 19:52:06 +02:00
|
|
|
#[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>,
|
|
|
|
}
|
|
|
|
|
2022-10-04 11:06:48 +02:00
|
|
|
impl IndexView {
|
2022-10-11 17:42:43 +02:00
|
|
|
fn new(uid: String, index: &Index) -> Result<IndexView, milli::Error> {
|
2023-02-20 16:42:54 +01:00
|
|
|
// It is important that this function does not keep the Index handle or a clone of it, because
|
|
|
|
// `list_indexes` relies on this property to avoid opening all indexes at once.
|
2022-10-04 11:06:48 +02:00
|
|
|
let rtxn = index.read_txn()?;
|
2022-09-27 19:52:06 +02:00
|
|
|
Ok(IndexView {
|
2022-10-04 11:06:48 +02:00
|
|
|
uid,
|
|
|
|
created_at: index.created_at(&rtxn)?,
|
|
|
|
updated_at: index.updated_at(&rtxn)?,
|
|
|
|
primary_key: index.primary_key(&rtxn)?.map(String::from),
|
2022-09-27 19:52:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 18:45:13 +01:00
|
|
|
#[derive(Deserr, Debug, Clone, Copy)]
|
2023-01-12 13:55:53 +01:00
|
|
|
#[deserr(error = DeserrQueryParamError, rename_all = camelCase, deny_unknown_fields)]
|
2023-01-11 14:31:34 +01:00
|
|
|
pub struct ListIndexes {
|
2023-01-16 16:59:26 +01:00
|
|
|
#[deserr(default, error = DeserrQueryParamError<InvalidIndexOffset>)]
|
|
|
|
pub offset: Param<usize>,
|
|
|
|
#[deserr(default = Param(PAGINATION_DEFAULT_LIMIT), error = DeserrQueryParamError<InvalidIndexLimit>)]
|
|
|
|
pub limit: Param<usize>,
|
2023-01-11 14:31:34 +01:00
|
|
|
}
|
|
|
|
impl ListIndexes {
|
|
|
|
fn as_pagination(self) -> Pagination {
|
2023-01-16 16:59:26 +01:00
|
|
|
Pagination { offset: self.offset.0, limit: self.limit.0 }
|
2023-01-11 14:31:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>>,
|
2023-02-14 13:12:42 +01:00
|
|
|
paginate: AwebQueryParameter<ListIndexes, DeserrQueryParamError>,
|
2021-09-28 22:22:59 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(parameters = ?paginate, "List indexes");
|
2023-02-20 09:25:29 +01:00
|
|
|
let filters = index_scheduler.filters();
|
2023-02-20 16:42:54 +01:00
|
|
|
let indexes: Vec<Option<IndexView>> =
|
|
|
|
index_scheduler.try_for_each_index(|uid, index| -> Result<Option<IndexView>, _> {
|
|
|
|
if !filters.is_index_authorized(uid) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
Ok(Some(IndexView::new(uid.to_string(), index)?))
|
|
|
|
})?;
|
|
|
|
// Won't cause to open all indexes because IndexView doesn't keep the `Index` opened.
|
|
|
|
let indexes: Vec<IndexView> = indexes.into_iter().flatten().collect();
|
2023-01-11 12:33:56 +01:00
|
|
|
let ret = paginate.as_pagination().auto_paginate_sized(indexes.into_iter());
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(returns = ?ret, "List indexes");
|
2022-06-02 13:31:46 +02:00
|
|
|
Ok(HttpResponse::Ok().json(ret))
|
2021-07-05 14:29:20 +02:00
|
|
|
}
|
|
|
|
|
2023-02-13 18:45:13 +01:00
|
|
|
#[derive(Deserr, Debug)]
|
2023-01-12 13:55:53 +01:00
|
|
|
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)]
|
2021-07-07 16:20:22 +02:00
|
|
|
pub struct IndexCreateRequest {
|
2023-01-12 13:55:53 +01:00
|
|
|
#[deserr(error = DeserrJsonError<InvalidIndexUid>, missing_field_error = DeserrJsonError::missing_index_uid)]
|
2023-01-17 13:51:07 +01:00
|
|
|
uid: IndexUid,
|
|
|
|
#[deserr(default, error = DeserrJsonError<InvalidIndexPrimaryKey>)]
|
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>>,
|
2023-02-14 13:12:42 +01:00
|
|
|
body: AwebJson<IndexCreateRequest, DeserrJsonError>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2024-02-20 11:24:44 +01:00
|
|
|
opt: web::Data<Opt>,
|
2021-10-29 16:10:58 +02:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2021-09-28 18:10:09 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(parameters = ?body, "Create index");
|
2022-09-22 12:14:51 +02:00
|
|
|
let IndexCreateRequest { primary_key, uid } = body.into_inner();
|
2021-10-12 14:46:35 +02:00
|
|
|
|
2023-02-22 12:13:53 +01:00
|
|
|
let allow_index_creation = index_scheduler.filters().allow_index_creation(&uid);
|
2022-09-06 15:13:09 +02:00
|
|
|
if allow_index_creation {
|
|
|
|
analytics.publish(
|
|
|
|
"Index Created".to_string(),
|
|
|
|
json!({ "primary_key": primary_key }),
|
|
|
|
Some(&req),
|
|
|
|
);
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2023-01-17 13:51:07 +01:00
|
|
|
let task = KindWithContent::IndexCreation { index_uid: uid.to_string(), primary_key };
|
2024-02-20 11:24:44 +01:00
|
|
|
let uid = get_task_id(&req, &opt)?;
|
2022-10-13 15:02:59 +02:00
|
|
|
let task: SummarizedTaskView =
|
2023-09-07 11:16:51 +02:00
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task, uid)).await??.into();
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(returns = ?task, "Create index");
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-09-06 15:13:09 +02:00
|
|
|
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
|
|
|
|
2023-01-12 09:35:56 +01:00
|
|
|
fn deny_immutable_fields_index(
|
|
|
|
field: &str,
|
|
|
|
accepted: &[&str],
|
|
|
|
location: ValuePointerRef,
|
2023-01-12 13:55:53 +01:00
|
|
|
) -> DeserrJsonError {
|
2023-01-19 16:45:10 +01:00
|
|
|
match field {
|
|
|
|
"uid" => immutable_field_error(field, accepted, Code::ImmutableIndexUid),
|
|
|
|
"createdAt" => immutable_field_error(field, accepted, Code::ImmutableIndexCreatedAt),
|
|
|
|
"updatedAt" => immutable_field_error(field, accepted, Code::ImmutableIndexUpdatedAt),
|
2023-02-14 13:58:33 +01:00
|
|
|
_ => deserr::take_cf_content(DeserrJsonError::<BadRequest>::error::<Infallible>(
|
2023-01-19 16:45:10 +01:00
|
|
|
None,
|
|
|
|
deserr::ErrorKind::UnknownKey { key: field, accepted },
|
|
|
|
location,
|
|
|
|
)),
|
|
|
|
}
|
2023-01-12 09:35:56 +01:00
|
|
|
}
|
2023-01-19 16:45:10 +01:00
|
|
|
|
2023-02-13 18:45:13 +01:00
|
|
|
#[derive(Deserr, Debug)]
|
2023-01-12 13:55:53 +01:00
|
|
|
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields = deny_immutable_fields_index)]
|
2021-07-07 16:20:22 +02:00
|
|
|
pub struct UpdateIndexRequest {
|
2023-01-17 13:51:07 +01:00
|
|
|
#[deserr(default, error = DeserrJsonError<InvalidIndexPrimaryKey>)]
|
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> {
|
2023-01-17 13:51:07 +01:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?;
|
|
|
|
|
2022-09-27 19:52:06 +02:00
|
|
|
let index = index_scheduler.index(&index_uid)?;
|
2022-10-04 11:06:48 +02:00
|
|
|
let index_view = IndexView::new(index_uid.into_inner(), &index)?;
|
2022-09-27 19:52:06 +02:00
|
|
|
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(returns = ?index_view, "Get index");
|
2022-09-22 12:14:51 +02:00
|
|
|
|
2022-09-27 19:52:06 +02:00
|
|
|
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>>,
|
2023-01-17 13:51:07 +01:00
|
|
|
index_uid: web::Path<String>,
|
2023-02-14 13:12:42 +01:00
|
|
|
body: AwebJson<UpdateIndexRequest, DeserrJsonError>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2024-02-20 11:24:44 +01:00
|
|
|
opt: web::Data<Opt>,
|
2021-10-29 16:10:58 +02:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(parameters = ?body, "Update index");
|
2023-01-17 13:51:07 +01:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?;
|
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(),
|
2022-12-14 13:00:43 +01:00
|
|
|
json!({ "primary_key": body.primary_key }),
|
2021-10-13 20:56:28 +02:00
|
|
|
Some(&req),
|
2021-10-12 15:00:04 +02:00
|
|
|
);
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-09-22 12:14:51 +02:00
|
|
|
let task = KindWithContent::IndexUpdate {
|
2023-01-17 13:51:07 +01:00
|
|
|
index_uid: index_uid.into_inner(),
|
2021-09-21 13:23:22 +02:00
|
|
|
primary_key: body.primary_key,
|
|
|
|
};
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2024-02-20 11:24:44 +01:00
|
|
|
let uid = get_task_id(&req, &opt)?;
|
2022-10-13 15:02:59 +02:00
|
|
|
let task: SummarizedTaskView =
|
2023-09-07 11:16:51 +02:00
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task, uid)).await??.into();
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(returns = ?task, "Update index");
|
2021-12-02 16:03:26 +01:00
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
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>,
|
2023-09-07 11:16:51 +02:00
|
|
|
req: HttpRequest,
|
2024-02-20 11:24:44 +01:00
|
|
|
opt: web::Data<Opt>,
|
2021-09-28 18:10:09 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2023-01-17 13:51:07 +01:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?;
|
2022-10-20 18:00:07 +02:00
|
|
|
let task = KindWithContent::IndexDeletion { index_uid: index_uid.into_inner() };
|
2024-02-20 11:24:44 +01:00
|
|
|
let uid = get_task_id(&req, &opt)?;
|
2022-10-13 15:02:59 +02:00
|
|
|
let task: SummarizedTaskView =
|
2023-09-07 11:16:51 +02:00
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task, uid)).await??.into();
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(returns = ?task, "Delete index");
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-28 18:10:09 +02:00
|
|
|
}
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2023-02-28 15:24:31 +01:00
|
|
|
/// Stats of an `Index`, as known to the `stats` route.
|
2023-02-23 19:31:57 +01:00
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IndexStats {
|
2023-02-28 15:24:31 +01:00
|
|
|
/// Number of documents in the index
|
2023-02-23 19:31:57 +01:00
|
|
|
pub number_of_documents: u64,
|
2023-02-28 15:24:31 +01:00
|
|
|
/// Whether the index is currently performing indexation, according to the scheduler.
|
2023-02-23 19:31:57 +01:00
|
|
|
pub is_indexing: bool,
|
2023-02-28 15:24:31 +01:00
|
|
|
/// Association of every field name with the number of times it occurs in the documents.
|
2023-02-23 19:31:57 +01:00
|
|
|
pub field_distribution: FieldDistribution,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<index_scheduler::IndexStats> for IndexStats {
|
|
|
|
fn from(stats: index_scheduler::IndexStats) -> Self {
|
|
|
|
IndexStats {
|
|
|
|
number_of_documents: stats.inner_stats.number_of_documents,
|
|
|
|
is_indexing: stats.is_indexing,
|
|
|
|
field_distribution: stats.inner_stats.field_distribution,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2023-01-17 13:51:07 +01:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?;
|
2022-10-20 18:00:07 +02:00
|
|
|
analytics.publish("Stats Seen".to_string(), json!({ "per_index_uid": true }), Some(&req));
|
2021-04-29 19:31:58 +02:00
|
|
|
|
2023-02-23 19:31:57 +01:00
|
|
|
let stats = IndexStats::from(index_scheduler.index_stats(&index_uid)?);
|
2022-09-27 19:52:06 +02:00
|
|
|
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(returns = ?stats, "Get index stats");
|
2022-09-27 19:52:06 +02:00
|
|
|
Ok(HttpResponse::Ok().json(stats))
|
|
|
|
}
|