lucisearch 0.8.0

Embeddable, in-process search engine — the SQLite/DuckDB of Elasticsearch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Wildcard query: match terms using * (any chars) and ? (single char).
//!
//! Compiles the wildcard pattern to an unanchored regex string, then to
//! a `RegexAutomaton` (DFA implementing `fst::Automaton`). Term
//! enumeration happens via `reader.automaton_search` — FST/DFA
//! intersection that prunes non-matching subtrees via `can_match`.
//! Same primitive `levenshtein_automata::DFA` uses for fuzzy and
//! `RegexpQuery` uses for general regex.
//!
//! For trailing-wildcard patterns like `tech*` the DFA's `can_match`
//! naturally prunes the FST to the literal-prefix subtree (no separate
//! fast path needed). For non-trailing patterns like `*ology` or
//! `t?ch` the DFA still drives O(matching subtrees) work — this is
//! the fix that closes the leading-wildcard performance gap.
//!
//! Uses **per-segment rewrite**: term enumeration happens inside
//! `scorer_supplier(reader)` for each segment, not globally at `bind()`.
//! Matched terms are unioned via [`ConstantScoreMultiTermSupplier`]
//! (shared `FilterScorer` + `BufferedUnionScorer`), matching Lucene's
//! `MultiTermQueryConstantScoreBlendedWrapper`. Every matching doc
//! receives a constant score of 1.0.
//!
//! See [[fix-wildcard-fuzzy-quadratic-dedup]],
//! [[optimization-multiterm-constant-score-rewrite]], and
//! [[optimization-regexp-wildcard-fst-automaton]].

use crate::core::{Result, ScoreMode};
use regex::Regex;

use crate::query::multi_term::ConstantScoreMultiTermSupplier;
use crate::query::regex_automaton::RegexAutomaton;
use crate::query::{BoundQuery, Query, ScorerSupplier};
use crate::search::searcher::Searcher;
use crate::segment::reader::SegmentReader;

/// Wildcard query: * matches any sequence, ? matches single character.
pub struct WildcardQuery {
    pub field: String,
    pub pattern: String,
}

impl WildcardQuery {
    /// Check if a term matches the wildcard pattern (for tests).
    pub fn matches(pattern: &str, term: &str) -> bool {
        let regex_pattern = wildcard_to_regex_anchored(pattern);
        Regex::new(&regex_pattern)
            .map(|re| re.is_match(term))
            .unwrap_or(false)
    }
}

/// Convert a wildcard pattern to an unanchored regex pattern (for the
/// `RegexAutomaton` path — anchoring is handled by the DFA's
/// `StartKind::Anchored` configuration).
/// `*` → `.*`, `?` → `.`, other regex meta chars are escaped.
fn wildcard_to_regex_unanchored(pattern: &str) -> String {
    let mut regex = String::with_capacity(pattern.len() * 2);
    for ch in pattern.chars() {
        match ch {
            '*' => regex.push_str(".*"),
            '?' => regex.push('.'),
            '.' | '+' | '(' | ')' | '[' | ']' | '{' | '}' | '\\' | '^' | '$' | '|' => {
                regex.push('\\');
                regex.push(ch);
            }
            _ => regex.push(ch),
        }
    }
    regex
}

/// Anchored variant for the legacy `WildcardQuery::matches` test helper.
fn wildcard_to_regex_anchored(pattern: &str) -> String {
    let mut s = String::with_capacity(pattern.len() * 2 + 2);
    s.push('^');
    s.push_str(&wildcard_to_regex_unanchored(pattern));
    s.push('$');
    s
}

impl Query for WildcardQuery {
    fn bind(&self, _searcher: &Searcher, _score_mode: ScoreMode) -> Result<Box<dyn BoundQuery>> {
        let regex_pattern = wildcard_to_regex_unanchored(&self.pattern);
        let automaton = RegexAutomaton::new(&regex_pattern)?;
        Ok(Box::new(BoundWildcardQuery {
            field: self.field.clone(),
            automaton,
        }))
    }
}

