@@ -650,6 +650,44 @@ def test_vector_search_with_filter(primary, source_table):
650650 row [2 ] == filter_val
651651 ), f"Expected all results to have random_bool == { filter_val } "
652652
653+ @pytest .mark .parametrize ("distance_metric" , ["" , "l2sq" , "cos" ])
654+ def test_weighted_vector_search (primary , distance_metric ):
655+ primary .execute ("testdb" , "CREATE TABLE IF NOT EXISTS small_world (id VARCHAR(3), b BOOLEAN, v VECTOR(3), s SPARSEVEC(3));" )
656+ primary .execute ("testdb" , """
657+ INSERT INTO small_world VALUES
658+ ('000', TRUE, '[0,0,0]', '{}/3'),
659+ ('001', TRUE, '[0,0,1]', '{3:1}/3'),
660+ ('010', FALSE, '[0,1,0]' , '{2:1}/3'),
661+ ('011', TRUE, '[0,1,1]', '{2:1,3:1}/3'),
662+ ('100', FALSE, '[1,0,0]', '{1:1}/3'),
663+ ('101', FALSE, '[1,0,1]', '{1:1,3:1}/3'),
664+ ('110', FALSE, '[1,1,0]', '{1:1,2:1}/3'),
665+ ('111', TRUE, '[1,1,1]', '{1:1,2:1,3:1}/3');
666+ """ )
667+ operator = op = { 'l2sq' : '<->' , 'cos' : '<=>' , 'hamming' : '<+>' }[distance_metric or 'l2sq' ]
668+ query_s = "{1:0.4,2:0.3,3:0.2}/3"
669+ query_v = "[-0.5,-0.1,-0.3]"
670+ function = f'weighted_vector_search_{ distance_metric } ' if distance_metric else 'weighted_vector_search'
671+ query = f"""
672+ SELECT
673+ id,
674+ round(cast(0.9 * (s { operator } :'{ query_s } '::sparsevec) + 0.1 * (v { operator } :'{ query_v } '::vector) as numeric), 2) as dist
675+ FROM lantern.{ function } (CAST(NULL as "small_world"), operator=>'{ operator } ',
676+ w1=> 0.9, col1=>'s'::text, vec1=>:'{ query_s } '::sparsevec,
677+ w2=> 0.1, col2=>'v'::text, vec2=>:'{ query_v } '::vector
678+ );
679+ LIMIT 3;
680+ """
681+ res = primary .execute ("testdb" , query )
682+
683+ expected_results_cos = [('111' , 0.22 ), ('110' , 0.24 ), ('101' , 0.39 )]
684+ expected_results_l2sq = [('000' , 0.54 ), ('100' , 0.78 ), ('010' , 0.87 )]
685+ if distance_metric == 'cos' :
686+ assert res == expected_results_cos
687+ else :
688+ assert res == expected_results_l2sq
689+
690+
653691# fixture to handle external index server setup
654692@pytest .fixture
655693def external_index (request ):
0 commit comments