Repository: meilisearch/meilisearch
File: milli/src/index.rs (Rust)

Code before:
        rtxn: &RoTxn,
        key: &(Script, Language),
    ) -> heed::Result<Option<RoaringBitmap>> {
        let soft_deleted_documents = self.soft_deleted_documents_ids(rtxn)?;
        let doc_ids = self.script_language_docids.get(rtxn, key)?;
        Ok(doc_ids.map(|ids| ids - soft_deleted_documents))
    }

    pub fn script_language(&self, rtxn: &RoTxn) -> heed::Result<HashMap<Script, Vec<Language>>> {
        let soft_deleted_documents = self.soft_deleted_documents_ids(rtxn)?;

        let mut script_language: HashMap<Script, Vec<Language>> = HashMap::new();
        let mut script_language_doc_count: Vec<(Script, Language, u64)> = Vec::new();
        let mut total = 0;
        for sl in self.script_language_docids.iter(rtxn)? {
            let ((script, language), docids) = sl?;

            // keep only Languages that contains at least 1 document.
            let remaining_documents_count = (docids - &soft_deleted_documents).len();
            total += remaining_documents_count;
            if remaining_documents_count > 0 {
                script_language_doc_count.push((script, language, remaining_documents_count));
            }
        }

        let threshold = total / 20; // 5% (arbitrar)
        for (script, language, count) in script_language_doc_count {
            if count > threshold {
                if let Some(languages) = script_language.get_mut(&script) {
                    (*languages).push(language);
                } else {
                    script_language.insert(script, vec![language]);
                }
            }
        }

        Ok(script_language)
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use std::collections::HashSet;
    use std::ops::Deref;

    use big_s::S;
    use heed::{EnvOpenOptions, RwTxn};
    use maplit::hashset;
    use tempfile::TempDir;

    use crate::documents::DocumentsBatchReader;

Review comment (suggestion):
```suggestion
        let threshold = total / 20; // 5% (arbitrary)
```

Code after:
        rtxn: &RoTxn,
        key: &(Script, Language),
    ) -> heed::Result<Option<RoaringBitmap>> {
        let soft_deleted_documents = self.soft_deleted_documents_ids(rtxn)?;
        let doc_ids = self.script_language_docids.get(rtxn, key)?;
        Ok(doc_ids.map(|ids| ids - soft_deleted_documents))
    }

    pub fn script_language(&self, rtxn: &RoTxn) -> heed::Result<HashMap<Script, Vec<Language>>> {
        let soft_deleted_documents = self.soft_deleted_documents_ids(rtxn)?;

        let mut script_language: HashMap<Script, Vec<Language>> = HashMap::new();
        let mut script_language_doc_count: Vec<(Script, Language, u64)> = Vec::new();
        let mut total = 0;
        for sl in self.script_language_docids.iter(rtxn)? {
            let ((script, language), docids) = sl?;

            // keep only Languages that contains at least 1 document.
            let remaining_documents_count = (docids - &soft_deleted_documents).len();
            total += remaining_documents_count;
            if remaining_documents_count > 0 {
                script_language_doc_count.push((script, language, remaining_documents_count));
            }
        }

        let threshold = total / 20; // 5% (arbitrary)
        for (script, language, count) in script_language_doc_count {
            if count > threshold {
                if let Some(languages) = script_language.get_mut(&script) {
                    (*languages).push(language);
                } else {
                    script_language.insert(script, vec![language]);
                }
            }
        }

        Ok(script_language)
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use std::collections::HashSet;
    use std::ops::Deref;

    use big_s::S;
    use heed::{EnvOpenOptions, RwTxn};
    use maplit::hashset;
    use tempfile::TempDir;

    use crate::documents::DocumentsBatchReader;