kevy-text 7.0.0

Dictionary-free full-text search core: CJK bigram tokenizer, inverted segments, BM25 scoring.
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
//! What a query asks for, beyond its text and result limit — the clause
//! options and the shapes they answer in. A child module of `segment`
//! (declared via `#[path]`), re-exported from it, so the crate's public
//! surface is unchanged by the split.

use std::collections::HashMap;

use super::{Distinct, Filter, Sort};

/// One ranked hit.
///
/// ```
/// let m = kevy_text::TextMatch::new(b"doc:1".to_vec(), 1.5);
/// assert_eq!((m.key.as_slice(), m.score), (&b"doc:1"[..], 1.5));
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct TextMatch {
    /// Row key.
    ///
    /// ```
    /// let mut seg = kevy_text::TextSegment::new();
    /// seg.apply(b"user:7", Some(b"likes rust"));
    /// assert_eq!(seg.matches(b"rust", 10)[0].key, b"user:7");
    /// ```
    pub key: Vec<u8>,
    /// Shard-local BM25 score.
    ///
    /// Higher is better; the page comes back best first.
    ///
    /// ```
    /// let mut seg = kevy_text::TextSegment::new();
    /// seg.apply(b"short", Some(b"rust"));
    /// seg.apply(b"long", Some(b"rust in a much longer document"));
    /// let hits = seg.matches(b"rust", 10);
    /// assert_eq!(hits[0].key, b"short");
    /// assert!(hits[0].score > hits[1].score && hits[1].score > 0.0);
    /// ```
    pub score: f64,
}

impl TextMatch {
    /// A hit for `key` at `score` — how a merge of several segments'
    /// pages, or of hot and cold hits, reports its union.
    ///
    /// ```
    /// let m = kevy_text::TextMatch::new(b"k".to_vec(), 0.25);
    /// assert_eq!(m.score, 0.25);
    /// ```
    #[must_use]
    pub fn new(key: Vec<u8>, score: f64) -> Self {
        Self { key, score }
    }
}

/// Corpus statistics supplied from outside a segment, for scoring one
/// shard's documents against the whole corpus rather than its own slice.
///
/// A cross-shard text query builds this by summing each shard's local
/// `n_docs` / `total_len` and, for each query token, its `df`. `df`
/// need only carry the query's tokens — the values a query actually
/// scores with — which is why global BM25 does not need a whole-corpus
/// df table.
///
/// ```
/// use std::collections::HashMap;
/// let stats = kevy_text::CorpusStats::new(10.0, 4.0, HashMap::from([(b"rust".to_vec(), 3)]));
/// assert_eq!(stats.df.get(&b"rust"[..]), Some(&3));
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct CorpusStats {
    /// Total documents across the corpus.
    ///
    /// One match among a thousand documents is rarer, so it scores higher,
    /// than the same match among two:
    ///
    /// ```
    /// use kevy_text::{CorpusStats, TextSegment};
    /// use std::collections::HashMap;
    /// let mut seg = TextSegment::new();
    /// seg.apply(b"doc:1", Some(b"rust engine"));
    /// let df = HashMap::from([(b"rust".to_vec(), 1)]);
    /// let small = CorpusStats::new(2.0, 2.0, df.clone());
    /// let large = CorpusStats::new(1000.0, 2.0, df);
    /// let score = |s| seg.matches_scored(b"rust", 1, Some(s))[0].score;
    /// assert!(score(&large) > score(&small));
    /// ```
    pub n_docs: f64,
    /// Mean document length (unweighted tokens) across the corpus.
    ///
    /// The same document is short against a corpus of long documents,
    /// and BM25 rewards a match in a short one:
    ///
    /// ```
    /// use kevy_text::{CorpusStats, TextSegment};
    /// use std::collections::HashMap;
    /// let mut seg = TextSegment::new();
    /// seg.apply(b"doc:1", Some(b"rust engine"));
    /// let df = HashMap::from([(b"rust".to_vec(), 1)]);
    /// let terse = CorpusStats::new(10.0, 2.0, df.clone());
    /// let verbose = CorpusStats::new(10.0, 50.0, df);
    /// let score = |s| seg.matches_scored(b"rust", 1, Some(s))[0].score;
    /// assert!(score(&verbose) > score(&terse));
    /// ```
    pub avgdl: f64,
    /// Global document frequency per query token; a token missing here
    /// falls back to the segment's local list length.
    ///
    /// ```
    /// use kevy_text::{CorpusStats, TextSegment};
    /// use std::collections::HashMap;
    /// let mut seg = TextSegment::new();
    /// seg.apply(b"doc:1", Some(b"rust engine"));
    /// let rare = CorpusStats::new(100.0, 2.0, HashMap::from([(b"rust".to_vec(), 1)]));
    /// let common = CorpusStats::new(100.0, 2.0, HashMap::from([(b"rust".to_vec(), 90)]));
    /// let score = |s| seg.matches_scored(b"rust", 1, Some(s))[0].score;
    /// assert!(score(&rare) > score(&common));
    /// // absent from `df`: this segment's own count (1) stands in
    /// let fallback = CorpusStats::new(100.0, 2.0, HashMap::new());
    /// assert_eq!(score(&fallback), score(&rare));
    /// ```
    pub df: HashMap<Vec<u8>, u32>,
}

