Elffuss BraidHub

kikocis/lancedb public

fix(python): force deduce vector column name if running explicit hybrid query (#1692)

Right now when passing vector and query explicitly for hybrid search ,
vector_column_name is not deduced.
(https://lancedb.github.io/lancedb/hybrid_search/hybrid_search/#hybrid-search-in-lancedb
). Because vector and query can be both none when initialising the
QueryBuilder in this case. This PR forces deduction of query type if it
is set to "hybrid"
Changesetadee0434ac3dab17d9df5c44aa1dd299a287382429d5a96aec6b82c34eb9b3c1
AuthorAyush Chaurasia <ayush.chaurarsia@gmail.com> · 2024-09-24 19:02 +05:30
CommitterGitHub <noreply@github.com> · 2024-09-24 19:02 +05:30
Parents6c24ed805bf1

Browse the files at this changeset

2 files changed

modified python/python/lancedb/table.py +3 −1
@@ -1630,7 +1630,9 @@
16301630 and also the "_distance" column which is the distance between the query
16311631 vector and the returned vector.
16321632 """
1633- if vector_column_name is None and query is not None and query_type != "fts":
1633+ if (
1634+ vector_column_name is None and query is not None and query_type != "fts"
1635+ ) or (vector_column_name is None and query_type == "hybrid"):
16341636 try:
16351637 vector_column_name = inf_vector_column_query(self.schema)
16361638 except Exception as e:
modified python/python/tests/test_table.py +30 −1
@@ -973,7 +973,36 @@
973973 .where("text='Arrrrggghhhhhhh'")
974974 .to_list()
975975 )
976- len(result) == 1
976+ assert len(result) == 1
977+
978+ # with explicit query type
979+ vector_query = list(range(emb.ndims()))
980+ result = (
981+ table.search(query_type="hybrid")
982+ .vector(vector_query)
983+ .text("Arrrrggghhhhhhh")
984+ .to_arrow()
985+ )
986+ assert len(result) > 0
987+ assert "_relevance_score" in result.column_names
988+
989+ # with vector_column_name
990+ result = (
991+ table.search(query_type="hybrid", vector_column_name="vector")
992+ .vector(vector_query)
993+ .text("Arrrrggghhhhhhh")
994+ .to_arrow()
995+ )
996+ assert len(result) > 0
997+ assert "_relevance_score" in result.column_names
998+
999+ # fail if only text or vector is provided
1000+ with pytest.raises(ValueError):
1001+ table.search(query_type="hybrid").to_list()
1002+ with pytest.raises(ValueError):
1003+ table.search(query_type="hybrid").vector(vector_query).to_list()
1004+ with pytest.raises(ValueError):
1005+ table.search(query_type="hybrid").text("Arrrrggghhhhhhh").to_list()
9771006
9781007
9791008 @pytest.mark.parametrize(