From feb12a581ec871568f8b813da0e9fe62fe976d03 Mon Sep 17 00:00:00 2001 From: mpostma Date: Sat, 27 Jun 2020 10:30:27 +0200 Subject: [PATCH 1/2] fix distinct attribute returning id instead of name --- meilisearch-http/src/routes/setting.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index ea35470ae..299bbc757 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -110,14 +110,14 @@ async fn get_all( println!("{:?}", attributes_for_faceting); - let searchable_attributes = schema.clone().map(|s| { + let searchable_attributes = schema.as_ref().map(|s| { s.indexed_name() .iter() .map(|s| s.to_string()) .collect::>() }); - let displayed_attributes = schema.clone().map(|s| { + let displayed_attributes = schema.as_ref().map(|s| { s.displayed_name() .iter() .map(|s| s.to_string()) @@ -253,7 +253,12 @@ async fn get_distinct( .open_index(&path.index_uid) .ok_or(Error::index_not_found(&path.index_uid))?; let reader = data.db.main_read_txn()?; - let distinct_attribute = index.main.distinct_attribute(&reader)?; + let distinct_attribute_id = index.main.distinct_attribute(&reader)?; + let schema = index.main.schema(&reader)?; + let distinct_attribute = match (schema, distinct_attribute_id) { + (Some(schema), Some(id)) => schema.name(id).map(str::to_string), + _ => None, + }; Ok(HttpResponse::Ok().json(distinct_attribute)) } From b1272d05b4873eb55170c779b89ebc9a36c6f584 Mon Sep 17 00:00:00 2001 From: mpostma Date: Sat, 27 Jun 2020 10:38:08 +0200 Subject: [PATCH 2/2] Test get distinct attribute --- meilisearch-http/tests/search_settings.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/meilisearch-http/tests/search_settings.rs b/meilisearch-http/tests/search_settings.rs index 1c8237ea8..16481d0f6 100644 --- a/meilisearch-http/tests/search_settings.rs +++ b/meilisearch-http/tests/search_settings.rs @@ -677,3 +677,17 @@ async fn search_with_settings_searchable_attributes_2() { let (response, _status_code) = server.search_get(query).await; assert_json_eq!(expect, response["hits"].clone(), ordered: false); } + +// issue #798 +#[actix_rt::test] +async fn distinct_attributes_returns_name_not_id() { + let mut server = common::Server::test_server().await; + let settings = json!({ + "distinctAttribute": "color", + }); + server.update_all_settings(settings).await; + let (response, _) = server.get_all_settings().await; + assert_eq!(response["distinctAttribute"], "color"); + let (response, _) = server.get_distinct_attribute().await; + assert_eq!(response, "color"); +}