impl CorpusStats {
    /// Statistics for a corpus of `n_docs` documents of mean length
    /// `avgdl`, with the document frequency of each query token.
    ///
    /// ```
    /// let s = kevy_text::CorpusStats::new(2.0, 3.5, Default::default());
    /// assert_eq!((s.n_docs, s.avgdl), (2.0, 3.5));
    /// ```
    #[must_use]
    pub fn new(n_docs: f64, avgdl: f64, df: HashMap<Vec<u8>, u32>) -> Self {
        Self { n_docs, avgdl, df }
    }
}

/// Which way an order runs: ascending or descending.
///
/// ```
/// use std::cmp::Ordering;
/// use kevy_text::SortOrder;
/// assert_eq!(SortOrder::Asc.apply(1.cmp(&2)), Ordering::Less);
/// assert_eq!(SortOrder::Desc.apply(1.cmp(&2)), Ordering::Greater);
/// assert_eq!(SortOrder::default(), SortOrder::Asc);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SortOrder {
    /// Smallest first.
    ///
    /// ```
    /// # use kevy_text::{QueryOpts, SegmentShape, Sort, SortOrder, TextSegment};
    /// # let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
    /// # for (k, text, price) in [("a", "red shoe", "30"), ("b", "red hat", "10"), ("c", "red cap", "20")] {
    /// #     seg.apply_doc(k.as_bytes(), Some(&[(text.as_bytes().to_vec(), 1.0)]), &[Some(price.as_bytes())]);
    /// # }
    /// // a "red shoe" 30, b "red hat" 10, c "red cap" 20
    /// let price = |v: &[u8]| Some(v.to_vec());
    /// let cheapest = QueryOpts::default().with_sort(Sort::new(0, &price).with_order(SortOrder::Asc));
    /// let page = seg.matches_query_with(b"red", 2, cheapest);
    /// assert_eq!(page.iter().map(|m| &m.key[..]).collect::<Vec<_>>(), [&b"b"[..], b"c"]);
    /// ```
    #[default]
    Asc,
    /// Largest first.
    ///
    /// ```
    /// # use kevy_text::{QueryOpts, SegmentShape, Sort, SortOrder, TextSegment};
    /// # let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
    /// # for (k, text, price) in [("a", "red shoe", "30"), ("b", "red hat", "10"), ("c", "red cap", "20")] {
    /// #     seg.apply_doc(k.as_bytes(), Some(&[(text.as_bytes().to_vec(), 1.0)]), &[Some(price.as_bytes())]);
    /// # }
    /// // a "red shoe" 30, b "red hat" 10, c "red cap" 20
    /// let price = |v: &[u8]| Some(v.to_vec());
    /// let dearest = QueryOpts::default().with_sort(Sort::new(0, &price).with_order(SortOrder::Desc));
    /// let page = seg.matches_query_with(b"red", 2, dearest);
    /// assert_eq!(page.iter().map(|m| &m.key[..]).collect::<Vec<_>>(), [&b"a"[..], b"c"]);
    /// ```
    Desc,
}

impl SortOrder {
    /// `ord` as this direction sees it: unchanged ascending, reversed
    /// descending.
    ///
    /// ```
    /// use kevy_text::SortOrder;
    /// let mut v = [3, 1, 2];
    /// v.sort_by(|a, b| SortOrder::Desc.apply(a.cmp(b)));
    /// assert_eq!(v, [3, 2, 1]);
    /// ```
    #[must_use]
    pub fn apply(self, ord: std::cmp::Ordering) -> std::cmp::Ordering {
        match self {
            Self::Asc => ord,
            Self::Desc => ord.reverse(),
        }
    }
}

