laurus 0.9.0

Unified search library for lexical, vector, and semantic retrieval
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
//! Configuration types for custom analyzer definitions within a schema.
//!
//! These types allow users to declaratively define custom text analyzers
//! composed of a tokenizer, optional char filters, and optional token
//! filters. Definitions are stored in the schema's `analyzers` map and
//! referenced by name from [`TextOption::analyzer`].
//!
//! # JSON Format
//!
//! ```json
//! {
//!   "char_filters": [{"type": "unicode_normalization", "form": "nfkc"}],
//!   "tokenizer": {"type": "regex", "pattern": "\\w+"},
//!   "token_filters": [{"type": "lowercase"}, {"type": "stop"}]
//! }
//! ```

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// Reference to an analyzer for a text field.
///
/// Two shapes are accepted (decoded via serde's untagged representation):
///
/// 1. **A bare string** — the name of a built-in or user-defined analyzer.
///    Example: `"standard"`, `"english"`, `"keyword"`, `"simple"`,
///    `"noop"`, or any name registered in [`Schema::analyzers`].
/// 2. **A structured object** — a parameterized built-in analyzer.
///    Currently only the language-specific Japanese preset:
///    `{"language": "japanese", "mode": "normal", "dict": "/path/to/ipadic"}`.
///
/// # JSON Examples
///
/// ```json
/// // Built-in preset that needs no parameters.
/// "standard"
///
/// // Japanese preset that requires a dictionary path.
/// {"language": "japanese", "mode": "normal", "dict": "/var/lib/lindera/ipadic"}
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AnalyzerSpec {
    /// Built-in or user-defined analyzer referenced by name.
    Named(String),
    /// Parameterized built-in analyzer.
    Builtin(BuiltinAnalyzerSpec),
}

/// Parameterized built-in analyzer presets.
///
/// Variants are tagged by the `"language"` discriminator in JSON.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "language", rename_all = "snake_case")]
pub enum BuiltinAnalyzerSpec {
    /// Japanese analyzer (Lindera + Japanese filters).
    Japanese {
        /// Lindera segmentation mode: `"normal"`, `"search"`, or
        /// `"decompose"`. Defaults to `"normal"` when omitted.
        #[serde(default = "default_lindera_mode")]
        mode: String,
        /// Filesystem path to the lindera dictionary directory
        /// (e.g. `/var/lib/lindera/ipadic`).
        dict: String,
        /// Optional user dictionary path.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        user_dict: Option<String>,
    },
}

impl From<&str> for AnalyzerSpec {
    fn from(value: &str) -> Self {
        AnalyzerSpec::Named(value.to_string())
    }
}

impl From<String> for AnalyzerSpec {
    fn from(value: String) -> Self {
        AnalyzerSpec::Named(value)
    }
}

impl From<BuiltinAnalyzerSpec> for AnalyzerSpec {
    fn from(value: BuiltinAnalyzerSpec) -> Self {
        AnalyzerSpec::Builtin(value)
    }
}

fn default_lindera_mode() -> String {
    "normal".to_string()
}

/// A custom analyzer definition composed of a tokenizer and optional
/// char/token filter chains.
///
/// # Fields
///
/// * `char_filters` - Applied to raw text before tokenization.
/// * `tokenizer` - Splits text into tokens.
/// * `token_filters` - Applied sequentially to the token stream.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalyzerDefinition {
    /// Char filters applied to raw text before tokenization.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub char_filters: Vec<CharFilterConfig>,

    /// The tokenizer that splits text into tokens.
    pub tokenizer: TokenizerConfig,

    /// Token filters applied to the token stream after tokenization.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub token_filters: Vec<TokenFilterConfig>,
}

/// Configuration for a tokenizer component.
///
/// Uses `{"type": "..."}` JSON format via serde's internally tagged
/// representation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TokenizerConfig {
    /// Splits on whitespace boundaries.
    Whitespace,

    /// Splits on Unicode word boundaries.
    UnicodeWord,

    /// Splits using a regular expression pattern.
    Regex {
        /// The regex pattern (default: `\w+`).
        #[serde(default = "default_regex_pattern")]
        pattern: String,

        /// If `true`, the pattern matches gaps between tokens
        /// rather than the tokens themselves.
        #[serde(default)]
        gaps: bool,
    },

    /// Produces n-grams of the specified size range.
    Ngram {
        /// Minimum n-gram size.
        min_gram: usize,
        /// Maximum n-gram size.
        max_gram: usize,
    },

    /// Morphological tokenizer using Lindera.
    Lindera {
        /// Tokenization mode: `"normal"`, `"search"`, or `"decompose"`.
        mode: String,
        /// Dictionary URI. In production builds, supply a filesystem path
        /// to a Lindera dictionary directory (e.g. `"/var/lib/lindera/ipadic"`).
        /// `embedded://*` URIs are only resolvable when the matching
        /// `embed-*` Lindera feature is enabled, which `laurus` does not do
        /// by default.
        dict: String,
        /// Optional user dictionary URI (filesystem path).
        #[serde(default)]
        user_dict: Option<String>,
    },

    /// Treats the entire input as a single token.
    Whole,
}

