fix: .phrase_query() not working (#2781)

The `self._query` value was not set when wrapping its copy `query` with
quotation marks.

The test for phrase queries has been updated to test the
`.phrase_query()` method as well, which will catch this bug.

---------

Co-authored-by: Will Jones <willjones127@gmail.com>
This commit is contained in:
Jackson Hew
2025-11-21 05:32:37 +11:00
committed by GitHub
parent 0084eb238b
commit bb6b0bea0c
3 changed files with 10 additions and 3 deletions

View File

@@ -1495,7 +1495,7 @@ class LanceFtsQueryBuilder(LanceQueryBuilder):
if self._phrase_query: if self._phrase_query:
if isinstance(query, str): if isinstance(query, str):
if not query.startswith('"') or not query.endswith('"'): if not query.startswith('"') or not query.endswith('"'):
query = f'"{query}"' self._query = f'"{query}"'
elif isinstance(query, FullTextQuery) and not isinstance( elif isinstance(query, FullTextQuery) and not isinstance(
query, PhraseQuery query, PhraseQuery
): ):

View File

@@ -325,11 +325,18 @@ def test_search_fts_phrase_query(table):
pass pass
table.create_fts_index("text", use_tantivy=False, with_position=True, replace=True) table.create_fts_index("text", use_tantivy=False, with_position=True, replace=True)
results = table.search("puppy").limit(100).to_list() results = table.search("puppy").limit(100).to_list()
# Test with quotation marks
phrase_results = table.search('"puppy runs"').limit(100).to_list() phrase_results = table.search('"puppy runs"').limit(100).to_list()
assert len(results) > len(phrase_results) assert len(results) > len(phrase_results)
assert len(phrase_results) > 0 assert len(phrase_results) > 0
# Test with a query # Test with .phrase_query()
phrase_results = table.search("puppy runs").phrase_query().limit(100).to_list()
assert len(results) > len(phrase_results)
assert len(phrase_results) > 0
# Test with PhraseQuery()
phrase_results = ( phrase_results = (
table.search(PhraseQuery("puppy runs", "text")).limit(100).to_list() table.search(PhraseQuery("puppy runs", "text")).limit(100).to_list()
) )

View File

@@ -1487,7 +1487,7 @@ def setup_hybrid_search_table(db: DBConnection, embedding_func):
table.add([{"text": p} for p in phrases]) table.add([{"text": p} for p in phrases])
# Create a fts index # Create a fts index
table.create_fts_index("text") table.create_fts_index("text", with_position=True)
return table, MyTable, emb return table, MyTable, emb