Add the max_weight of the weight map if it's lacking

This commit is contained in:
ManyTheFish 2025-03-06 13:58:28 +01:00
parent ca41ce3bbd
commit 5ceddbda84
2 changed files with 7 additions and 8 deletions

View File

@ -48,11 +48,6 @@ impl FieldidsWeightsMap {
self.map.values().copied().max() 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. /// Return an iterator visiting all field ids in arbitrary order.
pub fn ids(&self) -> impl Iterator<Item = FieldId> + '_ { pub fn ids(&self) -> impl Iterator<Item = FieldId> + '_ {
self.map.keys().copied() self.map.keys().copied()

View File

@ -57,6 +57,7 @@ impl RankingRuleGraphTrait for FidGraph {
let term = to_term; let term = to_term;
let mut all_fields = FxHashSet::default(); 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)? { for word in term.term_subset.all_single_words_except_prefix_db(ctx)? {
let fields = ctx.get_db_word_fids(word.interned())?; let fields = ctx.get_db_word_fids(word.interned())?;
all_fields.extend(fields); all_fields.extend(fields);
@ -81,6 +82,9 @@ impl RankingRuleGraphTrait for FidGraph {
let weight = weights_map let weight = weights_map
.weight(fid) .weight(fid)
.ok_or(InternalError::FieldidsWeightsMapMissingEntry { key: fid })?; .ok_or(InternalError::FieldidsWeightsMapMissingEntry { key: fid })?;
if weight > current_max_weight {
current_max_weight = weight;
}
edges.push(( edges.push((
weight as u32 * term.term_ids.len() as u32, weight as u32 * term.term_ids.len() as u32,
conditions_interner.insert(FidCondition { term: term.clone(), fid: Some(fid) }), 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 // 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 let Some(max_weight) = max_weight {
if !all_fields.contains(&max_fid) { if current_max_weight < max_weight {
edges.push(( edges.push((
max_weight as u32 * term.term_ids.len() as u32, // TODO improve the fid score i.e. fid^10. max_weight as u32 * term.term_ids.len() as u32, // TODO improve the fid score i.e. fid^10.
conditions_interner.insert(FidCondition { conditions_interner.insert(FidCondition {