2023-01-24 15:18:00 +01:00
|
|
|
use actix_http::StatusCode;
|
|
|
|
use actix_web::web::{self, Data};
|
|
|
|
use actix_web::{HttpRequest, HttpResponse};
|
|
|
|
use deserr::actix_web::AwebJson;
|
|
|
|
use index_scheduler::IndexScheduler;
|
|
|
|
use meilisearch_types::deserr::DeserrJsonError;
|
|
|
|
use meilisearch_types::error::ResponseError;
|
|
|
|
use meilisearch_types::keys::actions;
|
|
|
|
use serde::Serialize;
|
2024-02-08 10:14:50 +01:00
|
|
|
use tracing::debug;
|
2023-01-24 15:18:00 +01:00
|
|
|
|
|
|
|
use crate::analytics::{Analytics, MultiSearchAggregator};
|
|
|
|
use crate::extractors::authentication::policies::ActionPolicy;
|
|
|
|
use crate::extractors::authentication::{AuthenticationError, GuardedData};
|
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2024-03-28 11:50:53 +01:00
|
|
|
use crate::routes::indexes::search::search_kind;
|
2023-01-24 15:18:00 +01:00
|
|
|
use crate::search::{
|
|
|
|
add_search_rules, perform_search, SearchQueryWithIndex, SearchResultWithIndex,
|
|
|
|
};
|
2024-03-26 17:28:03 +01:00
|
|
|
use crate::search_queue::SearchQueue;
|
2023-01-24 15:18:00 +01:00
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(web::resource("").route(web::post().to(SeqHandler(multi_search_with_post))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct SearchResults {
|
|
|
|
results: Vec<SearchResultWithIndex>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, deserr::Deserr)]
|
|
|
|
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)]
|
|
|
|
pub struct SearchQueries {
|
|
|
|
queries: Vec<SearchQueryWithIndex>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn multi_search_with_post(
|
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::SEARCH }>, Data<IndexScheduler>>,
|
2024-03-26 17:28:03 +01:00
|
|
|
search_queue: Data<SearchQueue>,
|
2023-01-24 15:18:00 +01:00
|
|
|
params: AwebJson<SearchQueries, DeserrJsonError>,
|
|
|
|
req: HttpRequest,
|
|
|
|
analytics: web::Data<dyn Analytics>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let queries = params.into_inner().queries;
|
|
|
|
|
|
|
|
let mut multi_aggregate = MultiSearchAggregator::from_queries(&queries, &req);
|
2023-10-23 10:38:56 +02:00
|
|
|
let features = index_scheduler.features();
|
2023-01-24 15:18:00 +01:00
|
|
|
|
2024-03-26 17:28:03 +01:00
|
|
|
// Since we don't want to process half of the search requests and then get a permit refused
|
|
|
|
// we're going to get one permit for the whole duration of the multi-search request.
|
|
|
|
let _permit = search_queue.try_get_search_permit().await?;
|
|
|
|
|
2023-01-24 15:18:00 +01:00
|
|
|
// Explicitly expect a `(ResponseError, usize)` for the error type rather than `ResponseError` only,
|
|
|
|
// so that `?` doesn't work if it doesn't use `with_index`, ensuring that it is not forgotten in case of code
|
|
|
|
// changes.
|
2023-11-28 14:23:32 +01:00
|
|
|
let search_results: Result<_, (ResponseError, usize)> = async {
|
|
|
|
let mut search_results = Vec::with_capacity(queries.len());
|
|
|
|
for (query_index, (index_uid, mut query)) in
|
|
|
|
queries.into_iter().map(SearchQueryWithIndex::into_index_query).enumerate()
|
|
|
|
{
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(on_index = query_index, parameters = ?query, "Multi-search");
|
2023-11-28 14:23:32 +01:00
|
|
|
|
|
|
|
// Check index from API key
|
|
|
|
if !index_scheduler.filters().is_index_authorized(&index_uid) {
|
|
|
|
return Err(AuthenticationError::InvalidToken).with_index(query_index);
|
|
|
|
}
|
|
|
|
// Apply search rules from tenant token
|
|
|
|
if let Some(search_rules) = index_scheduler.filters().get_index_search_rules(&index_uid)
|
2023-01-24 15:18:00 +01:00
|
|
|
{
|
2023-11-28 14:23:32 +01:00
|
|
|
add_search_rules(&mut query, search_rules);
|
|
|
|
}
|
2023-01-24 15:18:00 +01:00
|
|
|
|
2023-11-28 14:23:32 +01:00
|
|
|
let index = index_scheduler
|
|
|
|
.index(&index_uid)
|
|
|
|
.map_err(|err| {
|
|
|
|
let mut err = ResponseError::from(err);
|
|
|
|
// Patch the HTTP status code to 400 as it defaults to 404 for `index_not_found`, but
|
|
|
|
// here the resource not found is not part of the URL.
|
|
|
|
err.code = StatusCode::BAD_REQUEST;
|
|
|
|
err
|
|
|
|
})
|
|
|
|
.with_index(query_index)?;
|
2023-01-24 15:18:00 +01:00
|
|
|
|
2024-03-28 11:50:53 +01:00
|
|
|
let search_kind =
|
|
|
|
search_kind(&query, index_scheduler.get_ref(), &index).with_index(query_index)?;
|
2023-11-15 15:46:37 +01:00
|
|
|
|
2023-12-14 16:01:35 +01:00
|
|
|
let search_result = tokio::task::spawn_blocking(move || {
|
2024-03-28 11:50:53 +01:00
|
|
|
perform_search(&index, query, features, search_kind)
|
2023-12-14 16:01:35 +01:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.with_index(query_index)?;
|
2023-06-22 23:12:29 +02:00
|
|
|
|
2023-11-28 14:23:32 +01:00
|
|
|
search_results.push(SearchResultWithIndex {
|
|
|
|
index_uid: index_uid.into_inner(),
|
|
|
|
result: search_result.with_index(query_index)?,
|
|
|
|
});
|
2023-01-24 15:18:00 +01:00
|
|
|
}
|
2023-11-28 14:23:32 +01:00
|
|
|
Ok(search_results)
|
2023-11-23 12:20:44 +01:00
|
|
|
}
|
2023-01-24 15:18:00 +01:00
|
|
|
.await;
|
|
|
|
|
|
|
|
if search_results.is_ok() {
|
|
|
|
multi_aggregate.succeed();
|
|
|
|
}
|
|
|
|
analytics.post_multi_search(multi_aggregate);
|
|
|
|
|
|
|
|
let search_results = search_results.map_err(|(mut err, query_index)| {
|
|
|
|
// Add the query index that failed as context for the error message.
|
|
|
|
// We're doing it only here and not directly in the `WithIndex` trait so that the `with_index` function returns a different type
|
|
|
|
// of result and we can benefit from static typing.
|
|
|
|
err.message = format!("Inside `.queries[{query_index}]`: {}", err.message);
|
|
|
|
err
|
|
|
|
})?;
|
|
|
|
|
2024-02-08 10:14:50 +01:00
|
|
|
debug!(returns = ?search_results, "Multi-search");
|
2023-01-24 15:18:00 +01:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(SearchResults { results: search_results }))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Local `Result` extension trait to avoid `map_err` boilerplate.
|
|
|
|
trait WithIndex {
|
|
|
|
type T;
|
|
|
|
/// convert the error type inside of the `Result` to a `ResponseError`, and return a couple of it + the usize.
|
|
|
|
fn with_index(self, index: usize) -> Result<Self::T, (ResponseError, usize)>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E: Into<ResponseError>> WithIndex for Result<T, E> {
|
|
|
|
type T = T;
|
|
|
|
fn with_index(self, index: usize) -> Result<T, (ResponseError, usize)> {
|
|
|
|
self.map_err(|err| (err.into(), index))
|
|
|
|
}
|
|
|
|
}
|