Skip to main content

searchlight/
query.rs

1use crate::tokenizer::{self, is_chinese_char};
2
3/// Represents different types of query operations
4#[derive(Debug, Clone, PartialEq)]
5pub enum QueryOp {
6    /// Match a single term exactly
7    Term(String),
8    /// Match a phrase (sequence of terms in order)
9    Phrase(Vec<String>),
10    /// Fuzzy match with edit distance tolerance
11    Fuzzy(String, usize),
12    /// Prefix match (autocomplete style)
13    Prefix(String),
14    /// Match Chinese characters (individual char matching)
15    CharMatch(Vec<char>),
16    /// AND of multiple query ops
17    And(Vec<QueryOp>),
18    /// OR of multiple query ops
19    Or(Vec<QueryOp>),
20    /// NOT (exclude) a query op
21    Not(Box<QueryOp>),
22}
23
24/// Parsed query with associated metadata
25#[derive(Debug, Clone)]
26pub struct ParsedQuery {
27    /// The root query operation
28    pub root: QueryOp,
29    /// Original query string
30    pub original: String,
31    /// Whether pinyin matching is requested
32    pub use_pinyin: bool,
33    /// Maximum edit distance for fuzzy matches
34    pub max_edit_distance: usize,
35}
36
37/// Query parser for the search engine.
38/// Supports:
39/// - Simple term queries: `hello world`
40/// - Phrase queries: `"exact phrase"`
41/// - Boolean AND: `hello AND world` or `+hello +world`
42/// - Boolean OR: `hello OR world`
43/// - Boolean NOT: `-exclude` or `NOT exclude`
44/// - Fuzzy queries: `term~` or `term~2`
45/// - Prefix queries: `prefix*`
46/// - Pinyin flag: `pinyin:ni hao` or `py:你好`
47pub struct QueryParser {
48    default_fuzzy_distance: usize,
49}
50
51impl QueryParser {
52    pub fn new() -> Self {
53        QueryParser {
54            default_fuzzy_distance: 2,
55        }
56    }
57
58    pub fn with_fuzzy_distance(distance: usize) -> Self {
59        QueryParser {
60            default_fuzzy_distance: distance,
61        }
62    }
63
64    /// Parse a query string into a ParsedQuery
65    pub fn parse(&self, input: &str) -> ParsedQuery {
66        let original = input.to_string();
67        let trimmed = input.trim();
68
69        // Check for pinyin mode flag
70        let (use_pinyin, query_text) =
71            if trimmed.starts_with("pinyin:") || trimmed.starts_with("py:") {
72                let q = trimmed.splitn(2, ':').nth(1).unwrap_or("").trim();
73                (true, q.to_string())
74            } else {
75                (false, trimmed.to_string())
76            };
77
78        let root = if query_text.is_empty() {
79            QueryOp::And(vec![])
80        } else {
81            self.parse_expression(&query_text)
82        };
83
84        ParsedQuery {
85            root,
86            original,
87            use_pinyin,
88            max_edit_distance: self.default_fuzzy_distance,
89        }
90    }
91
92    /// Parse a query expression, handling OR at the top level
93    fn parse_expression(&self, input: &str) -> QueryOp {
94        // Check for quoted phrase
95        if let Some(phrase) = self.try_parse_phrase(input) {
96            return phrase;
97        }
98
99        // Split by OR (lowest precedence)
100        let or_parts = self.split_operator(input, "OR");
101        if or_parts.len() > 1 {
102            let ops: Vec<QueryOp> = or_parts
103                .iter()
104                .map(|p| self.parse_and(p.trim()))
105                .filter(|op| !matches!(op, QueryOp::And(ref v) if v.is_empty()))
106                .collect();
107            return if ops.len() == 1 {
108                ops.into_iter().next().unwrap()
109            } else {
110                QueryOp::Or(ops)
111            };
112        }
113
114        self.parse_and(input)
115    }
116
117    /// Parse AND-connected terms
118    fn parse_and(&self, input: &str) -> QueryOp {
119        let and_parts = self.split_operator(input, "AND");
120
121        let mut terms: Vec<QueryOp> = Vec::new();
122        let mut exclude: Vec<QueryOp> = Vec::new();
123
124        for part in &and_parts {
125            let trimmed = part.trim();
126            if trimmed.is_empty() {
127                continue;
128            }
129
130            // Handle NOT / exclusion with - prefix (both at start and inline)
131            if let Some(rest) = trimmed.strip_prefix('-') {
132                exclude.push(self.parse_term(rest.trim()));
133                continue;
134            }
135
136            // Check for inline -exclusions like "hello -world"
137            if let Some(pos) = trimmed.find(" -") {
138                let include_part = &trimmed[..pos].trim();
139                let exclude_part = &trimmed[pos + 2..].trim();
140                if !include_part.is_empty() {
141                    terms.push(self.parse_term(include_part));
142                }
143                if !exclude_part.is_empty() {
144                    exclude.push(self.parse_term(exclude_part));
145                }
146                continue;
147            }
148
149            terms.push(self.parse_term(trimmed));
150        }
151
152        // Combine includes and excludes
153        let mut all_ops = terms;
154        for ex in exclude {
155            all_ops.push(QueryOp::Not(Box::new(ex)));
156        }
157
158        if all_ops.is_empty() {
159            QueryOp::And(vec![])
160        } else if all_ops.len() == 1 {
161            all_ops.into_iter().next().unwrap()
162        } else {
163            QueryOp::And(all_ops)
164        }
165    }
166
167    /// Parse a single term with possible modifiers
168    fn parse_term(&self, input: &str) -> QueryOp {
169        let trimmed = input.trim();
170
171        if trimmed.is_empty() {
172            return QueryOp::And(vec![]);
173        }
174
175        // Fuzzy match: term~ or term~2
176        if let Some(idx) = trimmed.rfind('~') {
177            let term = &trimmed[..idx];
178            let distance_str = &trimmed[idx + 1..];
179            let distance = if distance_str.is_empty() {
180                self.default_fuzzy_distance
181            } else {
182                distance_str
183                    .parse::<usize>()
184                    .unwrap_or(self.default_fuzzy_distance)
185            };
186            if !term.is_empty() {
187                // Tokenize the fuzzy term in case it contains multiple words
188                let tokens = tokenizer::tokenize(term);
189                if tokens.len() == 1 {
190                    return QueryOp::Fuzzy(tokens[0].text.clone(), distance.min(3));
191                } else {
192                    let terms: Vec<String> = tokens.into_iter().map(|t| t.text).collect();
193                    return QueryOp::And(
194                        terms
195                            .into_iter()
196                            .map(|t| QueryOp::Fuzzy(t, distance.min(3)))
197                            .collect(),
198                    );
199                }
200            }
201        }
202
203        // Prefix match: term*
204        if let Some(prefix) = trimmed.strip_suffix('*') {
205            if !prefix.is_empty() {
206                let tokens = tokenizer::tokenize(prefix);
207                if tokens.len() == 1 {
208                    return QueryOp::Prefix(tokens[0].text.clone());
209                } else {
210                    let terms: Vec<String> = tokens.into_iter().map(|t| t.text).collect();
211                    return QueryOp::And(terms.into_iter().map(|t| QueryOp::Prefix(t)).collect());
212                }
213            }
214        }
215
216        // Multi-word term (space-separated, outside phrase)
217        let tokens = tokenizer::tokenize(trimmed);
218        let has_chinese = trimmed.chars().any(is_chinese_char);
219
220        if has_chinese && tokens.len() >= 2 {
221            // For Chinese, also try character-level matching
222            let chars: Vec<char> = trimmed.chars().filter(|c| !c.is_whitespace()).collect();
223            if chars.len() >= 2 {
224                return QueryOp::Or(vec![
225                    QueryOp::And(tokens.into_iter().map(|t| QueryOp::Term(t.text)).collect()),
226                    QueryOp::CharMatch(chars),
227                ]);
228            }
229        }
230
231        if tokens.len() == 1 {
232            QueryOp::Term(tokens[0].text.clone())
233        } else {
234            QueryOp::And(tokens.into_iter().map(|t| QueryOp::Term(t.text)).collect())
235        }
236    }
237
238    /// Try to parse a quoted phrase
239    fn try_parse_phrase(&self, input: &str) -> Option<QueryOp> {
240        let trimmed = input.trim();
241        if trimmed.starts_with('"') && trimmed.contains('"') {
242            let end = trimmed[1..].find('"').map(|i| i + 1)?;
243            let phrase = &trimmed[1..end];
244            let tokens = tokenizer::tokenize(phrase);
245            let terms: Vec<String> = tokens.into_iter().map(|t| t.text).collect();
246            if !terms.is_empty() {
247                return Some(QueryOp::Phrase(terms));
248            }
249        }
250        None
251    }
252
253    /// Split input by an operator, respecting quoted sections
254    fn split_operator(&self, input: &str, operator: &str) -> Vec<String> {
255        let op_upper = operator.to_uppercase();
256        let mut parts = Vec::new();
257        let mut in_quote = false;
258        let mut current = String::new();
259        let chars: Vec<char> = input.chars().collect();
260        let mut i = 0;
261
262        while i < chars.len() {
263            if chars[i] == '"' {
264                in_quote = !in_quote;
265                current.push(chars[i]);
266                i += 1;
267                continue;
268            }
269
270            if !in_quote && i + operator.len() <= chars.len() {
271                let slice: String = chars[i..i + operator.len()].iter().collect();
272                let slice_upper = slice.to_uppercase();
273
274                // Check word boundaries
275                let prev_is_boundary = i == 0 || chars[i - 1].is_whitespace();
276                let next_is_boundary =
277                    i + operator.len() >= chars.len() || chars[i + operator.len()].is_whitespace();
278
279                if slice_upper == op_upper && prev_is_boundary && next_is_boundary {
280                    if !current.trim().is_empty() {
281                        parts.push(current.trim().to_string());
282                    }
283                    current = String::new();
284                    i += operator.len();
285                    continue;
286                }
287            }
288
289            current.push(chars[i]);
290            i += 1;
291        }
292
293        if !current.trim().is_empty() {
294            parts.push(current.trim().to_string());
295        }
296
297        if parts.is_empty() {
298            vec![input.to_string()]
299        } else {
300            parts
301        }
302    }
303}
304
305impl Default for QueryParser {
306    fn default() -> Self {
307        Self::new()
308    }
309}
310
311#[cfg(test)]
312mod tests {
313    use super::*;
314
315    #[test]
316    fn test_simple_term() {
317        let parser = QueryParser::new();
318        let q = parser.parse("hello");
319        assert_eq!(q.root, QueryOp::Term("hello".to_string()));
320    }
321
322    #[test]
323    fn test_and_query() {
324        let parser = QueryParser::new();
325        let q = parser.parse("hello AND world");
326        assert!(matches!(q.root, QueryOp::And(_)));
327        if let QueryOp::And(ops) = &q.root {
328            assert_eq!(ops.len(), 2);
329        }
330    }
331
332    #[test]
333    fn test_or_query() {
334        let parser = QueryParser::new();
335        let q = parser.parse("hello OR world");
336        assert!(matches!(q.root, QueryOp::Or(_)));
337    }
338
339    #[test]
340    fn test_not_query() {
341        let parser = QueryParser::new();
342        let q = parser.parse("hello -world");
343        assert!(matches!(q.root, QueryOp::And(_)));
344        if let QueryOp::And(ops) = &q.root {
345            assert!(ops.iter().any(|op| matches!(op, QueryOp::Not(_))));
346        }
347    }
348
349    #[test]
350    fn test_phrase_query() {
351        let parser = QueryParser::new();
352        let q = parser.parse("\"hello world\"");
353        assert!(matches!(q.root, QueryOp::Phrase(_)));
354    }
355
356    #[test]
357    fn test_fuzzy_query() {
358        let parser = QueryParser::new();
359        let q = parser.parse("hello~");
360        assert!(matches!(q.root, QueryOp::Fuzzy(_, _)));
361    }
362
363    #[test]
364    fn test_fuzzy_with_distance() {
365        let parser = QueryParser::new();
366        let q = parser.parse("hello~2");
367        if let QueryOp::Fuzzy(_, d) = &q.root {
368            assert_eq!(*d, 2);
369        } else {
370            panic!("Expected Fuzzy query");
371        }
372    }
373
374    #[test]
375    fn test_prefix_query() {
376        let parser = QueryParser::new();
377        let q = parser.parse("prog*");
378        assert!(matches!(q.root, QueryOp::Prefix(_)));
379    }
380
381    #[test]
382    fn test_pinyin_mode() {
383        let parser = QueryParser::new();
384        let q = parser.parse("pinyin:ni hao");
385        assert!(q.use_pinyin);
386    }
387}