/// Configuration for a char filter component.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CharFilterConfig {
    /// Applies Unicode normalization (NFC, NFD, NFKC, or NFKD).
    UnicodeNormalization {
        /// Normalization form: `"nfc"`, `"nfd"`, `"nfkc"`, or `"nfkd"`.
        form: String,
    },

    /// Replaces text matching a regex pattern.
    PatternReplace {
        /// The regex pattern to match.
        pattern: String,
        /// The replacement string.
        replacement: String,
    },

    /// Replaces strings using a mapping dictionary.
    Mapping {
        /// Key-value pairs for replacement.
        mapping: HashMap<String, String>,
    },

    /// Expands Japanese iteration marks (踊り字).
    JapaneseIterationMark {
        /// Whether to normalize kanji iteration marks.
        #[serde(default = "default_true")]
        kanji: bool,
        /// Whether to normalize kana iteration marks.
        #[serde(default = "default_true")]
        kana: bool,
    },
}

/// Configuration for a token filter component.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TokenFilterConfig {
    /// Converts tokens to lowercase.
    Lowercase,

    /// Removes stop words from the token stream.
    Stop {
        /// Custom stop word list. If `None`, uses default English
        /// stop words.
        #[serde(default)]
        words: Option<Vec<String>>,
    },

    /// Applies stemming to tokens.
    Stem {
        /// Stemmer type: `"porter"` (default), `"simple"`, or
        /// `"identity"`.
        #[serde(default)]
        stem_type: Option<String>,
    },

    /// Multiplies token scores by a boost factor.
    Boost {
        /// The boost multiplier.
        boost: f32,
    },

    /// Limits the number of tokens in the stream.
    Limit {
        /// Maximum number of tokens to emit.
        limit: usize,
    },

    /// Strips leading and trailing whitespace from tokens.
    Strip,

    /// Removes empty tokens from the stream.
    RemoveEmpty,

    /// Flattens a synonym graph into a linear token stream.
    FlattenGraph,
}

fn default_regex_pattern() -> String {
    r"\w+".to_string()
}