/// Bound wildcard query — defers term enumeration to per-segment
/// `scorer_supplier(reader)` calls via `automaton_search`.
struct BoundWildcardQuery {
    field: String,
    automaton: RegexAutomaton,
}

impl BoundQuery for BoundWildcardQuery {
    fn scorer_supplier(&self, reader: &SegmentReader) -> Result<Option<Box<dyn ScorerSupplier>>> {
        let field_id = match reader
            .header()
            .fields
            .iter()
            .find(|f| f.field_name == self.field)
            .map(|f| f.field_id)
        {
            Some(id) => id,
            None => return Ok(None),
        };

        // FST/DFA intersection — prunes non-matching subtrees via the
        // automaton's can_match. Trailing wildcards (tech*) naturally
        // narrow to the literal-prefix subtree; leading and middle
        // wildcards (*ology, t?ch) still drive O(matching subtrees)
        // work instead of O(all terms in field).
        let terms: Vec<(String, u32)> = reader.automaton_search(field_id, &self.automaton);
        if terms.is_empty() {
            return Ok(None);
        }

        Ok(Some(Box::new(ConstantScoreMultiTermSupplier::new(
            reader, field_id, terms,
        ))))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::Token;
    use crate::core::{FieldId, SegmentId};
    use crate::mapping::{FieldType, Mapping};
    use crate::segment::builder::SegmentBuilder;
    use crate::segment::reader::SegmentReader;

    #[test]
    fn wildcard_matching() {
        assert!(WildcardQuery::matches("tech*", "technology"));
        assert!(WildcardQuery::matches("tech*", "tech"));
        assert!(!WildcardQuery::matches("tech*", "tec"));
        assert!(WildcardQuery::matches("t?ch", "tech"));
        assert!(!WildcardQuery::matches("t?ch", "touch"));
        assert!(WildcardQuery::matches("*fox*", "quick fox jumps"));
        assert!(WildcardQuery::matches("*", "anything"));
        assert!(WildcardQuery::matches("?", "x"));
        assert!(!WildcardQuery::matches("?", ""));
        assert!(WildcardQuery::matches("a*b", "ab"));
        assert!(WildcardQuery::matches("a*b", "aXYZb"));
        assert!(!WildcardQuery::matches("a*b", "aXYZc"));
    }

    #[test]
    fn wildcard_query_star() {
        let schema = Mapping::builder().field("tag", FieldType::Keyword).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);
        for tag in &["technology", "technical", "tennis", "science"] {
            builder.add_document(
                &[(FieldId::new(0), vec![Token::new(*tag, 0, tag.len(), 0)])],
                b"{}",
            );
        }
        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            crate::analysis::AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);

