mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-28 05:24:00 +00:00
feat(python): expose code analyzer options on FTS and tokenize
Part of #4364 (Python side; the TypeScript `FtsOptions` change will follow in a separate PR). Lance already supports the code analyzer, but `FTS` and `lancedb.tokenize()` had no way to set it, so `FTS({'analyzer': 'code'})` and `FTS(split_identifiers=True)` raised `TypeError`. ## Changes - `FTS` and `lancedb.tokenize()` gain `analyzer` (`"text"` or `"code"`), `split_identifiers`, `split_on_numerics`, `preserve_original` and `index_operators`. They default to `None`, which keeps the tokenizer's own defaults, so existing indexes and calls are unchanged. - Validation follows pylance: `analyzer="code"` with a `base_tokenizer` other than `"code"` or the default `"simple"` is rejected, `analyzer="text"` with `base_tokenizer="code"` is rejected, and turning on a code flag without the code analyzer is rejected. Passing a flag as `False` is always allowed. - `analyzer="code"` with the default `base_tokenizer="simple"` selects the code tokenizer. `FTS` can't tell an explicit `"simple"` from the default, so that case is accepted too. - The deprecated `create_fts_index` is left alone; it points users at `create_index(config=FTS(...))`. - Rust `FtsIndexBuilder` is Lance's `InvertedIndexParams`, so the Rust SDK already had these options. The remote client serializes the same struct, so the options reach LanceDB Distributed in the `create_index` request body. A new case in the Rust `test_create_index` checks that body. ## Tests - `python/tests/test_fts.py`: tokenizer output for each flag, validation errors for `tokenize` and `FTS`, `False` flags on a text index, and local sync and async indexes that match `http`, `request` and `parseHTTPRequest` with splitting on and not match subwords with it off. - `cargo test --features remote remote::table::tests::test_create_index`
This commit is contained in:
@@ -1832,3 +1832,120 @@ async def test_fts_fast_search_async(async_table):
|
||||
.to_list()
|
||||
)
|
||||
assert len(results) > 0
|
||||
|
||||
|
||||
CODE_TEXT = "parseHTTPRequest user_id utf8"
|
||||
|
||||
|
||||
def _token_texts(tokens):
|
||||
return [token.text for token in tokens]
|
||||
|
||||
|
||||
def test_tokenize_code_analyzer_flags():
|
||||
default = _token_texts(ldb.tokenize(CODE_TEXT, analyzer="code"))
|
||||
assert default == _token_texts(ldb.tokenize(CODE_TEXT, base_tokenizer="code"))
|
||||
|
||||
no_split = _token_texts(
|
||||
ldb.tokenize(CODE_TEXT, analyzer="code", split_identifiers=False)
|
||||
)
|
||||
split = _token_texts(
|
||||
ldb.tokenize(CODE_TEXT, analyzer="code", split_identifiers=True)
|
||||
)
|
||||
assert "parsehttprequest" in no_split
|
||||
assert "parse" not in no_split
|
||||
assert {"parse", "http", "request", "user", "id"} <= set(split)
|
||||
|
||||
numerics = _token_texts(
|
||||
ldb.tokenize(
|
||||
CODE_TEXT,
|
||||
analyzer="code",
|
||||
split_identifiers=True,
|
||||
split_on_numerics=True,
|
||||
)
|
||||
)
|
||||
no_numerics = _token_texts(
|
||||
ldb.tokenize(
|
||||
CODE_TEXT,
|
||||
analyzer="code",
|
||||
split_identifiers=True,
|
||||
split_on_numerics=False,
|
||||
)
|
||||
)
|
||||
assert "utf" in numerics and "8" in numerics
|
||||
assert "utf8" in no_numerics
|
||||
|
||||
operators = _token_texts(
|
||||
ldb.tokenize("a::b -> c != d", analyzer="code", index_operators=True)
|
||||
)
|
||||
plain = _token_texts(
|
||||
ldb.tokenize("a::b -> c != d", analyzer="code", index_operators=False)
|
||||
)
|
||||
assert {"::", "->", "!="} <= set(operators)
|
||||
assert not {"::", "->", "!="} & set(plain)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("kwargs", "message"),
|
||||
[
|
||||
({"analyzer": "code", "base_tokenizer": "ngram"}, "requires base_tokenizer"),
|
||||
({"analyzer": "text", "base_tokenizer": "code"}, "requires analyzer='code'"),
|
||||
({"analyzer": "java"}, "analyzer must be"),
|
||||
({"split_identifiers": True}, "require analyzer='code'"),
|
||||
({"base_tokenizer": "raw", "index_operators": True}, "require analyzer='code'"),
|
||||
],
|
||||
)
|
||||
def test_code_analyzer_options_are_validated(mem_db, kwargs, message):
|
||||
with pytest.raises(ValueError, match=message):
|
||||
ldb.tokenize(CODE_TEXT, **kwargs)
|
||||
|
||||
table = mem_db.create_table("t", data=[{"text": CODE_TEXT}])
|
||||
with pytest.raises(ValueError, match=message):
|
||||
table.create_index("text", config=FTS(**kwargs))
|
||||
|
||||
|
||||
def test_code_analyzer_flags_false_are_allowed_for_text(mem_db):
|
||||
tokens = ldb.tokenize("Running in cafés", split_identifiers=False)
|
||||
assert _token_texts(tokens) == ["run", "cafe"]
|
||||
|
||||
|
||||
def test_fts_code_analyzer_index_matches_subwords(mem_db):
|
||||
table = mem_db.create_table(
|
||||
"code",
|
||||
data=[{"text": "def parseHTTPRequest(user_id): pass"}, {"text": "unrelated"}],
|
||||
)
|
||||
table.create_index(
|
||||
"text",
|
||||
config=FTS(analyzer="code", split_identifiers=True, preserve_original=True),
|
||||
)
|
||||
|
||||
details = table.list_indices()[0].index_details
|
||||
assert details["base_tokenizer"] == "code"
|
||||
assert details["split_identifiers"] is True
|
||||
assert details["preserve_original"] is True
|
||||
|
||||
for query in ["http", "request", "parseHTTPRequest"]:
|
||||
rows = table.search(query, query_type="fts").to_list()
|
||||
assert [row["text"] for row in rows] == ["def parseHTTPRequest(user_id): pass"]
|
||||
|
||||
|
||||
def test_fts_code_analyzer_without_splitting_does_not_match_subwords(mem_db):
|
||||
table = mem_db.create_table(
|
||||
"code", data=[{"text": "def parseHTTPRequest(user_id): pass"}]
|
||||
)
|
||||
table.create_index("text", config=FTS(analyzer="code", split_identifiers=False))
|
||||
|
||||
assert table.search("request", query_type="fts").to_list() == []
|
||||
assert len(table.search("parseHTTPRequest", query_type="fts").to_list()) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_fts_code_analyzer(mem_db_async):
|
||||
table = await mem_db_async.create_table(
|
||||
"code", data=[{"text": "def parseHTTPRequest(user_id): pass"}]
|
||||
)
|
||||
await table.create_index(
|
||||
"text", config=FTS(analyzer="code", split_identifiers=True)
|
||||
)
|
||||
|
||||
rows = await table.query().nearest_to_text("http").to_list()
|
||||
assert len(rows) == 1
|
||||
|
||||
Reference in New Issue
Block a user