mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-26 12:05:05 +08:00
refactor formatter
share the analyzer instance between the formatter and the compute_matches function
This commit is contained in:
parent
eb3d63691a
commit
3456a78552
@ -163,7 +163,11 @@ impl Index {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut documents = Vec::new();
|
let mut documents = Vec::new();
|
||||||
|
|
||||||
@ -174,7 +178,7 @@ impl Index {
|
|||||||
|
|
||||||
let matches_info = query
|
let matches_info = query
|
||||||
.matches
|
.matches
|
||||||
.then(|| compute_matches(&matching_words, &document));
|
.then(|| compute_matches(&matching_words, &document, &analyzer));
|
||||||
|
|
||||||
let formatted = format_fields(
|
let formatted = format_fields(
|
||||||
&fields_ids_map,
|
&fields_ids_map,
|
||||||
@ -221,12 +225,12 @@ impl Index {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_matches(matcher: &impl Matcher, document: &Document) -> MatchesInfo {
|
fn compute_matches<A: AsRef<[u8]>>(
|
||||||
|
matcher: &impl Matcher,
|
||||||
|
document: &Document,
|
||||||
|
analyzer: &Analyzer<A>
|
||||||
|
) -> MatchesInfo {
|
||||||
let mut matches = BTreeMap::new();
|
let mut matches = BTreeMap::new();
|
||||||
let stop_words = fst::Set::default();
|
|
||||||
let mut config = AnalyzerConfig::default();
|
|
||||||
config.stop_words(&stop_words);
|
|
||||||
let analyzer = Analyzer::new(config);
|
|
||||||
|
|
||||||
for (key, value) in document {
|
for (key, value) in document {
|
||||||
let mut infos = Vec::new();
|
let mut infos = Vec::new();
|
||||||
@ -455,17 +459,12 @@ impl Matcher for MatchingWords {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Formatter<'a, A> {
|
struct Formatter<'a, A> {
|
||||||
analyzer: Analyzer<'a, A>,
|
analyzer: &'a Analyzer<'a, A>,
|
||||||
marks: (String, String),
|
marks: (String, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, A: AsRef<[u8]>> Formatter<'a, A> {
|
impl<'a, A: AsRef<[u8]>> Formatter<'a, A> {
|
||||||
pub fn new(stop_words: &'a fst::Set<A>, marks: (String, String)) -> Self {
|
pub fn new(analyzer: &'a Analyzer<'a, A>, marks: (String, String)) -> Self {
|
||||||
let mut config = AnalyzerConfig::default();
|
|
||||||
config.stop_words(stop_words);
|
|
||||||
|
|
||||||
let analyzer = Analyzer::new(config);
|
|
||||||
|
|
||||||
Self { analyzer, marks }
|
Self { analyzer, marks }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -641,7 +640,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn no_ids_no_formatted() {
|
fn no_ids_no_formatted() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let id = fields.insert("test").unwrap();
|
let id = fields.insert("test").unwrap();
|
||||||
@ -673,7 +675,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn formatted_with_highlight_in_word() {
|
fn formatted_with_highlight_in_word() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let title = fields.insert("title").unwrap();
|
let title = fields.insert("title").unwrap();
|
||||||
@ -734,7 +739,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn formatted_with_crop_2() {
|
fn formatted_with_crop_2() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let title = fields.insert("title").unwrap();
|
let title = fields.insert("title").unwrap();
|
||||||
@ -795,7 +803,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn formatted_with_crop_10() {
|
fn formatted_with_crop_10() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let title = fields.insert("title").unwrap();
|
let title = fields.insert("title").unwrap();
|
||||||
@ -856,7 +867,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn formatted_with_crop_0() {
|
fn formatted_with_crop_0() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let title = fields.insert("title").unwrap();
|
let title = fields.insert("title").unwrap();
|
||||||
@ -917,7 +931,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn formatted_with_crop_and_no_match() {
|
fn formatted_with_crop_and_no_match() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let title = fields.insert("title").unwrap();
|
let title = fields.insert("title").unwrap();
|
||||||
@ -978,7 +995,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn formatted_with_crop_and_highlight() {
|
fn formatted_with_crop_and_highlight() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let title = fields.insert("title").unwrap();
|
let title = fields.insert("title").unwrap();
|
||||||
@ -1039,7 +1059,10 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn formatted_with_crop_and_highlight_in_word() {
|
fn formatted_with_crop_and_highlight_in_word() {
|
||||||
let stop_words = fst::Set::default();
|
let stop_words = fst::Set::default();
|
||||||
let formatter = Formatter::new(&stop_words, (String::from("<em>"), String::from("</em>")));
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
let formatter = Formatter::new(&analyzer, (String::from("<em>"), String::from("</em>")));
|
||||||
|
|
||||||
let mut fields = FieldsIdsMap::new();
|
let mut fields = FieldsIdsMap::new();
|
||||||
let title = fields.insert("title").unwrap();
|
let title = fields.insert("title").unwrap();
|
||||||
@ -1140,7 +1163,12 @@ mod test {
|
|||||||
matcher.insert("mollit", Some(6));
|
matcher.insert("mollit", Some(6));
|
||||||
matcher.insert("laboris", Some(7));
|
matcher.insert("laboris", Some(7));
|
||||||
|
|
||||||
let matches = compute_matches(&matcher, &value);
|
let stop_words = fst::Set::default();
|
||||||
|
let mut config = AnalyzerConfig::default();
|
||||||
|
config.stop_words(&stop_words);
|
||||||
|
let analyzer = Analyzer::new(config);
|
||||||
|
|
||||||
|
let matches = compute_matches(&matcher, &value, &analyzer);
|
||||||
assert_eq!(format!("{:?}", matches), r##"{"about": [MatchInfo { start: 0, length: 6 }, MatchInfo { start: 31, length: 7 }, MatchInfo { start: 191, length: 7 }, MatchInfo { start: 225, length: 7 }, MatchInfo { start: 233, length: 6 }], "color": [MatchInfo { start: 0, length: 3 }]}"##);
|
assert_eq!(format!("{:?}", matches), r##"{"about": [MatchInfo { start: 0, length: 6 }, MatchInfo { start: 31, length: 7 }, MatchInfo { start: 191, length: 7 }, MatchInfo { start: 225, length: 7 }, MatchInfo { start: 233, length: 6 }], "color": [MatchInfo { start: 0, length: 3 }]}"##);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ impl From<SearchQueryGet> for SearchQuery {
|
|||||||
crop_length: other.crop_length,
|
crop_length: other.crop_length,
|
||||||
attributes_to_highlight,
|
attributes_to_highlight,
|
||||||
filter,
|
filter,
|
||||||
matches: other.matches.unwrap_or_default(),
|
matches: other.matches,
|
||||||
facet_distributions,
|
facet_distributions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user