From f3f39444697e1f564c8ec2b24c924d943f574c2f Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Thu, 14 Dec 2023 11:21:25 +0100 Subject: [PATCH] Fix error checking --- meilisearch-types/src/error.rs | 2 +- meilisearch/src/routes/indexes/search.rs | 29 +++++++++++++++++++++--- meilisearch/src/search.rs | 3 +++ meilisearch/tests/search/hybrid.rs | 10 ++++---- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index 1dc33b140..6f2624053 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -463,7 +463,7 @@ impl fmt::Display for deserr_codes::InvalidSearchSemanticRatio { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "the value of `semanticRatio` is invalid, expected a value between `0.0` and `1.0`." + "the value of `semanticRatio` is invalid, expected a float between `0.0` and `1.0`." ) } } diff --git a/meilisearch/src/routes/indexes/search.rs b/meilisearch/src/routes/indexes/search.rs index ad7f0dc89..b8db20da4 100644 --- a/meilisearch/src/routes/indexes/search.rs +++ b/meilisearch/src/routes/indexes/search.rs @@ -78,7 +78,28 @@ pub struct SearchQueryGet { #[deserr(default, error = DeserrQueryParamError)] pub hybrid_embedder: Option, #[deserr(default, error = DeserrQueryParamError)] - pub hybrid_semantic_ratio: Option, + pub hybrid_semantic_ratio: Option, +} + +#[derive(Debug, Clone, Copy, Default, PartialEq, deserr::Deserr)] +#[deserr(try_from(String) = TryFrom::try_from -> InvalidSearchSemanticRatio)] +pub struct SemanticRatioGet(SemanticRatio); + +impl std::convert::TryFrom for SemanticRatioGet { + type Error = InvalidSearchSemanticRatio; + + fn try_from(s: String) -> Result { + let f: f32 = s.parse().map_err(|_| InvalidSearchSemanticRatio)?; + Ok(SemanticRatioGet(SemanticRatio::try_from(f)?)) + } +} + +impl std::ops::Deref for SemanticRatioGet { + type Target = SemanticRatio; + + fn deref(&self) -> &Self::Target { + &self.0 + } } impl From for SearchQuery { @@ -93,13 +114,15 @@ impl From for SearchQuery { let hybrid = match (other.hybrid_embedder, other.hybrid_semantic_ratio) { (None, None) => None, - (None, Some(semantic_ratio)) => Some(HybridQuery { semantic_ratio, embedder: None }), + (None, Some(semantic_ratio)) => { + Some(HybridQuery { semantic_ratio: *semantic_ratio, embedder: None }) + } (Some(embedder), None) => Some(HybridQuery { semantic_ratio: DEFAULT_SEMANTIC_RATIO(), embedder: Some(embedder), }), (Some(embedder), Some(semantic_ratio)) => { - Some(HybridQuery { semantic_ratio, embedder: Some(embedder) }) + Some(HybridQuery { semantic_ratio: *semantic_ratio, embedder: Some(embedder) }) } }; diff --git a/meilisearch/src/search.rs b/meilisearch/src/search.rs index 674b6e25e..27a6efab4 100644 --- a/meilisearch/src/search.rs +++ b/meilisearch/src/search.rs @@ -100,6 +100,7 @@ pub struct HybridQuery { #[derive(Debug, Clone, Copy, Default, PartialEq, Deserr)] #[deserr(try_from(f32) = TryFrom::try_from -> InvalidSearchSemanticRatio)] pub struct SemanticRatio(f32); + impl std::convert::TryFrom for SemanticRatio { type Error = InvalidSearchSemanticRatio; @@ -383,7 +384,9 @@ fn prepare_search<'t>( } if let Some(ref query) = query.q { + // if !matches!(query.hybrid, query.) { search.query(query); + // } } if let Some(ref searchable) = query.attributes_to_search_on { diff --git a/meilisearch/tests/search/hybrid.rs b/meilisearch/tests/search/hybrid.rs index e5f34bcd6..578667244 100644 --- a/meilisearch/tests/search/hybrid.rs +++ b/meilisearch/tests/search/hybrid.rs @@ -68,7 +68,7 @@ async fn simple_search() { ) .await; snapshot!(code, @"200 OK"); - snapshot!(response["hits"], @r###"[{"title":"Captain Marvel","desc":"a Shazam ersatz","id":"3","_vectors":{"default":[2.0,3.0]},"_semanticScore":0.99029034},{"title":"Captain Planet","desc":"He's not part of the Marvel Cinematic Universe","id":"2","_vectors":{"default":[1.0,2.0]},"_semanticScore":0.97434163},{"title":"Shazam!","desc":"a Captain Marvel ersatz","id":"1","_vectors":{"default":[1.0,3.0]},"_semanticScore":0.9472136}]"###); + snapshot!(response["hits"], @r###"[{"title":"Captain Planet","desc":"He's not part of the Marvel Cinematic Universe","id":"2","_vectors":{"default":[1.0,2.0]},"_semanticScore":0.97434163},{"title":"Captain Marvel","desc":"a Shazam ersatz","id":"3","_vectors":{"default":[2.0,3.0]},"_semanticScore":0.99029034},{"title":"Shazam!","desc":"a Captain Marvel ersatz","id":"1","_vectors":{"default":[1.0,3.0]},"_semanticScore":0.9472136}]"###); let (response, code) = index .search_post( @@ -92,7 +92,7 @@ async fn invalid_semantic_ratio() { snapshot!(code, @"400 Bad Request"); snapshot!(response, @r###" { - "message": "Invalid value at `.hybrid.semanticRatio`: the value of `semanticRatio` is invalid, expected a value between `0.0` and `1.0`.", + "message": "Invalid value at `.hybrid.semanticRatio`: the value of `semanticRatio` is invalid, expected a float between `0.0` and `1.0`.", "code": "invalid_search_semantic_ratio", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio" @@ -107,7 +107,7 @@ async fn invalid_semantic_ratio() { snapshot!(code, @"400 Bad Request"); snapshot!(response, @r###" { - "message": "Invalid value at `.hybrid.semanticRatio`: the value of `semanticRatio` is invalid, expected a value between `0.0` and `1.0`.", + "message": "Invalid value at `.hybrid.semanticRatio`: the value of `semanticRatio` is invalid, expected a float between `0.0` and `1.0`.", "code": "invalid_search_semantic_ratio", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio" @@ -125,7 +125,7 @@ async fn invalid_semantic_ratio() { snapshot!(code, @"400 Bad Request"); snapshot!(response, @r###" { - "message": "Invalid value type for parameter `hybridSemanticRatio`: expected a string, but found a string: `1.2`", + "message": "Invalid value in parameter `hybridSemanticRatio`: the value of `semanticRatio` is invalid, expected a float between `0.0` and `1.0`.", "code": "invalid_search_semantic_ratio", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio" @@ -143,7 +143,7 @@ async fn invalid_semantic_ratio() { snapshot!(code, @"400 Bad Request"); snapshot!(response, @r###" { - "message": "Invalid value type for parameter `hybridSemanticRatio`: expected a string, but found a string: `-0.2`", + "message": "Invalid value in parameter `hybridSemanticRatio`: the value of `semanticRatio` is invalid, expected a float between `0.0` and `1.0`.", "code": "invalid_search_semantic_ratio", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio"