From ac68f3319449921fc7c28ed7d65f5cd6bb0ad12b Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Wed, 13 Dec 2023 11:47:12 +0100 Subject: [PATCH] Add simple test --- meilisearch/tests/search/hybrid.rs | 152 +++++++++++++++++++++++++++++ meilisearch/tests/search/mod.rs | 1 + 2 files changed, 153 insertions(+) create mode 100644 meilisearch/tests/search/hybrid.rs diff --git a/meilisearch/tests/search/hybrid.rs b/meilisearch/tests/search/hybrid.rs new file mode 100644 index 000000000..e5f34bcd6 --- /dev/null +++ b/meilisearch/tests/search/hybrid.rs @@ -0,0 +1,152 @@ +use meili_snap::{json_string, snapshot}; +use once_cell::sync::Lazy; + +use crate::common::index::Index; +use crate::common::{Server, Value}; +use crate::json; + +async fn index_with_documents<'a>(server: &'a Server, documents: &Value) -> Index<'a> { + let index = server.index("test"); + + let (response, code) = server.set_features(json!({"vectorStore": true})).await; + + meili_snap::snapshot!(code, @"200 OK"); + meili_snap::snapshot!(meili_snap::json_string!(response), @r###" + { + "scoreDetails": false, + "vectorStore": true, + "metrics": false, + "exportPuffinReports": false, + "proximityPrecision": false + } + "###); + + let (response, code) = index + .update_settings( + json!({ "embedders": {"default": {"source": {"userProvided": {"dimensions": 2}}}} }), + ) + .await; + assert_eq!(202, code, "{:?}", response); + index.wait_task(0).await; + + let (response, code) = index.add_documents(documents.clone(), None).await; + assert_eq!(202, code, "{:?}", response); + index.wait_task(1).await; + index +} + +static SIMPLE_SEARCH_DOCUMENTS: Lazy = Lazy::new(|| { + json!([ + { + "title": "Shazam!", + "desc": "a Captain Marvel ersatz", + "id": "1", + "_vectors": {"default": [1.0, 3.0]}, + }, + { + "title": "Captain Planet", + "desc": "He's not part of the Marvel Cinematic Universe", + "id": "2", + "_vectors": {"default": [1.0, 2.0]}, + }, + { + "title": "Captain Marvel", + "desc": "a Shazam ersatz", + "id": "3", + "_vectors": {"default": [2.0, 3.0]}, + }]) +}); + +#[actix_rt::test] +async fn simple_search() { + let server = Server::new().await; + let index = index_with_documents(&server, &SIMPLE_SEARCH_DOCUMENTS).await; + + let (response, code) = index + .search_post( + json!({"q": "Captain", "vector": [1.0, 1.0], "hybrid": {"semanticRatio": 0.2}}), + ) + .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}]"###); + + let (response, code) = index + .search_post( + json!({"q": "Captain", "vector": [1.0, 1.0], "hybrid": {"semanticRatio": 0.8}}), + ) + .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}]"###); +} + +#[actix_rt::test] +async fn invalid_semantic_ratio() { + let server = Server::new().await; + let index = index_with_documents(&server, &SIMPLE_SEARCH_DOCUMENTS).await; + + let (response, code) = index + .search_post( + json!({"q": "Captain", "vector": [1.0, 1.0], "hybrid": {"semanticRatio": 1.2}}), + ) + .await; + 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`.", + "code": "invalid_search_semantic_ratio", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio" + } + "###); + + let (response, code) = index + .search_post( + json!({"q": "Captain", "vector": [1.0, 1.0], "hybrid": {"semanticRatio": -0.8}}), + ) + .await; + 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`.", + "code": "invalid_search_semantic_ratio", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio" + } + "###); + + let (response, code) = index + .search_get( + &yaup::to_string( + &json!({"q": "Captain", "vector": [1.0, 1.0], "hybridSemanticRatio": 1.2}), + ) + .unwrap(), + ) + .await; + snapshot!(code, @"400 Bad Request"); + snapshot!(response, @r###" + { + "message": "Invalid value type for parameter `hybridSemanticRatio`: expected a string, but found a string: `1.2`", + "code": "invalid_search_semantic_ratio", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio" + } + "###); + + let (response, code) = index + .search_get( + &yaup::to_string( + &json!({"q": "Captain", "vector": [1.0, 1.0], "hybridSemanticRatio": -0.2}), + ) + .unwrap(), + ) + .await; + snapshot!(code, @"400 Bad Request"); + snapshot!(response, @r###" + { + "message": "Invalid value type for parameter `hybridSemanticRatio`: expected a string, but found a string: `-0.2`", + "code": "invalid_search_semantic_ratio", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_search_semantic_ratio" + } + "###); +} diff --git a/meilisearch/tests/search/mod.rs b/meilisearch/tests/search/mod.rs index ad9c2aaa2..133a143fd 100644 --- a/meilisearch/tests/search/mod.rs +++ b/meilisearch/tests/search/mod.rs @@ -6,6 +6,7 @@ mod errors; mod facet_search; mod formatted; mod geo; +mod hybrid; mod multi; mod pagination; mod restrict_searchable;