/// One value bucket: the identity a cross-shard merge sums by, a spelling
/// of it that occurs in the corpus, and how many documents matched with
/// it.
///
/// ```
/// use kevy_text::{Bucket, Facet, QueryOpts, SegmentShape, TextSegment};
/// let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
/// seg.apply_doc(b"a", Some(&[(b"red shoe".to_vec(), 1.0)]), &[Some(b"ACME")]);
/// seg.apply_doc(b"b", Some(&[(b"red hat".to_vec(), 1.0)]), &[Some(b"acme")]);
/// // identity folds case; the label is a spelling that really occurs
/// let brand = |v: &[u8]| Some(v.to_ascii_lowercase());
/// let r = seg.matches_query_faceted(b"red", 10, QueryOpts::default(), &[Facet::new(0, &brand)]);
/// let (identity, label, count): &Bucket = &r.facets[0][0];
/// assert_eq!((&identity[..], count), (&b"acme"[..], &2));
/// assert!(label == b"ACME" || label == b"acme");
/// ```
pub type Bucket = (Vec<u8>, Vec<u8>, u64);

/// One faceted query's answer: the page, and a count per value for each
/// requested field.
///
/// ```
/// use kevy_text::{Facet, QueryOpts, SegmentShape, TextSegment};
/// let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
/// for (k, text, color) in [("a", "wool hat", "red"), ("b", "wool scarf", "red"), ("c", "wool sock", "blue")] {
///     seg.apply_doc(k.as_bytes(), Some(&[(text.as_bytes().to_vec(), 1.0)]), &[Some(color.as_bytes())]);
/// }
/// let color = |v: &[u8]| Some(v.to_vec());
/// let r = seg.matches_query_faceted(b"wool", 1, QueryOpts::default(), &[Facet::new(0, &color)]);
/// // one hit on the page, but the counts cover all three matches
/// assert_eq!(r.hits.len(), 1);
/// assert_eq!(r.facets[0], [(b"red".to_vec(), b"red".to_vec(), 2), (b"blue".to_vec(), b"blue".to_vec(), 1)]);
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct FacetedMatches {
    /// The ranked page, exactly what an unfaceted query would return.
    ///
    /// ```
    /// use kevy_text::{Facet, QueryOpts, SegmentShape, TextSegment};
    /// let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
    /// seg.apply_doc(b"a", Some(&[(b"wool hat".to_vec(), 1.0)]), &[Some(b"red")]);
    /// seg.apply_doc(b"b", Some(&[(b"wool wool sock".to_vec(), 1.0)]), &[Some(b"blue")]);
    /// let color = |v: &[u8]| Some(v.to_vec());
    /// let r = seg.matches_query_faceted(b"wool", 10, QueryOpts::default(), &[Facet::new(0, &color)]);
    /// assert_eq!(r.hits, seg.matches_query_with(b"wool", 10, QueryOpts::default()));
    /// ```
    pub hits: Vec<TextMatch>,
    /// Per requested facet field, `(identity, label, count)` over the
    /// whole match set. The identity is what a cross-shard merge sums by;
    /// the label is what it reports.
    ///
    /// One list per requested facet, in request order, most frequent
    /// value first:
    ///
    /// ```
    /// use kevy_text::{Facet, QueryOpts, SegmentShape, TextSegment};
    /// let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(2));
    /// seg.apply_doc(b"a", Some(&[(b"wool hat".to_vec(), 1.0)]), &[Some(b"red"), Some(b"S")]);
    /// seg.apply_doc(b"b", Some(&[(b"wool sock".to_vec(), 1.0)]), &[Some(b"blue"), Some(b"S")]);
    /// let raw = |v: &[u8]| Some(v.to_vec());
    /// let facets = [Facet::new(1, &raw), Facet::new(0, &raw)];
    /// let r = seg.matches_query_faceted(b"wool", 10, QueryOpts::default(), &facets);
    /// assert_eq!(r.facets.len(), 2);
    /// assert_eq!(r.facets[0], [(b"S".to_vec(), b"S".to_vec(), 2)]);
    /// assert_eq!(r.facets[1].len(), 2);
    /// ```
    pub facets: Vec<Vec<Bucket>>,
}

