Skip to content

Commit

Permalink
vector search yay!
Browse files Browse the repository at this point in the history
Signed-off-by: Praneeth Bedapudi <praneeth@bpraneeth.com>
  • Loading branch information
bedapudi6788 committed Jan 29, 2024
1 parent 5576691 commit 5e050da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
7 changes: 5 additions & 2 deletions liteindex/defined_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ def search(
sort_by=None,
reversed_sort=False,
n=None,
offset=None,
page_no=None,
select_keys=[],
update=None,
Expand All @@ -423,6 +424,9 @@ def search(
query_vector=None,
meta_query={},
):
if page_no is not None:
offset = (page_no - 1) * n

if not sort_by:
sort_by = "updated_at"

Expand Down Expand Up @@ -458,8 +462,7 @@ def search(
sort_by=sort_by,
reversed_sort=reversed_sort,
n=n,
page=page_no,
page_size=n if page_no else None,
offset=offset,
select_columns=(
("integer_id", "id", "updated_at")
+ (() if query_vector is None else ("score",))
Expand Down
17 changes: 6 additions & 11 deletions liteindex/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,13 @@ def search_query(
sort_by=None,
reversed_sort=False,
n=None,
page=None,
page_size=50,
offset=None,
select_columns=None,
):
# Prepare the query
where_conditions, params = parse_query(query, schema)

selected_columns = ", ".join(select_columns) if select_columns else "*"

# Build the query string
query_str = f"SELECT {selected_columns} FROM {table_name}"
if where_conditions:
query_str += f" WHERE {' AND '.join(where_conditions)}"
Expand All @@ -174,11 +171,7 @@ def search_query(
else:
query_str += f" ORDER BY {sort_by} {'DESC' if reversed_sort else 'ASC'}"

if n is not None:
query_str += f" LIMIT {n}"
elif page is not None:
start = (page - 1) * page_size
query_str += f" LIMIT {start}, {page_size}"
query_str += f" LIMIT {n if n is not None else -1} OFFSET {offset if offset is not None else 0}"

return query_str, params

Expand Down Expand Up @@ -437,9 +430,11 @@ def test_multiple_sorting(self):

def test_pagination(self):
query, params = search_query(
"users", {"age": 25}, self.schema, page=2, page_size=10
"users", {"age": 25}, self.schema, offset=10, n=10
)
self.assertEqual(
query, "SELECT * FROM users WHERE age = ? LIMIT 10 OFFSET 10"
)
self.assertEqual(query, "SELECT * FROM users WHERE age = ? LIMIT 10, 10")
self.assertEqual(params, [25])

def test_select_columns(self):
Expand Down

0 comments on commit 5e050da

Please sign in to comment.