From df749d424c2b25ee21e09293e7c3ea55fd3aa356 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Tue, 6 Jun 2023 18:15:42 +0200 Subject: [PATCH] add virtual conditions to fid and position to always have the max cost --- .../search/new/ranking_rule_graph/fid/mod.rs | 27 ++++++++++++++++++- .../new/ranking_rule_graph/position/mod.rs | 8 ++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/milli/src/search/new/ranking_rule_graph/fid/mod.rs b/milli/src/search/new/ranking_rule_graph/fid/mod.rs index 0f2cceaec..d4f95050a 100644 --- a/milli/src/search/new/ranking_rule_graph/fid/mod.rs +++ b/milli/src/search/new/ranking_rule_graph/fid/mod.rs @@ -68,7 +68,7 @@ impl RankingRuleGraphTrait for FidGraph { } let mut edges = vec![]; - for fid in all_fields { + for fid in all_fields.iter().copied() { // TODO: We can improve performances and relevancy by storing // the term subsets associated to each field ids fetched. edges.push(( @@ -80,6 +80,31 @@ impl RankingRuleGraphTrait for FidGraph { )); } + // always lookup the max_fid if we don't already and add an artificial condition for max scoring + let max_fid: Option = { + if let Some(max_fid) = ctx + .index + .searchable_fields_ids(ctx.txn)? + .map(|field_ids| field_ids.into_iter().max()) + { + max_fid + } else { + ctx.index.fields_ids_map(ctx.txn)?.ids().max() + } + }; + + if let Some(max_fid) = max_fid { + if !all_fields.contains(&max_fid) { + edges.push(( + max_fid as u32 * term.term_ids.len() as u32, // TODO improve the fid score i.e. fid^10. + conditions_interner.insert(FidCondition { + term: term.clone(), // TODO remove this ugly clone + fid: max_fid, + }), + )); + } + } + Ok(edges) } } diff --git a/milli/src/search/new/ranking_rule_graph/position/mod.rs b/milli/src/search/new/ranking_rule_graph/position/mod.rs index 9b0b6478f..4c04b9684 100644 --- a/milli/src/search/new/ranking_rule_graph/position/mod.rs +++ b/milli/src/search/new/ranking_rule_graph/position/mod.rs @@ -105,6 +105,14 @@ impl RankingRuleGraphTrait for PositionGraph { )); } + // artificial empty condition for computing max cost + let max_cost = term.term_ids.len() as u32 * 10; + edges.push(( + max_cost, + conditions_interner + .insert(PositionCondition { term: term.clone(), positions: Vec::default() }), + )); + Ok(edges) } }