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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Search highlighting: re-analyze field text and wrap matched terms in tags.
//!
//! Runs in the result construction phase (post-scoring, per-hit) following
//! the same materialization pattern as explain and fields retrieval.
//!
//! See [[feature-search-highlight]].

use std::collections::{HashMap, HashSet};

use crate::analysis::{Analyzer, AnalyzerRegistry};

use crate::query::ast::{ScoringExpression, SpanExpression};

// --- Configuration types ---

/// Parsed highlight request from the search JSON.
///
/// See [[feature-search-highlight]].
#[derive(Clone, Debug)]
pub struct HighlightConfig {
    pub fields: Vec<HighlightFieldConfig>,
    pub require_field_match: bool,
    pub order: HighlightOrder,
}

/// Per-field highlight settings.
///
/// `fragment_size` / `number_of_fragments` cap the number of spans
/// returned for very long fields. `0` for both means "emit every
/// match".
#[derive(Clone, Debug)]
pub struct HighlightFieldConfig {
    pub field: String,
    pub fragment_size: usize,
    pub number_of_fragments: usize,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HighlightOrder {
    /// Return fragments in their order of appearance in the field text.
    None,
    /// Return fragments ordered by relevance score (best match first).
    Score,
}

// --- Public output type ---

/// A single match span within a field's text.
///
/// The library returns match positions, not pre-rendered markup.
/// Consumers decide how to present matches (HTML tags, React nodes,
/// terminal colour codes, JSON, ...). See
/// [[feature-search-highlight]] and
/// [[architecture-scoring-materialization-separation]].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Highlight {
    /// The substring of the field text that matched.
    pub text: String,
    /// Byte offset in the original field text where the match starts.
    pub start: usize,
    /// Byte offset where the match ends (exclusive).
    pub end: usize,
}

// --- Internal types ---

#[derive(Clone, Debug)]
struct MatchedToken {
    offset_from: usize,
    offset_to: usize,
}

// --- Constants ---

const BOUNDARY_CHARS: &[char] = &['.', ',', '!', '?', ' ', '\t', '\n'];
const BOUNDARY_MAX_SCAN: usize = 20;

// --- Term extraction from query expression ---

/// Extract query terms from the expression tree, keyed by field name.
///
/// Walks the expression recursively. For text-analyzed queries (Match,
/// MatchPhrase), the query text is re-analyzed to get the actual
/// indexed terms.
///
/// See [[feature-search-highlight]].
pub(crate) fn extract_query_terms(
    ast: &ScoringExpression,
    searcher: &crate::search::searcher::Searcher,
) -> HashMap<String, HashSet<String>> {
    let mut terms: HashMap<String, HashSet<String>> = HashMap::new();
    collect_terms(ast, searcher, &mut terms);
    terms
}

