mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
feat(nodejs): expose code analyzer options on FtsOptions and tokenize (#4889)
Completes #4364 together with #4756, which did the Python side. ## Changes - `Index.fts()` / `FtsOptions` and `tokenize()` gain `analyzer` (`"text"` or `"code"`), `splitIdentifiers`, `splitOnNumerics`, `preserveOriginal` and `indexOperators`. All are optional, so existing indexes and calls behave as before. - Validation matches the Python SDK: `analyzer: "code"` with a `baseTokenizer` other than `"code"` or `"simple"` is rejected, `analyzer: "text"` with `baseTokenizer: "code"` is rejected, and turning on a code flag without the code analyzer is rejected. Passing a flag as `false` is always allowed. The checks live in one helper used by both `fts` and `tokenize`. - The option docs state the defaults (`analyzer: "code"` alone keeps identifiers whole; `splitOnNumerics` and `preserveOriginal` only matter with `splitIdentifiers: true`) and link the two Lance limitations noted on #4756 (lance#9674 and lance#9658). Shared presets can still include extra fields. For example, a preset can carry the index name for `createIndex()` alongside the analyzer options used by `Index.fts()` and `tokenize()`. ## Tests The Node tests check exact tokens from both the named index and standalone tokenization, whole identifiers, subword matches, shared presets and invalid combinations of known options. With the rebuilt native addon, the full local Jest run passed 2,045 tests across 37 suites, with 27 tests skipped. TypeScript checks, lint and API reference regeneration also passed. --------- Co-authored-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
+104
-2
@@ -36,6 +36,82 @@ impl Index {
|
||||
}
|
||||
}
|
||||
|
||||
/// Code-analyzer options shared by `Index.fts` and `tokenize`.
|
||||
struct CodeAnalyzerOptions {
|
||||
analyzer: Option<String>,
|
||||
split_identifiers: Option<bool>,
|
||||
split_on_numerics: Option<bool>,
|
||||
preserve_original: Option<bool>,
|
||||
index_operators: Option<bool>,
|
||||
}
|
||||
|
||||
impl CodeAnalyzerOptions {
|
||||
/// Check the options against the requested base tokenizer and return the
|
||||
/// base tokenizer to use, if any. `analyzer: "code"` selects the code
|
||||
/// tokenizer when `baseTokenizer` is unset or `"simple"`.
|
||||
fn resolve_base_tokenizer(
|
||||
&self,
|
||||
base_tokenizer: Option<String>,
|
||||
) -> napi::Result<Option<String>> {
|
||||
let uses_code = match self.analyzer.as_deref() {
|
||||
None | Some("text") => base_tokenizer.as_deref() == Some("code"),
|
||||
Some("code") => true,
|
||||
Some(other) => {
|
||||
return Err(napi::Error::from_reason(format!(
|
||||
"analyzer must be 'text' or 'code', got '{other}'"
|
||||
)));
|
||||
}
|
||||
};
|
||||
match (self.analyzer.as_deref(), base_tokenizer.as_deref()) {
|
||||
(Some("text"), Some("code")) => {
|
||||
return Err(napi::Error::from_reason(
|
||||
"baseTokenizer 'code' requires analyzer 'code'",
|
||||
));
|
||||
}
|
||||
(Some("code"), Some(tokenizer)) if tokenizer != "code" && tokenizer != "simple" => {
|
||||
return Err(napi::Error::from_reason(format!(
|
||||
"analyzer 'code' requires baseTokenizer 'code', got '{tokenizer}'"
|
||||
)));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let flags = [
|
||||
self.split_identifiers,
|
||||
self.split_on_numerics,
|
||||
self.preserve_original,
|
||||
self.index_operators,
|
||||
];
|
||||
if !uses_code && flags.into_iter().flatten().any(|flag| flag) {
|
||||
return Err(napi::Error::from_reason(
|
||||
"splitIdentifiers, splitOnNumerics, preserveOriginal and indexOperators \
|
||||
require analyzer 'code'",
|
||||
));
|
||||
}
|
||||
Ok(if uses_code {
|
||||
Some("code".to_string())
|
||||
} else {
|
||||
base_tokenizer
|
||||
})
|
||||
}
|
||||
|
||||
/// Apply the explicit flags on top of the tokenizer defaults.
|
||||
fn apply(&self, mut opts: FtsIndexBuilder) -> FtsIndexBuilder {
|
||||
if let Some(value) = self.split_identifiers {
|
||||
opts = opts.split_identifiers(value);
|
||||
}
|
||||
if let Some(value) = self.split_on_numerics {
|
||||
opts = opts.split_on_numerics(value);
|
||||
}
|
||||
if let Some(value) = self.preserve_original {
|
||||
opts = opts.preserve_original(value);
|
||||
}
|
||||
if let Some(value) = self.index_operators {
|
||||
opts = opts.index_operators(value);
|
||||
}
|
||||
opts
|
||||
}
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
#[allow(dead_code, clippy::too_many_arguments)]
|
||||
pub fn tokenize(
|
||||
@@ -51,9 +127,21 @@ pub fn tokenize(
|
||||
ngram_min_length: Option<u32>,
|
||||
ngram_max_length: Option<u32>,
|
||||
prefix_only: Option<bool>,
|
||||
analyzer: Option<String>,
|
||||
split_identifiers: Option<bool>,
|
||||
split_on_numerics: Option<bool>,
|
||||
preserve_original: Option<bool>,
|
||||
index_operators: Option<bool>,
|
||||
) -> napi::Result<Vec<FtsToken>> {
|
||||
let mut opts = FtsIndexBuilder::default();
|
||||
if let Some(base_tokenizer) = base_tokenizer {
|
||||
let code_options = CodeAnalyzerOptions {
|
||||
analyzer,
|
||||
split_identifiers,
|
||||
split_on_numerics,
|
||||
preserve_original,
|
||||
index_operators,
|
||||
};
|
||||
if let Some(base_tokenizer) = code_options.resolve_base_tokenizer(base_tokenizer)? {
|
||||
opts = opts.base_tokenizer(base_tokenizer);
|
||||
}
|
||||
if let Some(language) = language {
|
||||
@@ -89,6 +177,7 @@ pub fn tokenize(
|
||||
if let Some(prefix_only) = prefix_only {
|
||||
opts = opts.ngram_prefix_only(prefix_only);
|
||||
}
|
||||
opts = code_options.apply(opts);
|
||||
|
||||
Ok(lancedb_tokenize(&query, &opts)
|
||||
.default_error()?
|
||||
@@ -304,14 +393,27 @@ impl Index {
|
||||
ngram_max_length: Option<u32>,
|
||||
prefix_only: Option<bool>,
|
||||
block_size: Option<u32>,
|
||||
analyzer: Option<String>,
|
||||
split_identifiers: Option<bool>,
|
||||
split_on_numerics: Option<bool>,
|
||||
preserve_original: Option<bool>,
|
||||
index_operators: Option<bool>,
|
||||
) -> napi::Result<Self> {
|
||||
let mut opts = FtsIndexBuilder::default();
|
||||
if let Some(with_position) = with_position {
|
||||
opts = opts.with_position(with_position);
|
||||
}
|
||||
if let Some(base_tokenizer) = base_tokenizer {
|
||||
let code_options = CodeAnalyzerOptions {
|
||||
analyzer,
|
||||
split_identifiers,
|
||||
split_on_numerics,
|
||||
preserve_original,
|
||||
index_operators,
|
||||
};
|
||||
if let Some(base_tokenizer) = code_options.resolve_base_tokenizer(base_tokenizer)? {
|
||||
opts = opts.base_tokenizer(base_tokenizer);
|
||||
}
|
||||
opts = code_options.apply(opts);
|
||||
if let Some(language) = language {
|
||||
opts = opts.language(&language).map_err(|_| {
|
||||
napi::Error::from_reason(format!(
|
||||
|
||||
Reference in New Issue
Block a user