Skip to content

Commit

Permalink
Add support for filtered search in pgvectorscale
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheharyar570 committed Aug 28, 2024
1 parent bea6875 commit 8a354e1
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions vectordb_bench/backend/clients/pgvectorscale/pgvectorscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class PgVectorScale(VectorDB):
conn: psycopg.Connection[Any] | None = None
coursor: psycopg.Cursor[Any] | None = None

_unfiltered_search: sql.Composed
_filtered_search: sql.Composed

def __init__(
self,
dim: int,
Expand Down Expand Up @@ -99,6 +102,16 @@ def init(self) -> Generator[None, None, None]:
self.cursor.execute(command)
self.conn.commit()

self._filtered_search = sql.Composed(
[
sql.SQL("SELECT id FROM public.{} WHERE id >= %s ORDER BY embedding ").format(
sql.Identifier(self.table_name),
),
sql.SQL(self.case_config.search_param()["metric_fun_op"]),
sql.SQL(" %s::vector LIMIT %s::int")
]
)

self._unfiltered_search = sql.Composed(
[
sql.SQL("SELECT id FROM public.{} ORDER BY embedding ").format(
Expand Down Expand Up @@ -264,9 +277,14 @@ def search_embedding(
assert self.cursor is not None, "Cursor is not initialized"

q = np.asarray(query)
# TODO add filters support
result = self.cursor.execute(
self._unfiltered_search, (q, k), prepare=True, binary=True
)
if filters:
gt = filters.get("id")
self.cursor.execute(
self._filtered_search, (gt, q, k), prepare=True, binary=True
)
else:
result = self.cursor.execute(
self._unfiltered_search, (q, k), prepare=True, binary=True
)

return [int(i[0]) for i in result.fetchall()]

0 comments on commit 8a354e1

Please sign in to comment.