fn collect_terms(
    ast: &ScoringExpression,
    searcher: &crate::search::searcher::Searcher,
    terms: &mut HashMap<String, HashSet<String>>,
) {
    let analyzers = searcher.analyzers();
    match ast {
        ScoringExpression::Term { field, value } => {
            terms
                .entry(field.clone())
                .or_default()
                .insert(value.clone());
        }
        ScoringExpression::Terms { field, values } => {
            let set = terms.entry(field.clone()).or_default();
            for v in values {
                set.insert(v.clone());
            }
        }
        ScoringExpression::Match {
            field,
            query,
            analyzer,
        } => {
            let analyzer_name = searcher.resolve_search_analyzer(field, analyzer.as_deref());
            let a = analyzers.get(analyzer_name);
            let tokens = a.analyze(query);
            let set = terms.entry(field.clone()).or_default();
            for token in tokens {
                set.insert(token.text);
            }
        }
        ScoringExpression::MatchPhrase {
            field,
            query,
            analyzer,
        } => {
            let analyzer_name = searcher.resolve_search_analyzer(field, analyzer.as_deref());
            let a = analyzers.get(analyzer_name);
            let tokens = a.analyze(query);
            let set = terms.entry(field.clone()).or_default();
            for token in tokens {
                set.insert(token.text);
            }
        }
        ScoringExpression::MatchBoolPrefix {
            field,
            query,
            analyzer,
        } => {
            let analyzer_name = searcher.resolve_search_analyzer(field, analyzer.as_deref());
            let a = analyzers.get(analyzer_name);
            let tokens = a.analyze(query);
            let set = terms.entry(field.clone()).or_default();
            for token in tokens {
                set.insert(token.text);
            }
        }
        ScoringExpression::Prefix { field, value } => {
            terms
                .entry(field.clone())
                .or_default()
                .insert(value.clone());
        }
        ScoringExpression::Fuzzy { field, value, .. } => {
            terms
                .entry(field.clone())
                .or_default()
                .insert(value.clone());
        }
        ScoringExpression::Wildcard { field, value } => {
            terms
                .entry(field.clone())
                .or_default()
                .insert(value.clone());
        }
        ScoringExpression::MultiMatch {
            fields,
            query,
            analyzer,
            ..
        } => {
            // For multi_match, use the first field's analyzer for all fields
            let analyzer_name = if let Some(f) = fields.first() {
                searcher.resolve_search_analyzer(f, analyzer.as_deref())
            } else {
                "standard"
            };
            let a = analyzers.get(analyzer_name);
            let tokens = a.analyze(query);
            for f in fields {
                let set = terms.entry(f.clone()).or_default();
                for token in &tokens {
                    set.insert(token.text.clone());
                }
            }
        }
        // All span queries delegate to the SpanExpression walker —
        // single arm, one concept.
        ScoringExpression::Span(span_ast) => {
            collect_span_terms(span_ast, searcher, terms);
        }
        // Recursive cases — descend into sub-queries
        ScoringExpression::Bool {
            must,
            should,
            must_not: _,
            filter,
            ..
        } => {
            for q in must.iter().chain(should).chain(filter) {
                collect_terms(q, searcher, terms);
            }
            // must_not terms are intentionally excluded — they are
            // exclusion criteria, not highlight targets.
        }
        ScoringExpression::DisMax { queries, .. } => {
            for q in queries {
                collect_terms(q, searcher, terms);
            }
        }
        ScoringExpression::ConstantScore { query, .. }
        | ScoringExpression::Boost { query, .. }
        | ScoringExpression::Nested { query, .. } => {
            collect_terms(query, searcher, terms);
        }
        ScoringExpression::ScriptScore { query, .. }
        | ScoringExpression::FunctionScore { query, .. } => {
            collect_terms(query, searcher, terms);
        }
        ScoringExpression::Boosting { positive, .. } => {
            collect_terms(positive, searcher, terms);
        }
        // Leaf queries with no terms to extract
        ScoringExpression::Exists { .. }
        | ScoringExpression::Range { .. }
        | ScoringExpression::GeoDistance { .. }
        | ScoringExpression::GeoBoundingBox { .. }
        | ScoringExpression::GeoShape { .. }
        | ScoringExpression::Regexp { .. }
        | ScoringExpression::Knn { .. }
        | ScoringExpression::MatchAll
        | ScoringExpression::MatchNone => {}
    }
}

/// Walk a span AST and collect terms for highlighting. Mirrors
/// [`collect_terms`] but operates on the span-typed subset.
fn collect_span_terms(
    ast: &SpanExpression,
    searcher: &crate::search::searcher::Searcher,
    terms: &mut HashMap<String, HashSet<String>>,
) {
    let _ = searcher;
    match ast {
        SpanExpression::SpanTerm { field, value } => {
            terms
                .entry(field.clone())
                .or_default()
                .insert(value.clone());
        }
        SpanExpression::SpanNear {
            field,
            terms: near_terms,
            ..
        } => {
            let set = terms.entry(field.clone()).or_default();
            for v in near_terms {
                set.insert(v.clone());
            }
        }
        SpanExpression::SpanNot { include, .. } => {
            // Exclude side is negation, not highlight-worthy.
            collect_span_terms(include, searcher, terms);
        }
        SpanExpression::SpanFirst { query, .. } => {
            collect_span_terms(query, searcher, terms);
        }
    }
}

// --- Token matching ---

