|
1 | 1 | use tantivy::collector::{Count, TopDocs}; |
2 | 2 | use tantivy::query::{ |
3 | | - BooleanQuery, BoostQuery, FuzzyTermQuery, Occur, Query, QueryParser, TermQuery, |
| 3 | + BooleanQuery, BoostQuery, FuzzyTermQuery, Occur, PhraseQuery, Query, QueryParser, TermQuery, |
4 | 4 | }; |
5 | | -use tantivy::schema::IndexRecordOption; |
| 5 | +use tantivy::schema::{Facet, IndexRecordOption}; |
| 6 | +use tantivy::snippet::SnippetGenerator; |
6 | 7 | use tantivy::{Index, ReloadPolicy, TantivyDocument, Term}; |
7 | 8 | use tauri_plugin_path2::Path2PluginExt; |
8 | 9 |
|
9 | 10 | use crate::query::build_created_at_range_query; |
10 | 11 | use crate::schema::{extract_search_document, get_fields}; |
11 | 12 | use crate::tokenizer::register_tokenizers; |
12 | 13 | use crate::{ |
13 | | - CollectionConfig, CollectionIndex, IndexState, SearchDocument, SearchHit, SearchRequest, |
14 | | - SearchResult, |
| 14 | + CollectionConfig, CollectionIndex, HighlightRange, IndexState, SearchDocument, SearchHit, |
| 15 | + SearchRequest, SearchResult, Snippet, |
15 | 16 | }; |
16 | 17 |
|
17 | 18 | pub fn detect_language(text: &str) -> hypr_language::Language { |
18 | 19 | hypr_language::detect(text) |
19 | 20 | } |
20 | 21 |
|
| 22 | +fn parse_query_parts(query: &str) -> (Vec<&str>, Vec<&str>) { |
| 23 | + let mut phrases = Vec::new(); |
| 24 | + let mut regular_terms = Vec::new(); |
| 25 | + let mut in_quote = false; |
| 26 | + let mut quote_start = 0; |
| 27 | + let mut current_start = 0; |
| 28 | + |
| 29 | + let chars: Vec<char> = query.chars().collect(); |
| 30 | + let mut i = 0; |
| 31 | + |
| 32 | + while i < chars.len() { |
| 33 | + if chars[i] == '"' { |
| 34 | + if in_quote { |
| 35 | + let phrase = &query[quote_start..i]; |
| 36 | + if !phrase.trim().is_empty() { |
| 37 | + phrases.push(phrase.trim()); |
| 38 | + } |
| 39 | + in_quote = false; |
| 40 | + current_start = i + 1; |
| 41 | + } else { |
| 42 | + let before = &query[current_start..i]; |
| 43 | + for term in before.split_whitespace() { |
| 44 | + if !term.is_empty() { |
| 45 | + regular_terms.push(term); |
| 46 | + } |
| 47 | + } |
| 48 | + in_quote = true; |
| 49 | + quote_start = i + 1; |
| 50 | + } |
| 51 | + } |
| 52 | + i += 1; |
| 53 | + } |
| 54 | + |
| 55 | + if in_quote { |
| 56 | + let phrase = &query[quote_start..]; |
| 57 | + if !phrase.trim().is_empty() { |
| 58 | + phrases.push(phrase.trim()); |
| 59 | + } |
| 60 | + } else { |
| 61 | + let remaining = &query[current_start..]; |
| 62 | + for term in remaining.split_whitespace() { |
| 63 | + if !term.is_empty() { |
| 64 | + regular_terms.push(term); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + (phrases, regular_terms) |
| 70 | +} |
| 71 | + |
21 | 72 | pub struct Tantivy<'a, R: tauri::Runtime, M: tauri::Manager<R>> { |
22 | 73 | manager: &'a M, |
23 | 74 | _runtime: std::marker::PhantomData<fn() -> R>, |
@@ -95,18 +146,80 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager<R>> Tantivy<'a, R, M> { |
95 | 146 | let searcher = reader.searcher(); |
96 | 147 |
|
97 | 148 | let use_fuzzy = request.options.fuzzy.unwrap_or(false); |
| 149 | + let phrase_slop = request.options.phrase_slop.unwrap_or(0); |
98 | 150 |
|
99 | 151 | // Title boost factor (3x) to match Orama's title:3, content:1 behavior |
100 | 152 | const TITLE_BOOST: f32 = 3.0; |
101 | 153 |
|
102 | 154 | let mut combined_query: Box<dyn Query> = if use_fuzzy { |
103 | 155 | let distance = request.options.distance.unwrap_or(1); |
104 | | - let terms: Vec<&str> = request.query.split_whitespace().collect(); |
| 156 | + |
| 157 | + // Parse query to extract phrases (quoted) and regular terms |
| 158 | + let (phrases, regular_terms) = parse_query_parts(&request.query); |
| 159 | + |
105 | 160 | let mut term_queries: Vec<(Occur, Box<dyn Query>)> = Vec::new(); |
106 | 161 |
|
107 | | - // For each term, create a Must clause that requires the term to match |
108 | | - // in either title OR content (with title boosted) |
109 | | - for term in terms { |
| 162 | + // Handle quoted phrases with PhraseQuery |
| 163 | + for phrase in phrases { |
| 164 | + let words: Vec<&str> = phrase.split_whitespace().collect(); |
| 165 | + if words.len() > 1 { |
| 166 | + // Create phrase query for title field |
| 167 | + let title_terms: Vec<Term> = words |
| 168 | + .iter() |
| 169 | + .map(|w| Term::from_field_text(fields.title, w)) |
| 170 | + .collect(); |
| 171 | + let mut title_phrase = PhraseQuery::new(title_terms); |
| 172 | + title_phrase.set_slop(phrase_slop); |
| 173 | + |
| 174 | + // Create phrase query for content field |
| 175 | + let content_terms: Vec<Term> = words |
| 176 | + .iter() |
| 177 | + .map(|w| Term::from_field_text(fields.content, w)) |
| 178 | + .collect(); |
| 179 | + let mut content_phrase = PhraseQuery::new(content_terms); |
| 180 | + content_phrase.set_slop(phrase_slop); |
| 181 | + |
| 182 | + // Boost title matches by 3x |
| 183 | + let boosted_title: Box<dyn Query> = |
| 184 | + Box::new(BoostQuery::new(Box::new(title_phrase), TITLE_BOOST)); |
| 185 | + let content_query: Box<dyn Query> = Box::new(content_phrase); |
| 186 | + |
| 187 | + // Phrase must match in at least one field (title OR content) |
| 188 | + let phrase_field_query = BooleanQuery::new(vec![ |
| 189 | + (Occur::Should, boosted_title), |
| 190 | + (Occur::Should, content_query), |
| 191 | + ]); |
| 192 | + |
| 193 | + term_queries.push((Occur::Must, Box::new(phrase_field_query))); |
| 194 | + } else if !words.is_empty() { |
| 195 | + // Single word "phrase" - treat as regular term |
| 196 | + let word = words[0]; |
| 197 | + let title_fuzzy = FuzzyTermQuery::new( |
| 198 | + Term::from_field_text(fields.title, word), |
| 199 | + distance, |
| 200 | + true, |
| 201 | + ); |
| 202 | + let content_fuzzy = FuzzyTermQuery::new( |
| 203 | + Term::from_field_text(fields.content, word), |
| 204 | + distance, |
| 205 | + true, |
| 206 | + ); |
| 207 | + |
| 208 | + let boosted_title: Box<dyn Query> = |
| 209 | + Box::new(BoostQuery::new(Box::new(title_fuzzy), TITLE_BOOST)); |
| 210 | + let content_query: Box<dyn Query> = Box::new(content_fuzzy); |
| 211 | + |
| 212 | + let term_field_query = BooleanQuery::new(vec![ |
| 213 | + (Occur::Should, boosted_title), |
| 214 | + (Occur::Should, content_query), |
| 215 | + ]); |
| 216 | + |
| 217 | + term_queries.push((Occur::Must, Box::new(term_field_query))); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + // Handle regular (unquoted) terms with fuzzy matching |
| 222 | + for term in regular_terms { |
110 | 223 | let title_fuzzy = |
111 | 224 | FuzzyTermQuery::new(Term::from_field_text(fields.title, term), distance, true); |
112 | 225 | let content_fuzzy = FuzzyTermQuery::new( |
@@ -157,20 +270,81 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager<R>> Tantivy<'a, R, M> { |
157 | 270 | ])); |
158 | 271 | } |
159 | 272 |
|
| 273 | + // Apply facet filter |
| 274 | + if let Some(ref facet_path) = request.filters.facet { |
| 275 | + if let Ok(facet) = Facet::from_text(facet_path) { |
| 276 | + let facet_term = Term::from_facet(fields.facets, &facet); |
| 277 | + let facet_query = TermQuery::new(facet_term, IndexRecordOption::Basic); |
| 278 | + combined_query = Box::new(BooleanQuery::new(vec![ |
| 279 | + (Occur::Must, combined_query), |
| 280 | + (Occur::Must, Box::new(facet_query)), |
| 281 | + ])); |
| 282 | + } |
| 283 | + } |
| 284 | + |
160 | 285 | // Use tuple collector to get both top docs and total count |
161 | 286 | let (top_docs, count) = searcher.search( |
162 | 287 | &combined_query, |
163 | 288 | &(TopDocs::with_limit(request.limit), Count), |
164 | 289 | )?; |
165 | 290 |
|
| 291 | + let generate_snippets = request.options.snippets.unwrap_or(false); |
| 292 | + let snippet_max_chars = request.options.snippet_max_chars.unwrap_or(150); |
| 293 | + |
| 294 | + let (title_snippet_gen, content_snippet_gen) = if generate_snippets { |
| 295 | + let mut title_gen = |
| 296 | + SnippetGenerator::create(&searcher, &*combined_query, fields.title)?; |
| 297 | + title_gen.set_max_num_chars(snippet_max_chars); |
| 298 | + |
| 299 | + let mut content_gen = |
| 300 | + SnippetGenerator::create(&searcher, &*combined_query, fields.content)?; |
| 301 | + content_gen.set_max_num_chars(snippet_max_chars); |
| 302 | + |
| 303 | + (Some(title_gen), Some(content_gen)) |
| 304 | + } else { |
| 305 | + (None, None) |
| 306 | + }; |
| 307 | + |
166 | 308 | let mut hits = Vec::new(); |
167 | 309 | for (score, doc_address) in top_docs { |
168 | 310 | let retrieved_doc: TantivyDocument = searcher.doc(doc_address)?; |
169 | 311 |
|
170 | 312 | if let Some(search_doc) = extract_search_document(schema, &fields, &retrieved_doc) { |
| 313 | + let title_snippet = title_snippet_gen.as_ref().map(|generator| { |
| 314 | + let snippet = generator.snippet_from_doc(&retrieved_doc); |
| 315 | + Snippet { |
| 316 | + fragment: snippet.fragment().to_string(), |
| 317 | + highlights: snippet |
| 318 | + .highlighted() |
| 319 | + .iter() |
| 320 | + .map(|range| HighlightRange { |
| 321 | + start: range.start, |
| 322 | + end: range.end, |
| 323 | + }) |
| 324 | + .collect(), |
| 325 | + } |
| 326 | + }); |
| 327 | + |
| 328 | + let content_snippet = content_snippet_gen.as_ref().map(|generator| { |
| 329 | + let snippet = generator.snippet_from_doc(&retrieved_doc); |
| 330 | + Snippet { |
| 331 | + fragment: snippet.fragment().to_string(), |
| 332 | + highlights: snippet |
| 333 | + .highlighted() |
| 334 | + .iter() |
| 335 | + .map(|range| HighlightRange { |
| 336 | + start: range.start, |
| 337 | + end: range.end, |
| 338 | + }) |
| 339 | + .collect(), |
| 340 | + } |
| 341 | + }); |
| 342 | + |
171 | 343 | hits.push(SearchHit { |
172 | 344 | score, |
173 | 345 | document: search_doc, |
| 346 | + title_snippet, |
| 347 | + content_snippet, |
174 | 348 | }); |
175 | 349 | } |
176 | 350 | } |
@@ -232,6 +406,12 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager<R>> Tantivy<'a, R, M> { |
232 | 406 | doc.add_text(fields.content, &document.content); |
233 | 407 | doc.add_i64(fields.created_at, document.created_at); |
234 | 408 |
|
| 409 | + for facet_path in &document.facets { |
| 410 | + if let Ok(facet) = Facet::from_text(facet_path) { |
| 411 | + doc.add_facet(fields.facets, facet); |
| 412 | + } |
| 413 | + } |
| 414 | + |
235 | 415 | writer.add_document(doc)?; |
236 | 416 | writer.commit()?; |
237 | 417 |
|
@@ -273,6 +453,12 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager<R>> Tantivy<'a, R, M> { |
273 | 453 | doc.add_text(fields.content, &document.content); |
274 | 454 | doc.add_i64(fields.created_at, document.created_at); |
275 | 455 |
|
| 456 | + for facet_path in &document.facets { |
| 457 | + if let Ok(facet) = Facet::from_text(facet_path) { |
| 458 | + doc.add_facet(fields.facets, facet); |
| 459 | + } |
| 460 | + } |
| 461 | + |
276 | 462 | writer.add_document(doc)?; |
277 | 463 | writer.commit()?; |
278 | 464 |
|
|
0 commit comments