From 5ceddbda8475caa4808d74f46cc4639f4af6653b Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Thu, 6 Mar 2025 13:58:28 +0100 Subject: [PATCH] Add the max_weight of the weight map if it's lacking --- crates/milli/src/fieldids_weights_map.rs | 5 ----- .../milli/src/search/new/ranking_rule_graph/fid/mod.rs | 10 +++++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/milli/src/fieldids_weights_map.rs b/crates/milli/src/fieldids_weights_map.rs index f23bc1512..57c99f77f 100644 --- a/crates/milli/src/fieldids_weights_map.rs +++ b/crates/milli/src/fieldids_weights_map.rs @@ -48,11 +48,6 @@ impl FieldidsWeightsMap { self.map.values().copied().max() } - /// Returns the field id with the highest weight. - pub fn max_weight_fid(&self) -> Option<(FieldId, Weight)> { - self.map.iter().max_by_key(|(_, weight)| *weight).map(|(fid, weight)| (*fid, *weight)) - } - /// Return an iterator visiting all field ids in arbitrary order. pub fn ids(&self) -> impl Iterator + '_ { self.map.keys().copied() diff --git a/crates/milli/src/search/new/ranking_rule_graph/fid/mod.rs b/crates/milli/src/search/new/ranking_rule_graph/fid/mod.rs index 62d75d2ac..f424b7241 100644 --- a/crates/milli/src/search/new/ranking_rule_graph/fid/mod.rs +++ b/crates/milli/src/search/new/ranking_rule_graph/fid/mod.rs @@ -57,6 +57,7 @@ impl RankingRuleGraphTrait for FidGraph { let term = to_term; let mut all_fields = FxHashSet::default(); + let mut current_max_weight = 0; for word in term.term_subset.all_single_words_except_prefix_db(ctx)? { let fields = ctx.get_db_word_fids(word.interned())?; all_fields.extend(fields); @@ -81,6 +82,9 @@ impl RankingRuleGraphTrait for FidGraph { let weight = weights_map .weight(fid) .ok_or(InternalError::FieldidsWeightsMapMissingEntry { key: fid })?; + if weight > current_max_weight { + current_max_weight = weight; + } edges.push(( weight as u32 * term.term_ids.len() as u32, conditions_interner.insert(FidCondition { term: term.clone(), fid: Some(fid) }), @@ -88,10 +92,10 @@ impl RankingRuleGraphTrait for FidGraph { } // always lookup the max_fid if we don't already and add an artificial condition for max scoring - let max_weight_fid = weights_map.max_weight_fid(); + let max_weight = weights_map.max_weight(); - if let Some((max_fid, max_weight)) = max_weight_fid { - if !all_fields.contains(&max_fid) { + if let Some(max_weight) = max_weight { + if current_max_weight < max_weight { edges.push(( max_weight as u32 * term.term_ids.len() as u32, // TODO improve the fid score i.e. fid^10. conditions_interner.insert(FidCondition {