/// Re-analyze field text and find matching token positions.
fn find_matching_tokens(
    text: &str,
    query_terms: &HashSet<String>,
    analyzer: &Analyzer,
) -> Vec<MatchedToken> {
    let tokens = analyzer.analyze(text);
    tokens
        .into_iter()
        .filter(|token| query_terms.contains(&token.text))
        .map(|token| MatchedToken {
            offset_from: token.offset_from,
            offset_to: token.offset_to,
        })
        .collect()
}

// --- Fragment selection ---

/// Select the best fragments from the field text.
///
/// Uses a greedy approach: score each candidate window by summing
/// the weights of matched terms it contains, then greedily pick
/// the top non-overlapping fragments.
fn select_fragments(
    text: &str,
    matches: &[MatchedToken],
    fragment_size: usize,
    number_of_fragments: usize,
    order: HighlightOrder,
) -> Vec<(usize, usize, f32)> {
    if matches.is_empty() {
        return Vec::new();
    }
    if number_of_fragments == 0 {
        return vec![(0, text.len(), 1.0)];
    }

    // Build candidate windows centered on each match
    let mut candidates: Vec<(usize, usize, f32)> = Vec::new();

    for m in matches {
        let center = (m.offset_from + m.offset_to) / 2;
        let half = fragment_size / 2;
        let raw_start = center.saturating_sub(half);
        let raw_end = (raw_start + fragment_size).min(text.len());

        let start = snap_to_boundary(text, raw_start, true);
        let end = snap_to_boundary(text, raw_end, false);

        // Score: count matching tokens in this window
        let score: f32 = matches
            .iter()
            .filter(|t| t.offset_from >= start && t.offset_to <= end)
            .count() as f32;

        candidates.push((start, end, score));
    }

    // Sort by score descending, then by position
    candidates.sort_by(|a, b| {
        b.2.partial_cmp(&a.2)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.0.cmp(&b.0))
    });

    // Greedily select non-overlapping fragments
    let mut selected: Vec<(usize, usize, f32)> = Vec::new();
    for (start, end, score) in candidates {
        if selected.len() >= number_of_fragments {
            break;
        }
        let overlaps = selected.iter().any(|(s, e, _)| start < *e && end > *s);
        if !overlaps {
            selected.push((start, end, score));
        }
    }

    // Order: by score or by position
    match order {
        HighlightOrder::Score => {
            selected.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
        }
        HighlightOrder::None => {
            selected.sort_by_key(|f| f.0);
        }
    }

    selected
}

/// Snap a byte offset to a nearby boundary character.
fn snap_to_boundary(text: &str, offset: usize, snap_left: bool) -> usize {
    if offset == 0 || offset >= text.len() {
        return offset;
    }
    let scan_range = if snap_left {
        offset.saturating_sub(BOUNDARY_MAX_SCAN)..offset
    } else {
        offset..(offset + BOUNDARY_MAX_SCAN).min(text.len())
    };

    if snap_left {
        for i in (scan_range.start..scan_range.end).rev() {
            if text.is_char_boundary(i) {
                if let Some(c) = text[i..].chars().next() {
                    if BOUNDARY_CHARS.contains(&c) {
                        return i + c.len_utf8();
                    }
                }
            }
        }
    } else {
        for i in scan_range {
            if text.is_char_boundary(i) {
                if let Some(c) = text[i..].chars().next() {
                    if BOUNDARY_CHARS.contains(&c) {
                        return i;
                    }
                }
            }
        }
    }

    offset
}

// --- Top-level highlight function ---

