Add logger to attribute rr, fix a bug

This commit is contained in:
Loïc Lecrenier 2023-04-13 09:14:23 +02:00
parent 5acf953298
commit 8edad8291b
4 changed files with 92 additions and 24 deletions

View File

@ -50,7 +50,6 @@ fn main() -> Result<(), Box<dyn Error>> {
let docs = execute_search(
&mut ctx,
&(!query.trim().is_empty()).then(|| query.trim().to_owned()),
// what a the from which when there is
TermsMatchingStrategy::Last,
false,
&None,

View File

@ -309,13 +309,14 @@ impl<'ctx> SearchContext<'ctx> {
let fids = match self.db_cache.word_fids.entry(word) {
Entry::Occupied(fids) => fids.get().clone(),
Entry::Vacant(entry) => {
let key = self.word_interner.get(word).as_bytes();
let mut key = self.word_interner.get(word).as_bytes().to_owned();
key.push(0);
let mut fids = vec![];
let remap_key_type = self
.index
.word_fid_docids
.remap_types::<ByteSlice, ByteSlice>()
.prefix_iter(self.txn, key)?
.prefix_iter(self.txn, &key)?
.remap_key_type::<StrBEU16Codec>();
for result in remap_key_type {
let ((_, fid), value) = result?;
@ -334,13 +335,14 @@ impl<'ctx> SearchContext<'ctx> {
let fids = match self.db_cache.word_prefix_fids.entry(word_prefix) {
Entry::Occupied(fids) => fids.get().clone(),
Entry::Vacant(entry) => {
let key = self.word_interner.get(word_prefix).as_bytes();
let mut key = self.word_interner.get(word_prefix).as_bytes().to_owned();
key.push(0);
let mut fids = vec![];
let remap_key_type = self
.index
.word_prefix_fid_docids
.remap_types::<ByteSlice, ByteSlice>()
.prefix_iter(self.txn, key)?
.prefix_iter(self.txn, &key)?
.remap_key_type::<StrBEU16Codec>();
for result in remap_key_type {
let ((_, fid), value) = result?;

View File

@ -11,8 +11,8 @@ use crate::search::new::interner::Interned;
use crate::search::new::query_graph::QueryNodeData;
use crate::search::new::query_term::LocatedQueryTermSubset;
use crate::search::new::ranking_rule_graph::{
Edge, ProximityCondition, ProximityGraph, RankingRuleGraph, RankingRuleGraphTrait,
TypoCondition, TypoGraph,
AttributeCondition, AttributeGraph, Edge, ProximityCondition, ProximityGraph, RankingRuleGraph,
RankingRuleGraphTrait, TypoCondition, TypoGraph,
};
use crate::search::new::ranking_rules::BoxRankingRule;
use crate::search::new::{QueryGraph, QueryNode, RankingRule, SearchContext, SearchLogger};
@ -29,12 +29,15 @@ pub enum SearchEvents {
ProximityPaths { paths: Vec<Vec<Interned<ProximityCondition>>> },
TypoGraph { graph: RankingRuleGraph<TypoGraph> },
TypoPaths { paths: Vec<Vec<Interned<TypoCondition>>> },
AttributeGraph { graph: RankingRuleGraph<AttributeGraph> },
AttributePaths { paths: Vec<Vec<Interned<AttributeCondition>>> },
}
enum Location {
Words,
Typo,
Proximity,
Attribute,
Other,
}
@ -81,6 +84,7 @@ impl SearchLogger<QueryGraph> for VisualSearchLogger {
"words" => Location::Words,
"typo" => Location::Typo,
"proximity" => Location::Proximity,
"attribute" => Location::Attribute,
_ => Location::Other,
});
}
@ -152,6 +156,15 @@ impl SearchLogger<QueryGraph> for VisualSearchLogger {
self.events.push(SearchEvents::ProximityPaths { paths: paths.clone() });
}
}
Location::Attribute => {
if let Some(graph) = state.downcast_ref::<RankingRuleGraph<AttributeGraph>>() {
self.events.push(SearchEvents::AttributeGraph { graph: graph.clone() });
}
if let Some(paths) = state.downcast_ref::<Vec<Vec<Interned<AttributeCondition>>>>()
{
self.events.push(SearchEvents::AttributePaths { paths: paths.clone() });
}
}
Location::Other => {}
}
}
@ -314,6 +327,10 @@ impl<'ctx> DetailedLoggerFinish<'ctx> {
SearchEvents::TypoPaths { paths } => {
self.write_rr_graph_paths::<TypoGraph>(paths)?;
}
SearchEvents::AttributeGraph { graph } => self.write_rr_graph(&graph)?,
SearchEvents::AttributePaths { paths } => {
self.write_rr_graph_paths::<AttributeGraph>(paths)?;
}
}
Ok(())
}

View File

@ -1,9 +1,4 @@
use std::collections::HashMap;
use crate::{
index::tests::TempIndex, search::new::tests::collect_field_values, Criterion, Search,
SearchResult, TermsMatchingStrategy,
};
use crate::{index::tests::TempIndex, Criterion, Search, SearchResult, TermsMatchingStrategy};
fn create_index() -> TempIndex {
let index = TempIndex::new();
@ -24,21 +19,75 @@ fn create_index() -> TempIndex {
.add_documents(documents!([
{
"id": 0,
"title": "the quick brown fox jumps over the lazy dog",
"description": "Pack my box with five dozen liquor jugs",
"plot": "How vexingly quick daft zebras jump",
"title": "",
"description": "",
"plot": "the quick brown fox jumps over the lazy dog",
},
{
"id": 1,
"title": "Pack my box with five dozen liquor jugs",
"title": "",
"description": "the quick brown foxes jump over the lazy dog",
"plot": "How vexingly quick daft zebras jump",
"plot": "",
},
{
"id": 2,
"title": "How vexingly quick daft zebras jump",
"description": "Pack my box with five dozen liquor jugs",
"plot": "the quick brown fox jumps over the lazy dog",
"title": "the quick brown fox jumps over the lazy dog",
"description": "",
"plot": "",
},
{
"id": 3,
"title": "the",
"description": "quick brown fox jumps over the lazy dog",
"plot": "",
},
{
"id": 4,
"title": "the quick",
"description": "brown fox jumps over the lazy dog",
"plot": "",
},
{
"id": 5,
"title": "the quick brown",
"description": "fox jumps over the lazy dog",
"plot": "",
},
{
"id": 6,
"title": "the quick brown fox",
"description": "jumps over the lazy dog",
"plot": "",
},
{
"id": 7,
"title": "the quick",
"description": "brown fox jumps",
"plot": "over the lazy dog",
},
{
"id": 8,
"title": "the quick brown",
"description": "fox",
"plot": "jumps over the lazy dog",
},
{
"id": 9,
"title": "the quick brown",
"description": "fox jumps",
"plot": "over the lazy dog",
},
{
"id": 10,
"title": "",
"description": "the quick brown fox",
"plot": "jumps over the lazy dog",
},
{
"id": 11,
"title": "the quick",
"description": "",
"plot": "brown fox jumps over the lazy dog",
}
]))
.unwrap();
@ -46,13 +95,14 @@ fn create_index() -> TempIndex {
}
#[test]
fn test_attributes_are_ranked_correctly() {
fn test_attributes_simple() {
let index = create_index();
let txn = index.read_txn().unwrap();
let mut s = Search::new(&txn, &index);
s.terms_matching_strategy(TermsMatchingStrategy::All);
s.query("the quick brown fox");
s.query("the quick brown fox jumps over the lazy dog");
let SearchResult { documents_ids, .. } = s.execute().unwrap();
insta::assert_snapshot!(format!("{documents_ids:?}"), @"[0, 1, 2]");
insta::assert_snapshot!(format!("{documents_ids:?}"), @"[2, 6, 5, 4, 3, 9, 7, 8, 11, 10, 0]");
}