Merge pull request #800 from MarinPostma/distinct-attribute-return-correct-name

Fix distinct attribute returning id instead of name
This commit is contained in:
Clément Renault 2020-06-29 10:42:57 +02:00 committed by GitHub
commit 21567eeb8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -110,14 +110,14 @@ async fn get_all(
println!("{:?}", attributes_for_faceting); println!("{:?}", attributes_for_faceting);
let searchable_attributes = schema.clone().map(|s| { let searchable_attributes = schema.as_ref().map(|s| {
s.indexed_name() s.indexed_name()
.iter() .iter()
.map(|s| s.to_string()) .map(|s| s.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
}); });
let displayed_attributes = schema.clone().map(|s| { let displayed_attributes = schema.as_ref().map(|s| {
s.displayed_name() s.displayed_name()
.iter() .iter()
.map(|s| s.to_string()) .map(|s| s.to_string())
@ -253,7 +253,12 @@ async fn get_distinct(
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(Error::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; 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)) Ok(HttpResponse::Ok().json(distinct_attribute))
} }

View File

@ -677,3 +677,17 @@ async fn search_with_settings_searchable_attributes_2() {
let (response, _status_code) = server.search_get(query).await; let (response, _status_code) = server.search_get(query).await;
assert_json_eq!(expect, response["hits"].clone(), ordered: false); 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");
}