/// Generate match spans for a single search hit.
///
/// Reads field text from `_source`, re-analyses with the field's
/// index-time analyser, matches terms, and emits the resulting
/// positions as [`Highlight`] spans. No rendering/tagging happens
/// here — consumers decide presentation.
///
/// The optional fragment-selection step (when
/// `number_of_fragments > 0`) caps output to spans within the top-N
/// fragment windows by score; otherwise all matches are returned in
/// positional order.
///
/// See [[feature-search-highlight]].
pub fn highlight_hit(
    source: &serde_json::Value,
    config: &HighlightConfig,
    query_terms: &HashMap<String, HashSet<String>>,
    analyzers: &AnalyzerRegistry,
    mapping: Option<&crate::mapping::Mapping>,
) -> Option<HashMap<String, Vec<Highlight>>> {
    let obj = source.as_object()?;
    let mut out: HashMap<String, Vec<Highlight>> = HashMap::new();

    for field_config in &config.fields {
        let field_name = &field_config.field;

        let all_terms: HashSet<String>;
        let effective_terms = if config.require_field_match {
            match query_terms.get(field_name) {
                Some(t) => t,
                None => continue,
            }
        } else {
            all_terms = query_terms.values().flatten().cloned().collect();
            &all_terms
        };

        if effective_terms.is_empty() {
            continue;
        }

        let text = match obj.get(field_name) {
            Some(serde_json::Value::String(s)) if !s.is_empty() => s.as_str(),
            _ => continue,
        };

        // For highlighting, tokenise with the field's index-time analyser.
        let field_analyzer_name = mapping
            .and_then(|m| m.field_id(field_name))
            .and_then(|fid| mapping.unwrap().field(fid).analyzer.as_deref())
            .unwrap_or("standard");
        let analyzer = analyzers.get(field_analyzer_name);

        let matches = find_matching_tokens(text, effective_terms, analyzer);
        if matches.is_empty() {
            continue;
        }

        let spans = spans_for_field(text, &matches, field_config, config.order);
        if !spans.is_empty() {
            out.insert(field_name.clone(), spans);
        }
    }

    if out.is_empty() { None } else { Some(out) }
}

/// Convert raw matches into `Highlight` spans, optionally capped by the
/// field config's fragment-selection knobs.
///
/// When `number_of_fragments == 0`, returns every match in positional
/// order. Otherwise runs the fragment selector and retains only the
/// matches that fall inside the selected windows.
fn spans_for_field(
    text: &str,
    matches: &[MatchedToken],
    field_config: &HighlightFieldConfig,
    order: HighlightOrder,
) -> Vec<Highlight> {
    if field_config.number_of_fragments == 0 {
        let mut spans: Vec<Highlight> = matches.iter().map(|m| span_from_match(text, m)).collect();
        spans.sort_by_key(|s| s.start);
        return spans;
    }

    let fragments = select_fragments(
        text,
        matches,
        field_config.fragment_size,
        field_config.number_of_fragments,
        order,
    );

    let mut spans: Vec<Highlight> = Vec::new();
    for (fstart, fend, _score) in &fragments {
        for m in matches {
            if m.offset_from >= *fstart && m.offset_to <= *fend {
                spans.push(span_from_match(text, m));
            }
        }
    }
    spans.sort_by_key(|s| s.start);
    spans.dedup_by_key(|s| (s.start, s.end));
    spans
}

fn span_from_match(text: &str, m: &MatchedToken) -> Highlight {
    Highlight {
        text: text[m.offset_from..m.offset_to].to_string(),
        start: m.offset_from,
        end: m.offset_to,
    }
}

// --- Request parsing ---

/// Parse a highlight config from the ES-shape search-request JSON.
///
/// Ignores ``pre_tags`` / ``post_tags`` / ``no_match_size`` / ``encoder``
/// keys: those are rendering concerns and no longer affect output. Kept
/// here for ES copy-paste compatibility — see
/// [[fix-strict-search-parsing]].
pub fn parse_highlight_config(json: &serde_json::Value) -> HighlightConfig {
    let require_field_match = json
        .get("require_field_match")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    let order = match json.get("order").and_then(|v| v.as_str()) {
        Some("score") => HighlightOrder::Score,
        _ => HighlightOrder::None,
    };

    let fields = json
        .get("fields")
        .and_then(|v| v.as_object())
        .map(|obj| {
            obj.iter()
                .map(|(name, field_json)| {
                    let fragment_size = field_json
                        .get("fragment_size")
                        .and_then(|v| v.as_u64())
                        .map(|v| v as usize)
                        .unwrap_or(100);
                    let number_of_fragments = field_json
                        .get("number_of_fragments")
                        .and_then(|v| v.as_u64())
                        .map(|v| v as usize)
                        .unwrap_or(5);
                    HighlightFieldConfig {
                        field: name.clone(),
                        fragment_size,
                        number_of_fragments,
                    }
                })
                .collect()
        })
        .unwrap_or_default();

    HighlightConfig {
        fields,
        require_field_match,
        order,
    }
}