        let results = searcher
            .search_query(
                &WildcardQuery {
                    field: "tag".into(),
                    pattern: "tech*".into(),
                },
                10,
                0,
            )
            .unwrap();
        assert_eq!(results.total_hits.value, 2); // technology, technical
    }

    #[test]
    fn wildcard_query_question_mark() {
        let schema = Mapping::builder().field("tag", FieldType::Keyword).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);
        for tag in &["cat", "cut", "cot", "cart"] {
            builder.add_document(
                &[(FieldId::new(0), vec![Token::new(*tag, 0, tag.len(), 0)])],
                b"{}",
            );
        }
        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            crate::analysis::AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);

        let results = searcher
            .search_query(
                &WildcardQuery {
                    field: "tag".into(),
                    pattern: "c?t".into(),
                },
                10,
                0,
            )
            .unwrap();
        assert_eq!(results.total_hits.value, 3); // cat, cut, cot (not cart — 4 chars)
    }

    #[test]
    fn wildcard_constant_score_all_ones() {
        // Multi-term wildcard match: every hit should score exactly 1.0,
        // matching ES's CONSTANT_SCORE_BLENDED_REWRITE behavior.
        // See [[optimization-multiterm-constant-score-rewrite]].
        let schema = Mapping::builder().field("tag", FieldType::Keyword).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);
        for tag in &["technology", "technical", "tennis", "science"] {
            builder.add_document(
                &[(FieldId::new(0), vec![Token::new(*tag, 0, tag.len(), 0)])],
                b"{}",
            );
        }
        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            crate::analysis::AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);

        let results = searcher
            .search_query(
                &WildcardQuery {
                    field: "tag".into(),
                    pattern: "tech*".into(),
                },
                10,
                0,
            )
            .unwrap();
        assert_eq!(results.total_hits.value, 2);
        for hit in &results.hits {
            assert_eq!(
                hit.score, 1.0,
                "wildcard hit should have constant score 1.0, got {}",
                hit.score
            );
        }
    }

    #[test]
    fn wildcard_high_cardinality_constant_score() {
        // Stress the multi-term BufferedUnionScorer path (>>1 sub-scorer)
        // with constant scoring. This is the unit-test analogue of the
        // UMLS wildcard benchmark — exercises window refilling and
        // sub-scorer exhaustion logic at unit-test speed.
        let schema = Mapping::builder().field("tag", FieldType::Keyword).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);
        for i in 0..1000 {
            let tag = format!("tag_{i:04}");
            builder.add_document(
                &[(FieldId::new(0), vec![Token::new(&tag, 0, tag.len(), 0)])],
                b"{}",
            );
        }
        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            crate::analysis::AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);

        let results = searcher
            .search_query(
                &WildcardQuery {
                    field: "tag".into(),
                    pattern: "tag_*".into(),
                },
                1000,
                0,
            )
            .unwrap();
        assert_eq!(results.total_hits.value, 1000);
        assert_eq!(results.hits.len(), 1000);
        for hit in &results.hits {
            assert_eq!(
                hit.score, 1.0,
                "high-cardinality wildcard hit should score 1.0, got {}",
                hit.score
            );
        }
    }

    #[test]
    fn wildcard_no_matches() {
        let schema = Mapping::builder().field("tag", FieldType::Keyword).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);
        builder.add_document(
            &[(FieldId::new(0), vec![Token::new("hello", 0, 5, 0)])],
            b"{}",
        );
        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            crate::analysis::AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);

        let results = searcher
            .search_query(
                &WildcardQuery {
                    field: "tag".into(),
                    pattern: "xyz*".into(),
                },
                10,
                0,
            )
            .unwrap();
        assert_eq!(results.total_hits.value, 0);
    }

    #[test]
    fn wildcard_leading_wildcard_correctness() {
        // Regression sentinel: leading-wildcard hit set must be
        // identical before and after the FST automaton intersection
        // change. See [[optimization-regexp-wildcard-fst-automaton]].
        let schema = Mapping::builder().field("tag", FieldType::Keyword).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);
        for tag in &["technology", "ecology", "biology", "tennis"] {
            builder.add_document(
                &[(FieldId::new(0), vec![Token::new(*tag, 0, tag.len(), 0)])],
                b"{}",
            );
        }
        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            crate::analysis::AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);

        let results = searcher
            .search_query(
                &WildcardQuery {
                    field: "tag".into(),
                    pattern: "*ology".into(),
                },
                10,
                0,
            )
            .unwrap();
        // technology, ecology, biology — all end with "ology"
        assert_eq!(results.total_hits.value, 3);
        for hit in &results.hits {
            assert_eq!(hit.score, 1.0);
        }
    }

    #[test]
    fn wildcard_middle_wildcard_correctness() {
        // Regression sentinel: middle-wildcard hit set must be
        // identical before and after the FST automaton intersection
        // change. See [[optimization-regexp-wildcard-fst-automaton]].
        let schema = Mapping::builder().field("tag", FieldType::Keyword).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);
        for tag in &["bat1", "bet1", "bit1", "but2", "bat2"] {
            builder.add_document(
                &[(FieldId::new(0), vec![Token::new(*tag, 0, tag.len(), 0)])],
                b"{}",
            );
        }
        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            crate::analysis::AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);

        let results = searcher
            .search_query(
                &WildcardQuery {
                    field: "tag".into(),
                    pattern: "b?t1".into(),
                },
                10,
                0,
            )
            .unwrap();
        // bat1, bet1, bit1 — all match b?t1; but2 and bat2 don't (end in 2)
        assert_eq!(results.total_hits.value, 3);
    }
}