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

258 lines
9.3 KiB
Rust
Raw Normal View History

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};
use index_scheduler::IndexScheduler;
2021-06-23 12:18:34 +02:00
use log::debug;
use meilisearch_types::deserr::query_params::Param;
use meilisearch_types::deserr::{immutable_field_error, DeserrJsonError, DeserrQueryParamError};
2023-01-17 11:05:01 +01:00
use meilisearch_types::error::deserr_codes::*;
use meilisearch_types::error::{Code, ResponseError};
use meilisearch_types::index_uid::IndexUid;
use meilisearch_types::milli::{self, FieldDistribution, Index};
use meilisearch_types::tasks::KindWithContent;
use serde::Serialize;
2021-10-12 14:46:35 +02:00
use serde_json::json;
use time::OffsetDateTime;
2020-12-12 13:32:06 +01:00
2023-01-11 14:31:34 +01:00
use super::{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;
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, 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),
})
}
}
2023-02-13 18:45:13 +01:00
#[derive(Deserr, Debug, Clone, Copy)]
#[deserr(error = DeserrQueryParamError, rename_all = camelCase, deny_unknown_fields)]
2023-01-11 14:31:34 +01:00
pub struct ListIndexes {
#[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 {
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> {
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<_>, _>>()?;
2023-01-11 12:33:56 +01:00
let ret = paginate.as_pagination().auto_paginate_sized(indexes.into_iter());
debug!("returns: {:?}", ret);
Ok(HttpResponse::Ok().json(ret))
2021-07-05 14:29:20 +02:00
}
2023-02-13 18:45:13 +01:00
#[derive(Deserr, Debug)]
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)]
2021-07-07 16:20:22 +02:00
pub struct IndexCreateRequest {
#[deserr(error = DeserrJsonError<InvalidIndexUid>, missing_field_error = DeserrJsonError::missing_index_uid)]
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,
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();
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),
);
let task = KindWithContent::IndexCreation { index_uid: uid.to_string(), 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
fn deny_immutable_fields_index(
field: &str,
accepted: &[&str],
location: ValuePointerRef,
) -> DeserrJsonError {
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),
_ => deserr::take_cf_content(DeserrJsonError::<BadRequest>::error::<Infallible>(
None,
deserr::ErrorKind::UnknownKey { key: field, accepted },
location,
)),
}
}
2023-02-13 18:45:13 +01:00
#[derive(Deserr, Debug)]
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields = deny_immutable_fields_index)]
2021-07-07 16:20:22 +02:00
pub struct UpdateIndexRequest {
#[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> {
let index_uid = IndexUid::try_from(index_uid.into_inner())?;
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>>,
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,
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);
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(),
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: index_uid.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
}
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> {
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() };
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> {
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));
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
}