/// Everything a MATCH query carries beyond its text and result limit.
///
/// Grouping them keeps the query entry point from growing a parameter per
/// clause, and gives every clause one place to be defaulted from
/// ([`QueryOpts::default`] is the plain, exact, unscoped query). Set what
/// differs with the `with_*` builders or by assigning the fields.
///
/// ```
/// let q = kevy_text::QueryOpts::default().with_typo(1).with_fields(&[0]);
/// assert_eq!((q.typo, q.fields), (1, &[0usize][..]));
/// ```
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct QueryOpts<'a> {
    /// Corpus-wide BM25 statistics — the second pass of a cross-shard
    /// query. `None` scores against this segment's own slice.
    ///
    /// ```
    /// use kevy_text::{CorpusStats, QueryOpts, TextSegment};
    /// use std::collections::HashMap;
    /// let mut seg = TextSegment::new();
    /// seg.apply(b"doc:1", Some(b"rust engine"));
    /// let corpus = CorpusStats::new(50.0, 4.0, HashMap::from([(b"rust".to_vec(), 5)]));
    /// let mut opts = QueryOpts::default();
    /// opts.stats = Some(&corpus);
    /// let global = seg.matches_query_with(b"rust", 1, opts)[0].score;
    /// assert_eq!(global, seg.matches_scored(b"rust", 1, Some(&corpus))[0].score);
    /// assert_ne!(global, seg.matches_query_with(b"rust", 1, QueryOpts::default())[0].score);
    /// ```
    pub stats: Option<&'a CorpusStats>,
    /// Edit distance allowed on bare terms (`TYPO n`); 0 = exact.
    ///
    /// ```
    /// use kevy_text::{QueryOpts, TextSegment};
    /// let mut seg = TextSegment::new();
    /// seg.apply(b"doc:1", Some(b"kevy storage"));
    /// assert!(seg.matches_query_with(b"storge", 10, QueryOpts::default()).is_empty());
    /// let mut fuzzy = QueryOpts::default();
    /// fuzzy.typo = 1;
    /// assert_eq!(seg.matches_query_with(b"storge", 10, fuzzy)[0].key, b"doc:1");
    /// ```
    pub typo: u32,
    /// Field positions the query is restricted to (`IN <field…>`); empty
    /// = every field.
    ///
    /// ```
    /// use kevy_text::{QueryOpts, SegmentShape, TextSegment};
    /// let mut seg = TextSegment::with_shape(SegmentShape::default().with_fields(2));
    /// // field 0 = title, field 1 = body
    /// seg.apply_fields(b"doc:1", Some(&[(b"kevy".to_vec(), 1.0), (b"about redis".to_vec(), 1.0)]));
    /// assert_eq!(seg.matches_query_with(b"redis", 10, QueryOpts::default()).len(), 1);
    /// let mut titles = QueryOpts::default();
    /// titles.fields = &[0];
    /// assert!(seg.matches_query_with(b"redis", 10, titles).is_empty());
    /// ```
    pub fields: &'a [usize],
    /// `FILTER`: non-scoring predicates, ANDed. Applied before the top-K
    /// — filtering afterwards would return fewer hits than exist.
    ///
    /// ```
    /// # use kevy_text::{Filter, QueryOpts, SegmentShape, TextSegment};
    /// # let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
    /// # for (k, text, price) in [("a", "red shoe", "30"), ("b", "red hat", "10"), ("c", "red cap", "20")] {
    /// #     seg.apply_doc(k.as_bytes(), Some(&[(text.as_bytes().to_vec(), 1.0)]), &[Some(price.as_bytes())]);
    /// # }
    /// // a "red shoe" 30, b "red hat" 10, c "red cap" 20
    /// let under_25 = |v: &[u8]| v < &b"25"[..];
    /// let filter = [Filter::new(0, &under_25)];
    /// let mut opts = QueryOpts::default();
    /// opts.filter = &filter;
    /// let mut keys: Vec<_> = seg.matches_query_with(b"red", 10, opts).into_iter().map(|m| m.key).collect();
    /// keys.sort();
    /// assert_eq!(keys, [b"b".to_vec(), b"c".to_vec()]);
    /// ```
    pub filter: &'a [Filter<'a>],
    /// `SORT`: select by a stored value instead of by score. Selecting,
    /// not re-ordering: a document that wins on the sort key must be
    /// chosen even when its score would never have reached the page.
    ///
    /// ```
    /// use kevy_text::{QueryOpts, SegmentShape, Sort, TextSegment};
    /// let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
    /// seg.apply_doc(b"best", Some(&[(b"red red red".to_vec(), 1.0)]), &[Some(b"2024")]);
    /// seg.apply_doc(b"oldest", Some(&[(b"red and many other words".to_vec(), 1.0)]), &[Some(b"1999")]);
    /// assert_eq!(seg.matches_query_with(b"red", 1, QueryOpts::default())[0].key, b"best");
    /// let year = |v: &[u8]| Some(v.to_vec());
    /// let mut opts = QueryOpts::default();
    /// opts.sort = Some(Sort::new(0, &year));
    /// assert_eq!(seg.matches_query_with(b"red", 1, opts)[0].key, b"oldest");
    /// ```
    pub sort: Option<Sort<'a>>,
    /// `DISTINCT`: at most one hit per value of a stored field. Applied
    /// during selection, so the page is filled with `limit` DISTINCT
    /// documents rather than `limit` documents that then collapse.
    ///
    /// ```
    /// use kevy_text::{Distinct, QueryOpts, SegmentShape, TextSegment};
    /// let mut seg = TextSegment::with_shape(SegmentShape::default().with_values(1));
    /// seg.apply_doc(b"a1", Some(&[(b"red red".to_vec(), 1.0)]), &[Some(b"acme")]);
    /// seg.apply_doc(b"a2", Some(&[(b"red shoe".to_vec(), 1.0)]), &[Some(b"acme")]);
    /// seg.apply_doc(b"z1", Some(&[(b"red hat and scarf".to_vec(), 1.0)]), &[Some(b"zeta")]);
    /// let brand = |v: &[u8]| Some(v.to_vec());
    /// let mut opts = QueryOpts::default();
    /// opts.distinct = Some(Distinct::new(0, &brand));
    /// // two slots, filled by two different brands
    /// let page = seg.matches_query_with(b"red", 2, opts);
    /// assert_eq!(page.iter().map(|m| &m.key[..]).collect::<Vec<_>>(), [&b"a1"[..], b"z1"]);
    /// ```
    pub distinct: Option<Distinct<'a>>,
}

