From 7c7f753463e8880e25a76e165e495f84a5353c6c Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 27 May 2020 19:45:23 +0200 Subject: [PATCH] add facet count in response --- meilisearch-core/src/bucket_sort.rs | 6 +++--- meilisearch-http/src/helpers/meilisearch.rs | 3 +++ meilisearch-http/tests/search.rs | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/meilisearch-core/src/bucket_sort.rs b/meilisearch-core/src/bucket_sort.rs index b991745ba..a4bce2503 100644 --- a/meilisearch-core/src/bucket_sort.rs +++ b/meilisearch-core/src/bucket_sort.rs @@ -30,7 +30,7 @@ pub struct SortResult { pub nb_hits: usize, pub exhaustive_nb_hit: bool, pub facets: Option>>, - pub exhaustive_facet_count: Option, + pub exhaustive_facets_count: Option, } pub fn bucket_sort<'c, FI>( @@ -118,7 +118,7 @@ where if let Some(f) = facet_count_docids { // hardcoded value, until approximation optimization - result.exhaustive_facet_count = Some(true); + result.exhaustive_facets_count = Some(true); result.facets = Some(facet_count(f, &docids)); } @@ -265,7 +265,7 @@ where if let Some(f) = facet_count_docids { // hardcoded value, until approximation optimization - result.exhaustive_facet_count = Some(true); + result.exhaustive_facets_count = Some(true); result.facets = Some(facet_count(f, &docids)); } diff --git a/meilisearch-http/src/helpers/meilisearch.rs b/meilisearch-http/src/helpers/meilisearch.rs index 6e27b03f2..b6ae2c6b8 100644 --- a/meilisearch-http/src/helpers/meilisearch.rs +++ b/meilisearch-http/src/helpers/meilisearch.rs @@ -249,6 +249,7 @@ impl<'a> SearchBuilder<'a> { processing_time_ms: time_ms, query: self.query.to_string(), facets_distribution: search_result.facets, + exhaustive_facets_count: search_result.exhaustive_facets_count, }; Ok(results) @@ -335,6 +336,8 @@ pub struct SearchResult { pub query: String, #[serde(skip_serializing_if = "Option::is_none")] pub facets_distribution: Option>>, + #[serde(skip_serializing_if = "Option::is_none")] + pub exhaustive_facets_count: Option, } /// returns the start index and the length on the crop. diff --git a/meilisearch-http/tests/search.rs b/meilisearch-http/tests/search.rs index 37f363062..559c1d0cc 100644 --- a/meilisearch-http/tests/search.rs +++ b/meilisearch-http/tests/search.rs @@ -1297,6 +1297,12 @@ async fn test_faceted_search_invalid() { async fn test_facet_count() { let mut server = common::Server::test_server().await; + // test without facet distribution + let query = "q=a"; + let (response, _status_code) = server.search(query).await; + assert!(response.get("exhaustiveFacetsCount").is_none()); + assert!(response.get("facetsDistribution").is_none()); + // test no facets set, search on color let query = "q=a&facetsDistribution=%5B%22color%22%5D"; let (_response, status_code) = server.search(query).await; @@ -1308,6 +1314,7 @@ async fn test_facet_count() { server.update_all_settings(body).await; // same as before, but now facets are set: let (response, _status_code) = server.search(query).await; + assert!(response.get("exhaustiveFacetsCount").is_some()); assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 1); // searching on color and tags let query = "q=a&facetsDistribution=%5B%22color%22,%20%22tags%22%5D";