Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lancedb_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def _sql_escape(value: str) -> str:
"""Escape single quotes for safe use in SQL WHERE clauses."""
return value.replace("'", "''")

@staticmethod
def _escape_fts_query(query: str) -> str:
"""Escape natural-language tokens Tantivy misreads as field syntax."""
return re.sub(r"(?<!\\):", r"\\:", query)

@staticmethod
def _validate_identifier(key: str) -> None:
"""Raise ValueError if *key* is not a safe SQL identifier."""
Expand Down Expand Up @@ -637,7 +642,7 @@ def keyword_search(
table = self._vs.table
except TableNotFoundError:
return [] # Table not created yet — legitimately empty
q = table.search(query, query_type="fts")
q = table.search(self._escape_fts_query(query), query_type="fts")
if where:
q = q.where(where, prefilter=True)
rows = q.limit(top_k).to_list()
Expand Down
25 changes: 25 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,31 @@ def test_prefilter_combined_source_and_folder():
assert result[0].doc_id == "b.pdf"


def test_keyword_search_escapes_time_like_tokens():
"""FTS keyword search should handle time-like natural-language tokens."""
with tempfile.TemporaryDirectory() as tmpdir:
vec = [1.0] + [0.0] * 767
nodes = [
_make_node(
"appt.md",
"c:0",
"Appointment at 10:00 for the unit showing",
vec,
source_name="comm_messages",
status="active",
),
]
store = _build_store_with_fts(tmpdir, nodes)

hits = store.keyword_search(
"10:00 showing",
top_k=10,
where="lower(metadata.source_name) = 'comm_messages'",
)

assert [hit.doc_id for hit in hits] == ["appt.md"]


def test_prefilter_complex_filter_ast():
"""Complex filter AST should restrict results before search scoring."""
with tempfile.TemporaryDirectory() as tmpdir:
Expand Down