mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-06-14 07:57:59 +00:00
231 lines
7.3 KiB
Rust
231 lines
7.3 KiB
Rust
use std::convert::TryFrom;
|
|
use std::sync::Arc;
|
|
|
|
use actix_web::web::Data;
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
|
use index::Index;
|
|
use index_scheduler::milli::FieldDistribution;
|
|
use index_scheduler::{IndexScheduler, KindWithContent, Query, Status};
|
|
use log::debug;
|
|
use meilisearch_types::error::ResponseError;
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::json;
|
|
use time::OffsetDateTime;
|
|
|
|
use crate::analytics::Analytics;
|
|
use crate::extractors::authentication::{policies::*, AuthenticationError, GuardedData};
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
|
use index_scheduler::task::TaskView;
|
|
|
|
use super::Pagination;
|
|
|
|
pub mod documents;
|
|
pub mod search;
|
|
pub mod settings;
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(
|
|
web::resource("")
|
|
.route(web::get().to(list_indexes))
|
|
.route(web::post().to(SeqHandler(create_index))),
|
|
)
|
|
.service(
|
|
web::scope("/{index_uid}")
|
|
.service(
|
|
web::resource("")
|
|
.route(web::get().to(SeqHandler(get_index)))
|
|
.route(web::patch().to(SeqHandler(update_index)))
|
|
.route(web::delete().to(SeqHandler(delete_index))),
|
|
)
|
|
.service(web::resource("/stats").route(web::get().to(SeqHandler(get_index_stats))))
|
|
.service(web::scope("/documents").configure(documents::configure))
|
|
.service(web::scope("/search").configure(search::configure))
|
|
.service(web::scope("/settings").configure(settings::configure)),
|
|
);
|
|
}
|
|
|
|
#[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 TryFrom<&Index> for IndexView {
|
|
type Error = index::error::IndexError;
|
|
|
|
fn try_from(index: &Index) -> Result<IndexView, Self::Error> {
|
|
Ok(IndexView {
|
|
uid: index.name.clone(),
|
|
created_at: index.created_at()?,
|
|
updated_at: index.updated_at()?,
|
|
primary_key: index.primary_key()?,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub async fn list_indexes(
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, Data<IndexScheduler>>,
|
|
paginate: web::Query<Pagination>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
let search_rules = &index_scheduler.filters().search_rules;
|
|
let indexes: Vec<_> = index_scheduler.indexes()?;
|
|
let nb_indexes = indexes.len();
|
|
let indexes = indexes
|
|
.iter()
|
|
.filter(|index| search_rules.is_index_authorized(&index.name))
|
|
.map(IndexView::try_from)
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
let ret = paginate.auto_paginate_sized(indexes.into_iter());
|
|
|
|
debug!("returns: {:?}", ret);
|
|
Ok(HttpResponse::Ok().json(ret))
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
pub struct IndexCreateRequest {
|
|
uid: String,
|
|
primary_key: Option<String>,
|
|
}
|
|
|
|
pub async fn create_index(
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_CREATE }>, Data<IndexScheduler>>,
|
|
body: web::Json<IndexCreateRequest>,
|
|
req: HttpRequest,
|
|
analytics: web::Data<dyn Analytics>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
let IndexCreateRequest { primary_key, uid } = body.into_inner();
|
|
|
|
let allow_index_creation = meilisearch.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,
|
|
primary_key,
|
|
};
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
|
} else {
|
|
Err(AuthenticationError::InvalidToken.into())
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
#[allow(dead_code)]
|
|
pub struct UpdateIndexRequest {
|
|
uid: Option<String>,
|
|
primary_key: Option<String>,
|
|
}
|
|
|
|
pub async fn get_index(
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_GET }>, Data<IndexScheduler>>,
|
|
index_uid: web::Path<String>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
let index = index_scheduler.index(&index_uid)?;
|
|
let index_view: IndexView = (&index).try_into()?;
|
|
|
|
debug!("returns: {:?}", index_view);
|
|
|
|
Ok(HttpResponse::Ok().json(index_view))
|
|
}
|
|
|
|
pub async fn update_index(
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_UPDATE }>, Data<IndexScheduler>>,
|
|
path: web::Path<String>,
|
|
body: web::Json<UpdateIndexRequest>,
|
|
req: HttpRequest,
|
|
analytics: web::Data<dyn Analytics>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
debug!("called with params: {:?}", body);
|
|
let body = body.into_inner();
|
|
analytics.publish(
|
|
"Index Updated".to_string(),
|
|
json!({ "primary_key": body.primary_key}),
|
|
Some(&req),
|
|
);
|
|
|
|
let task = KindWithContent::IndexUpdate {
|
|
index_uid: path.into_inner(),
|
|
primary_key: body.primary_key,
|
|
};
|
|
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
|
|
|
debug!("returns: {:?}", task);
|
|
Ok(HttpResponse::Accepted().json(task))
|
|
}
|
|
|
|
pub async fn delete_index(
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_DELETE }>, Data<IndexScheduler>>,
|
|
index_uid: web::Path<String>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
let task = KindWithContent::IndexDeletion {
|
|
index_uid: index_uid.into_inner(),
|
|
};
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
|
}
|
|
|
|
pub async fn get_index_stats(
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::STATS_GET }>, Data<IndexScheduler>>,
|
|
index_uid: web::Path<String>,
|
|
req: HttpRequest,
|
|
analytics: web::Data<dyn Analytics>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
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 processing_task = index_scheduler.get_tasks(
|
|
Query::default()
|
|
.with_status(Status::Processing)
|
|
.with_index(index_uid.clone())
|
|
.with_limit(1),
|
|
)?;
|
|
let is_processing = !processing_task.is_empty();
|
|
|
|
let index = index_scheduler.index(&index_uid)?;
|
|
Ok(IndexStats {
|
|
number_of_documents: index.number_of_documents()?,
|
|
is_indexing: is_processing,
|
|
field_distribution: index.field_distribution()?,
|
|
})
|
|
}
|
|
}
|