impl<'a> QueryOpts<'a> {
    /// Score against corpus-wide `stats` rather than this segment's slice.
    ///
    /// ```
    /// let s = kevy_text::CorpusStats::default();
    /// assert!(kevy_text::QueryOpts::default().with_stats(&s).stats.is_some());
    /// ```
    #[must_use]
    pub fn with_stats(mut self, stats: &'a CorpusStats) -> Self {
        self.stats = Some(stats);
        self
    }

    /// Allow `typo` edits on bare terms.
    ///
    /// ```
    /// assert_eq!(kevy_text::QueryOpts::default().with_typo(2).typo, 2);
    /// ```
    #[must_use]
    pub fn with_typo(mut self, typo: u32) -> Self {
        self.typo = typo;
        self
    }

    /// Restrict the query to these field positions.
    ///
    /// ```
    /// assert_eq!(kevy_text::QueryOpts::default().with_fields(&[1, 2]).fields, &[1, 2]);
    /// ```
    #[must_use]
    pub fn with_fields(mut self, fields: &'a [usize]) -> Self {
        self.fields = fields;
        self
    }

    /// Keep only documents every one of `filter` passes.
    ///
    /// ```
    /// let any = |_: &[u8]| true;
    /// let f = [kevy_text::Filter::new(0, &any)];
    /// assert_eq!(kevy_text::QueryOpts::default().with_filter(&f).filter.len(), 1);
    /// ```
    #[must_use]
    pub fn with_filter(mut self, filter: &'a [Filter<'a>]) -> Self {
        self.filter = filter;
        self
    }

    /// Select by a stored value instead of by score.
    ///
    /// ```
    /// let key = |v: &[u8]| Some(v.to_vec());
    /// let q = kevy_text::QueryOpts::default().with_sort(kevy_text::Sort::new(0, &key));
    /// assert!(q.sort.is_some());
    /// ```
    #[must_use]
    pub fn with_sort(mut self, sort: Sort<'a>) -> Self {
        self.sort = Some(sort);
        self
    }

    /// At most one hit per value of a stored field.
    ///
    /// ```
    /// let key = |v: &[u8]| Some(v.to_vec());
    /// let q = kevy_text::QueryOpts::default().with_distinct(kevy_text::Distinct::new(0, &key));
    /// assert!(q.distinct.is_some());
    /// ```
    #[must_use]
    pub fn with_distinct(mut self, distinct: Distinct<'a>) -> Self {
        self.distinct = Some(distinct);
        self
    }
}