From 6bf07d969e71b7661970bbcdcef4f2611c3a19dd Mon Sep 17 00:00:00 2001 From: Tamo Date: Thu, 13 Jun 2024 15:49:42 +0200 Subject: [PATCH] add failing test --- meilisearch/tests/search/distinct.rs | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/meilisearch/tests/search/distinct.rs b/meilisearch/tests/search/distinct.rs index aea98215d..68f7f18e8 100644 --- a/meilisearch/tests/search/distinct.rs +++ b/meilisearch/tests/search/distinct.rs @@ -107,6 +107,39 @@ static DOCUMENTS: Lazy = Lazy::new(|| { ]) }); +static NESTED_DOCUMENTS: Lazy = Lazy::new(|| { + json!([ + { + "id": 1, + "description": "Leather Jacket", + "brand": "Lee Jeans", + "product_id": "123456", + "color": { "main": "Brown", "pattern": "stripped" }, + }, + { + "id": 2, + "description": "Leather Jacket", + "brand": "Lee Jeans", + "product_id": "123456", + "color": { "main": "Black", "pattern": "stripped" }, + }, + { + "id": 3, + "description": "Leather Jacket", + "brand": "Lee Jeans", + "product_id": "123456", + "color": { "main": "Blue", "pattern": "used" }, + }, + { + "id": 4, + "description": "T-Shirt", + "brand": "Nike", + "product_id": "789012", + "color": { "main": "Blue", "pattern": "stripped" }, + } + ]) +}); + static DOCUMENT_PRIMARY_KEY: &str = "id"; static DOCUMENT_DISTINCT_KEY: &str = "product_id"; @@ -239,3 +272,31 @@ async fn distinct_search_with_pagination_no_ranking() { snapshot!(response["totalPages"], @"2"); snapshot!(response["totalHits"], @"6"); } + +#[actix_rt::test] +async fn distinct_at_search_time() { + let server = Server::new().await; + let index = server.index("tamo"); + + let documents = NESTED_DOCUMENTS.clone(); + index.add_documents(documents, Some(DOCUMENT_PRIMARY_KEY)).await; + index.update_settings_filterable_attributes(json!(["color"])).await; + index.wait_task(1).await; + + fn get_hits(response: &Value) -> Vec<&str> { + let hits_array = response["hits"] + .as_array() + .unwrap_or_else(|| panic!("{}", &serde_json::to_string_pretty(&response).unwrap())); + hits_array.iter().map(|h| h[DOCUMENT_DISTINCT_KEY].as_str().unwrap()).collect::>() + } + + let (response, code) = + index.search_post(json!({"page": 0, "hitsPerPage": 2, "distinct": "color.main"})).await; + let hits = get_hits(&response); + snapshot!(code, @"200 OK"); + snapshot!(hits.len(), @"0"); + snapshot!(format!("{:?}", hits), @r#"[]"#); + snapshot!(response["page"], @"0"); + snapshot!(response["totalPages"], @"3"); + snapshot!(response["totalHits"], @"6"); +}