fn default_true() -> bool {
    true
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_analyzer_definition_serde_roundtrip() {
        let def = AnalyzerDefinition {
            char_filters: vec![CharFilterConfig::UnicodeNormalization {
                form: "nfkc".into(),
            }],
            tokenizer: TokenizerConfig::Regex {
                pattern: r"\w+".into(),
                gaps: false,
            },
            token_filters: vec![
                TokenFilterConfig::Lowercase,
                TokenFilterConfig::Stop {
                    words: Some(vec!["the".into(), "a".into()]),
                },
                TokenFilterConfig::Stem { stem_type: None },
            ],
        };

        let json = serde_json::to_string(&def).unwrap();
        let deserialized: AnalyzerDefinition = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.token_filters.len(), 3);
        assert_eq!(deserialized.char_filters.len(), 1);
    }

    #[test]
    fn test_tokenizer_config_variants() {
        let configs = vec![
            r#"{"type": "whitespace"}"#,
            r#"{"type": "unicode_word"}"#,
            r#"{"type": "regex", "pattern": "\\w+", "gaps": false}"#,
            r#"{"type": "ngram", "min_gram": 2, "max_gram": 3}"#,
            r#"{"type": "whole"}"#,
        ];
        for json in configs {
            let config: TokenizerConfig = serde_json::from_str(json).unwrap();
            let serialized = serde_json::to_string(&config).unwrap();
            let _roundtrip: TokenizerConfig = serde_json::from_str(&serialized).unwrap();
        }
    }

    #[test]
    fn test_char_filter_config_variants() {
        let configs = vec![
            r#"{"type": "unicode_normalization", "form": "nfkc"}"#,
            r#"{"type": "pattern_replace", "pattern": "foo", "replacement": "bar"}"#,
            r#"{"type": "mapping", "mapping": {"a": "b"}}"#,
            r#"{"type": "japanese_iteration_mark"}"#,
        ];
        for json in configs {
            let config: CharFilterConfig = serde_json::from_str(json).unwrap();
            let serialized = serde_json::to_string(&config).unwrap();
            let _roundtrip: CharFilterConfig = serde_json::from_str(&serialized).unwrap();
        }
    }

    #[test]
    fn test_token_filter_config_variants() {
        let configs = vec![
            r#"{"type": "lowercase"}"#,
            r#"{"type": "stop"}"#,
            r#"{"type": "stop", "words": ["the", "a"]}"#,
            r#"{"type": "stem"}"#,
            r#"{"type": "stem", "stem_type": "porter"}"#,
            r#"{"type": "boost", "boost": 2.0}"#,
            r#"{"type": "limit", "limit": 100}"#,
            r#"{"type": "strip"}"#,
            r#"{"type": "remove_empty"}"#,
            r#"{"type": "flatten_graph"}"#,
        ];
        for json in configs {
            let config: TokenFilterConfig = serde_json::from_str(json).unwrap();
            let serialized = serde_json::to_string(&config).unwrap();
            let _roundtrip: TokenFilterConfig = serde_json::from_str(&serialized).unwrap();
        }
    }

    #[test]
    fn test_full_schema_with_analyzers_json() {
        let json = r#"{
            "char_filters": [{"type": "unicode_normalization", "form": "nfkc"}],
            "tokenizer": {"type": "lindera", "mode": "normal", "dict": "embedded://ipadic"},
            "token_filters": [{"type": "lowercase"}]
        }"#;
        let def: AnalyzerDefinition = serde_json::from_str(json).unwrap();
        assert!(matches!(def.tokenizer, TokenizerConfig::Lindera { .. }));
    }

    #[test]
    fn test_minimal_definition() {
        let json = r#"{"tokenizer": {"type": "whitespace"}}"#;
        let def: AnalyzerDefinition = serde_json::from_str(json).unwrap();
        assert!(def.char_filters.is_empty());
        assert!(def.token_filters.is_empty());
    }

    #[test]
    fn test_analyzer_spec_named_string_form() {
        let spec: AnalyzerSpec = serde_json::from_str(r#""standard""#).unwrap();
        assert!(matches!(spec, AnalyzerSpec::Named(ref s) if s == "standard"));

        let serialized = serde_json::to_string(&spec).unwrap();
        assert_eq!(serialized, r#""standard""#);
    }

    #[test]
    fn test_analyzer_spec_japanese_struct_form() {
        let json = r#"{"language": "japanese", "dict": "/var/lib/lindera/ipadic"}"#;
        let spec: AnalyzerSpec = serde_json::from_str(json).unwrap();
        match spec {
            AnalyzerSpec::Builtin(BuiltinAnalyzerSpec::Japanese {
                mode,
                dict,
                user_dict,
            }) => {
                assert_eq!(mode, "normal"); // default
                assert_eq!(dict, "/var/lib/lindera/ipadic");
                assert!(user_dict.is_none());
            }
            other => panic!("expected Japanese variant, got: {other:?}"),
        }
    }

    #[test]
    fn test_analyzer_spec_japanese_with_mode_and_user_dict() {
        let json = r#"{
            "language": "japanese",
            "mode": "search",
            "dict": "/var/lib/lindera/ipadic",
            "user_dict": "/etc/laurus/user.csv"
        }"#;
        let spec: AnalyzerSpec = serde_json::from_str(json).unwrap();
        match spec {
            AnalyzerSpec::Builtin(BuiltinAnalyzerSpec::Japanese {
                mode,
                dict,
                user_dict,
            }) => {
                assert_eq!(mode, "search");
                assert_eq!(dict, "/var/lib/lindera/ipadic");
                assert_eq!(user_dict.as_deref(), Some("/etc/laurus/user.csv"));
            }
            other => panic!("expected Japanese variant, got: {other:?}"),
        }
    }

    #[test]
    fn test_analyzer_spec_serialize_japanese() {
        let spec = AnalyzerSpec::Builtin(BuiltinAnalyzerSpec::Japanese {
            mode: "normal".into(),
            dict: "/var/lib/lindera/ipadic".into(),
            user_dict: None,
        });
        let serialized = serde_json::to_string(&spec).unwrap();
        // Round-trip and inspect.
        let roundtrip: AnalyzerSpec = serde_json::from_str(&serialized).unwrap();
        assert_eq!(spec, roundtrip);
        // The serialized form must include the language discriminator.
        assert!(serialized.contains(r#""language":"japanese""#));
        // user_dict is None and skipped.
        assert!(!serialized.contains("user_dict"));
    }

    #[test]
    fn test_analyzer_spec_from_str_into() {
        let spec: AnalyzerSpec = "english".into();
        assert!(matches!(spec, AnalyzerSpec::Named(ref s) if s == "english"));
    }
}