diff --git a/meilisearch-core/src/store/main.rs b/meilisearch-core/src/store/main.rs index f1ef6fc5e..9d5e95425 100644 --- a/meilisearch-core/src/store/main.rs +++ b/meilisearch-core/src/store/main.rs @@ -286,15 +286,15 @@ impl Main { Ok(self.main.delete::<_, Str>(writer, RANKING_RULES_KEY)?) } - pub fn distinct_attribute(&self, reader: &heed::RoTxn) -> MResult> { - if let Some(value) = self.main.get::<_, Str, Str>(reader, DISTINCT_ATTRIBUTE_KEY)? { - return Ok(Some(value.to_owned())) + pub fn distinct_attribute(&self, reader: &heed::RoTxn) -> MResult> { + if let Some(value) = self.main.get::<_, Str, OwnedType>(reader, DISTINCT_ATTRIBUTE_KEY)? { + return Ok(Some(FieldId(value.to_owned()))) } return Ok(None) } - pub fn put_distinct_attribute(self, writer: &mut heed::RwTxn, value: &str) -> MResult<()> { - Ok(self.main.put::<_, Str, Str>(writer, DISTINCT_ATTRIBUTE_KEY, value)?) + pub fn put_distinct_attribute(self, writer: &mut heed::RwTxn, value: FieldId) -> MResult<()> { + Ok(self.main.put::<_, Str, OwnedType>(writer, DISTINCT_ATTRIBUTE_KEY, &value.0)?) } pub fn delete_distinct_attribute(self, writer: &mut heed::RwTxn) -> MResult { diff --git a/meilisearch-core/src/update/settings_update.rs b/meilisearch-core/src/update/settings_update.rs index fbf266001..9aecb23f7 100644 --- a/meilisearch-core/src/update/settings_update.rs +++ b/meilisearch-core/src/update/settings_update.rs @@ -59,7 +59,8 @@ pub fn apply_settings_update( match settings.distinct_attribute { UpdateState::Update(v) => { - index.main.put_distinct_attribute(writer, &v)?; + let field_id = schema.insert(&v)?; + index.main.put_distinct_attribute(writer, field_id)?; }, UpdateState::Clear => { index.main.delete_distinct_attribute(writer)?; diff --git a/meilisearch-http/src/helpers/meilisearch.rs b/meilisearch-http/src/helpers/meilisearch.rs index 65b6da08f..f9183d013 100644 --- a/meilisearch-http/src/helpers/meilisearch.rs +++ b/meilisearch-http/src/helpers/meilisearch.rs @@ -139,19 +139,17 @@ impl<'a> SearchBuilder<'a> { } if let Some(field) = self.index.main.distinct_attribute(reader)? { - if let Some(field_id) = schema.id(&field) { - let index = &self.index; - query_builder.with_distinct(1, move |id| { - match index.document_attribute_bytes(reader, id, field_id) { - Ok(Some(bytes)) => { - let mut s = SipHasher::new(); - bytes.hash(&mut s); - Some(s.finish()) - } - _ => None, + let index = &self.index; + query_builder.with_distinct(1, move |id| { + match index.document_attribute_bytes(reader, id, field) { + Ok(Some(bytes)) => { + let mut s = SipHasher::new(); + bytes.hash(&mut s); + Some(s.finish()) } - }); - } + _ => None, + } + }); } query_builder.set_facet_filter(self.facet_filters); diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 37fe5bda2..ea35470ae 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -89,10 +89,14 @@ async fn get_all( .map(|r| r.to_string()) .collect(); - let distinct_attribute = index.main.distinct_attribute(&reader)?; let schema = index.main.schema(&reader)?; + let distinct_attribute = match (index.main.distinct_attribute(&reader)?, &schema) { + (Some(id), Some(schema)) => schema.name(id).map(str::to_string), + _ => None, + }; + let attributes_for_faceting = match (&schema, &index.main.attributes_for_faceting(&reader)?) { (Some(schema), Some(attrs)) => { attrs