Integrate the new Highlighter in the search

This commit is contained in:
ManyTheFish
2023-04-06 13:58:56 +02:00
parent ebe23b04c9
commit 9c5f64769a
6 changed files with 77 additions and 41 deletions

View File

@ -1,4 +1,5 @@
use std::cmp::Reverse;
use std::fmt;
use std::ops::RangeInclusive;
use charabia::Token;
@ -23,6 +24,7 @@ pub struct LocatedMatchingWords {
/// Structure created from a query tree
/// referencing words that match the given query tree.
#[derive(Default)]
pub struct MatchingWords {
word_interner: DedupInterner<String>,
phrase_interner: DedupInterner<Phrase>,
@ -240,6 +242,40 @@ impl<'a> PartialMatch<'a> {
}
}
impl fmt::Debug for MatchingWords {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let MatchingWords { word_interner, phrase_interner, phrases, words } = self;
let phrases: Vec<_> = phrases
.iter()
.map(|p| {
(
phrase_interner
.get(p.value)
.words
.iter()
.map(|w| w.map_or("STOP_WORD", |w| word_interner.get(w)))
.collect::<Vec<_>>()
.join(" "),
p.positions.clone(),
)
})
.collect();
let words: Vec<_> = words
.iter()
.flat_map(|w| {
w.value
.iter()
.map(|s| (word_interner.get(*s), w.positions.clone(), w.is_prefix))
.collect::<Vec<_>>()
})
.collect();
f.debug_struct("MatchingWords").field("phrases", &phrases).field("words", &words).finish()
}
}
#[cfg(test)]
pub(crate) mod tests {
use std::borrow::Cow;

View File

@ -1,7 +1,8 @@
use std::borrow::Cow;
use charabia::{SeparatorKind, Token, Tokenizer};
use matching_words::{MatchType, MatchingWords, PartialMatch, WordId};
pub use matching_words::MatchingWords;
use matching_words::{MatchType, PartialMatch, WordId};
use serde::Serialize;
use super::query_term::LocatedQueryTerm;
@ -23,12 +24,7 @@ pub struct MatcherBuilder<'a, A> {
}
impl<'a, A> MatcherBuilder<'a, A> {
pub fn new(
ctx: SearchContext,
located_terms: Vec<LocatedQueryTerm>,
tokenizer: Tokenizer<'a, 'a, A>,
) -> Self {
let matching_words = MatchingWords::new(ctx, located_terms);
pub fn new(matching_words: MatchingWords, tokenizer: Tokenizer<'a, 'a, A>) -> Self {
Self {
matching_words,
tokenizer,
@ -514,7 +510,8 @@ mod tests {
let tokenizer = TokenizerBuilder::new().build();
let tokens = tokenizer.tokenize(query);
let query_terms = located_query_terms_from_string(&mut ctx, tokens, None).unwrap();
Self::new(ctx, query_terms, TokenizerBuilder::new().build())
let matching_words = MatchingWords::new(ctx, query_terms);
Self::new(matching_words, TokenizerBuilder::new().build())
}
}