From ff440c1d9d28e60e682f0352bd9c2b2b789a01a7 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 1 Jun 2021 12:20:29 +0200 Subject: [PATCH] Introduce the faceted fields method to retrieve those that needs faceting --- milli/src/index.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/milli/src/index.rs b/milli/src/index.rs index 9a52188dc..9cfcd841c 100644 --- a/milli/src/index.rs +++ b/milli/src/index.rs @@ -357,8 +357,29 @@ impl Index { Ok(filterable_fields) } + + /* faceted documents ids */ + + /// Returns the faceted fields names. + /// + /// Faceted fields are the union of all the filterable, distinct, and Asc/Desc fields. pub fn faceted_fields(&self, rtxn: &RoTxn) -> heed::Result> { - Ok(self.main.get::<_, Str, SerdeJson<_>>(rtxn, FACETED_FIELDS_KEY)?.unwrap_or_default()) + let filterable_fields = self.filterable_fields(rtxn)?; + let distinct_field = self.distinct_attribute(rtxn)?; + let asc_desc_fields = self.criteria(rtxn)? + .into_iter() + .filter_map(|criterion| match criterion { + Criterion::Asc(field) | Criterion::Desc(field) => Some(field), + _otherwise => None, + }); + + let mut faceted_fields = filterable_fields; + faceted_fields.extend(asc_desc_fields); + if let Some(field) = distinct_field { + faceted_fields.insert(field.to_owned()); + } + + Ok(faceted_fields) } /// Same as `faceted_fields`, but returns ids instead.