From 1ee6933b80bfce9c0c17c514be3151c051769f1d Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sat, 20 Jul 2024 18:25:45 +0200 Subject: [PATCH] fixed the testcase being externally defined --- server/main-api/src/search/mod.rs | 17 +++++++++++--- .../src/search/search_executor/mod.rs | 22 ++++++++++++++----- .../src/search/search_executor/query.rs | 20 +++++++++++------ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/server/main-api/src/search/mod.rs b/server/main-api/src/search/mod.rs index ef23b514f..4cb666c10 100644 --- a/server/main-api/src/search/mod.rs +++ b/server/main-api/src/search/mod.rs @@ -3,6 +3,7 @@ use std::time::Instant; use crate::AppData; use actix_web::{get, web, HttpResponse}; use cached::proc_macro::cached; +use meilisearch_sdk::client::Client; use serde::{Deserialize, Serialize}; use tracing::{debug, error}; use unicode_truncate::UnicodeTruncateStr; @@ -131,9 +132,19 @@ async fn cached_geoentry_search( highlighting: Highlighting, limits: Limits, ) -> Vec { - search_executor::do_geoentry_search(q, highlighting, limits) - .await - .0 + let ms_url = std::env::var("MIELI_URL").unwrap_or_else(|_| "http://localhost:7700".to_string()); + let client = Client::new(ms_url, std::env::var("MEILI_MASTER_KEY").ok()); + match client { + Ok(client) => { + search_executor::do_geoentry_search(&client, q, highlighting, limits) + .await + .0 + } + Err(e) => { + error!("Cannot connect to meilisearch because {e:?}"); + vec![] + } + } } #[cfg(test)] diff --git a/server/main-api/src/search/search_executor/mod.rs b/server/main-api/src/search/search_executor/mod.rs index 7fbfba294..3974cf3a0 100644 --- a/server/main-api/src/search/search_executor/mod.rs +++ b/server/main-api/src/search/search_executor/mod.rs @@ -1,3 +1,4 @@ +use meilisearch_sdk::client::Client; use serde::Serialize; use tracing::error; @@ -37,13 +38,14 @@ struct ResultEntry { } #[tracing::instrument] pub async fn do_geoentry_search( + client: &Client, q: String, highlighting: Highlighting, limits: Limits, ) -> LimitedVec { let parsed_input = ParsedQuery::from(q.as_str()); - match query::GeoEntryQuery::from((&parsed_input, &limits, &highlighting)) + match query::GeoEntryQuery::from((client, &parsed_input, &limits, &highlighting)) .execute() .await { @@ -76,6 +78,7 @@ pub async fn do_geoentry_search( #[cfg(test)] mod test { use super::*; + use crate::setup::tests::MeiliSearchTestContainer; use std::fmt::{Display, Formatter}; #[derive(serde::Deserialize)] @@ -98,8 +101,9 @@ mod test { let mut acceptable_range = actual.iter().flat_map(|r| r.entries.clone()).take(among); acceptable_range.any(|r| r.id == self.target) } - async fn search(&self) -> Vec { + async fn search(&self, client: &Client) -> Vec { do_geoentry_search( + client, self.query.clone(), Highlighting::default(), Limits::default(), @@ -126,8 +130,12 @@ mod test { #[tokio::test] #[tracing_test::traced_test] async fn test_good_queries() { + let ms = MeiliSearchTestContainer::new().await; + crate::setup::meilisearch::load_data(&ms.client) + .await + .unwrap(); for query in TestQuery::load_good() { - let actual = query.search().await; + let actual = query.search(&ms.client).await; assert!( query.actual_matches_among(&actual), "{query}\nSince it can't, please move it to .bad list, actual={actual:?}" @@ -145,8 +153,12 @@ mod test { #[tokio::test] #[tracing_test::traced_test] async fn test_bad_queries() { - for query in TestQuery::load_bad() { - let actual = query.search().await; + let ms = MeiliSearchTestContainer::new().await; + crate::setup::meilisearch::load_data(&ms.client) + .await + .unwrap(); + for query in TestQuery::load_bad() { + let actual = query.search(&ms.client).await; assert!( !query.actual_matches_among(&actual), "{query}\nSince it can't, please move it to .bad list, actual={actual:?}" diff --git a/server/main-api/src/search/search_executor/query.rs b/server/main-api/src/search/search_executor/query.rs index 6c1b16c47..d1a3758c3 100644 --- a/server/main-api/src/search/search_executor/query.rs +++ b/server/main-api/src/search/search_executor/query.rs @@ -45,6 +45,7 @@ impl From<&Filter> for GeoEntryFilters { #[derive(Debug)] pub(super) struct GeoEntryQuery { + client: Client, parsed_input: ParsedQuery, limits: Limits, highlighting: Highlighting, @@ -52,9 +53,17 @@ pub(super) struct GeoEntryQuery { sorting: Vec, } -impl From<(&ParsedQuery, &Limits, &Highlighting)> for GeoEntryQuery { - fn from((parsed_input, limits, highlighting): (&ParsedQuery, &Limits, &Highlighting)) -> Self { +impl From<(&Client, &ParsedQuery, &Limits, &Highlighting)> for GeoEntryQuery { + fn from( + (client, parsed_input, limits, highlighting): ( + &Client, + &ParsedQuery, + &Limits, + &Highlighting, + ), + ) -> Self { Self { + client: client.clone(), parsed_input: parsed_input.clone(), limits: *limits, highlighting: highlighting.clone(), @@ -68,10 +77,7 @@ impl GeoEntryQuery { #[tracing::instrument(ret(level = tracing::Level::TRACE))] pub async fn execute(self) -> Result, Error> { let q_default = self.prompt_for_querying(); - let ms_url = - std::env::var("MIELI_URL").unwrap_or_else(|_| "http://localhost:7700".to_string()); - let client = Client::new(ms_url, std::env::var("MEILI_MASTER_KEY").ok())?; - let entries = client.index("entries"); + let entries = self.client.index("entries"); // due to lifetime shenanigans this is added here (I can't make it move down to the other statements) // If you can make it, please propose a PR, I know that this is really hacky ^^ @@ -86,7 +92,7 @@ impl GeoEntryQuery { // for all entries and only rooms, search matching (and relevant) buildings can be // expected to be at the top of the merged search. However sometimes a lot of // buildings will be hidden (e.g. building parts), so the extra room search .... - client + self.client .multi_search() .with_search_query( self.merged_query(&entries, &q_default)