From c8eeb5cdc8d0113157c37f02171e1195d4d76aa1 Mon Sep 17 00:00:00 2001 From: Charlotte Vermandel Date: Mon, 25 Apr 2022 22:35:56 +0200 Subject: [PATCH] Add test to ensure nested field support --- src/search.rs | 94 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/src/search.rs b/src/search.rs index baad8b3b..04db41ac 100644 --- a/src/search.rs +++ b/src/search.rs @@ -283,13 +283,19 @@ mod tests { use crate::{client::*, search::*}; use meilisearch_test_macro::meilisearch_test; use serde::{Deserialize, Serialize}; - use serde_json::{Map, Value, json}; + use serde_json::{json, Map, Value}; + + #[derive(Debug, Serialize, Deserialize, PartialEq)] + struct Nested { + child: String, + } #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Document { id: usize, value: String, kind: String, + nested: Nested, } impl PartialEq> for Document { @@ -302,16 +308,16 @@ mod tests { async fn setup_test_index(client: &Client, index: &Index) -> Result<(), Error> { let t0 = index.add_documents(&[ - Document { id: 0, kind: "text".into(), value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".to_string() }, - Document { id: 1, kind: "text".into(), value: "dolor sit amet, consectetur adipiscing elit".to_string() }, - Document { id: 2, kind: "title".into(), value: "The Social Network".to_string() }, - Document { id: 3, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string() }, - Document { id: 4, kind: "title".into(), value: "Harry Potter and the Chamber of Secrets".to_string() }, - Document { id: 5, kind: "title".into(), value: "Harry Potter and the Prisoner of Azkaban".to_string() }, - Document { id: 6, kind: "title".into(), value: "Harry Potter and the Goblet of Fire".to_string() }, - Document { id: 7, kind: "title".into(), value: "Harry Potter and the Order of the Phoenix".to_string() }, - Document { id: 8, kind: "title".into(), value: "Harry Potter and the Half-Blood Prince".to_string() }, - Document { id: 9, kind: "title".into(), value: "Harry Potter and the Deathly Hallows".to_string() }, + Document { id: 0, kind: "text".into(), value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".to_string(), nested: Nested { child: "first".to_string() } }, + Document { id: 1, kind: "text".into(), value: "dolor sit amet, consectetur adipiscing elit".to_string(), nested: Nested { child: "second".to_string() } }, + Document { id: 2, kind: "title".into(), value: "The Social Network".to_string(), nested: Nested { child: "third".to_string() } }, + Document { id: 3, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string(), nested: Nested { child: "fourth".to_string() } }, + Document { id: 4, kind: "title".into(), value: "Harry Potter and the Chamber of Secrets".to_string(), nested: Nested { child: "fift".to_string() } }, + Document { id: 5, kind: "title".into(), value: "Harry Potter and the Prisoner of Azkaban".to_string(), nested: Nested { child: "sixth".to_string() } }, + Document { id: 6, kind: "title".into(), value: "Harry Potter and the Goblet of Fire".to_string(), nested: Nested { child: "seventh".to_string() } }, + Document { id: 7, kind: "title".into(), value: "Harry Potter and the Order of the Phoenix".to_string(), nested: Nested { child: "eighth".to_string() } }, + Document { id: 8, kind: "title".into(), value: "Harry Potter and the Half-Blood Prince".to_string(), nested: Nested { child: "ninth".to_string() } }, + Document { id: 9, kind: "title".into(), value: "Harry Potter and the Deathly Hallows".to_string(), nested: Nested { child: "tenth".to_string() } }, ], None).await?; let t1 = index.set_filterable_attributes(["kind", "value"]).await?; let t2 = index.set_sortable_attributes(["title"]).await?; @@ -332,6 +338,28 @@ mod tests { Ok(()) } + #[meilisearch_test] + async fn test_query_string_on_nested_field(client: Client, index: Index) -> Result<(), Error> { + setup_test_index(&client, &index).await?; + + let results: SearchResults = + index.search().with_query("second").execute().await?; + + assert_eq!( + &Document { + id: 1, + value: "dolor sit amet, consectetur adipiscing elit".to_string(), + kind: "text".to_string(), + nested: Nested { + child: "second".to_string() + } + }, + &results.hits[0].result + ); + + Ok(()) + } + #[meilisearch_test] async fn test_query_limit(client: Client, index: Index) -> Result<(), Error> { setup_test_index(&client, &index).await?; @@ -457,7 +485,10 @@ mod tests { id: 0, value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do…" .to_string(), - kind: "text".to_string() + kind: "text".to_string(), + nested: Nested { + child: "first".to_string() + } }, results.hits[0].formatted_result.as_ref().unwrap() ); @@ -470,7 +501,10 @@ mod tests { &Document { id: 0, value: "Lorem ipsum dolor sit amet…".to_string(), - kind: "text".to_string() + kind: "text".to_string(), + nested: Nested { + child: "first".to_string() + } }, results.hits[0].formatted_result.as_ref().unwrap() ); @@ -490,6 +524,7 @@ mod tests { id: 0, value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".to_string(), kind: "text".to_string(), + nested: Nested { child: "first".to_string() } }, results.hits[0].formatted_result.as_ref().unwrap()); @@ -502,7 +537,10 @@ mod tests { &Document { id: 0, value: "Lorem ipsum dolor sit amet…".to_string(), - kind: "text".to_string() + kind: "text".to_string(), + nested: Nested { + child: "first".to_string() + } }, results.hits[0].formatted_result.as_ref().unwrap() ); @@ -521,7 +559,10 @@ mod tests { &Document { id: 1, value: "dolor sit amet, consectetur adipiscing elit".to_string(), - kind: "text".to_string() + kind: "text".to_string(), + nested: Nested { + child: "first".to_string() + } }, results.hits[0].formatted_result.as_ref().unwrap(), ); @@ -534,7 +575,10 @@ mod tests { &Document { id: 1, value: "dolor sit amet, consectetur adipiscing elit".to_string(), - kind: "text".to_string() + kind: "text".to_string(), + nested: Nested { + child: "first".to_string() + } }, results.hits[0].formatted_result.as_ref().unwrap() ); @@ -577,15 +621,20 @@ mod tests { } #[meilisearch_test] - async fn test_generate_tenant_token_from_client(client: Client, index: Index) -> Result<(), Error> { - use crate::key::{KeyBuilder, Action}; + async fn test_generate_tenant_token_from_client( + client: Client, + index: Index, + ) -> Result<(), Error> { + use crate::key::{Action, KeyBuilder}; setup_test_index(&client, &index).await?; let key = KeyBuilder::new("key for generate_tenant_token test") .with_action(Action::All) .with_index("*") - .create(&client).await.unwrap(); + .create(&client) + .await + .unwrap(); let allowed_client = Client::new("http://localhost:7700", key.key); let search_rules = vec![ @@ -597,10 +646,13 @@ mod tests { ]; for rules in search_rules { - let token = allowed_client.generate_tenant_token(rules, None, None).expect("Cannot generate tenant token."); + let token = allowed_client + .generate_tenant_token(rules, None, None) + .expect("Cannot generate tenant token."); let new_client = Client::new("http://localhost:7700", token); - let result: SearchResults = new_client.index(index.uid.to_string()) + let result: SearchResults = new_client + .index(index.uid.to_string()) .search() .execute() .await?;