1
+ import asyncio
1
2
import typing as t
2
3
import redis .asyncio as redis
3
4
4
5
from fastapi import APIRouter
6
+
5
7
from vecsim_app import config
6
8
from vecsim_app import TEXT_MODEL
7
9
from vecsim_app .schema import (
@@ -38,28 +40,23 @@ async def products_from_results(total, results) -> list:
38
40
operation_id = "get_products_samples" )
39
41
async def get_products (limit : int = 20 , skip : int = 0 , gender : str = "" , category : str = "" ):
40
42
products = []
41
- # TODO - if we can generalize this - that would be sick
43
+ expressions = []
42
44
if gender and category :
43
- products = await Product . find (
45
+ expressions . append (
44
46
(Product .product_metadata .gender == gender ) & \
45
47
(Product .product_metadata .master_category == category )
46
- ).copy (offset = skip , limit = limit ).execute ()
47
-
48
+ )
48
49
elif gender and not category :
49
- products = await Product .find (
50
- Product .product_metadata .gender == gender
51
- ).copy (offset = skip , limit = limit ).execute ()
52
-
50
+ expressions .append (Product .product_metadata .gender == gender )
53
51
elif category and not gender :
54
- products = await Product .find (
55
- Product .product_metadata .master_category == category
56
- ).copy (offset = skip , limit = limit ).execute ()
57
-
58
- else :
59
- products = await Product .find ().copy (offset = skip , limit = limit ).execute ()
52
+ expressions .append (Product .product_metadata .master_category == category )
53
+ # Run query
54
+ products = await Product .find (* expressions )\
55
+ .copy (offset = skip , limit = limit )\
56
+ .execute (exhaust_results = False )
60
57
# Get total count
61
58
total = (
62
- await redis_client .ft ().search (
59
+ await redis_client .ft (config . INDEX_NAME ).search (
63
60
count (gender = gender , category = category )
64
61
)
65
62
).total
@@ -104,11 +101,13 @@ async def find_products_by_image(similarity_request: SimilarityRequest) -> t.Dic
104
101
vector = await redis_client .hget (product_vector_key , "img_vector" )
105
102
106
103
# obtain results of the query
107
- total = (await redis_client .ft ().search (count_query )).total
108
- results = await redis_client .ft ().search (query , query_params = {"vec_param" : vector })
104
+ total , results = await asyncio .gather (
105
+ redis_client .ft (config .INDEX_NAME ).search (count_query ),
106
+ redis_client .ft (config .INDEX_NAME ).search (query , query_params = {"vec_param" : vector })
107
+ )
109
108
110
109
# Get Product records of those results
111
- return await products_from_results (total , results )
110
+ return await products_from_results (total . total , results )
112
111
113
112
114
113
@r .post ("/vectorsearch/text" ,
@@ -134,11 +133,13 @@ async def find_products_by_text(similarity_request: SimilarityRequest) -> t.Dict
134
133
vector = await redis_client .hget (product_vector_key , "text_vector" )
135
134
136
135
# obtain results of the query
137
- total = (await redis_client .ft ().search (count_query )).total
138
- results = await redis_client .ft ().search (query , query_params = {"vec_param" : vector })
136
+ total , results = await asyncio .gather (
137
+ redis_client .ft (config .INDEX_NAME ).search (count_query ),
138
+ redis_client .ft (config .INDEX_NAME ).search (query , query_params = {"vec_param" : vector })
139
+ )
139
140
140
141
# Get Product records of those results
141
- return await products_from_results (total , results )
142
+ return await products_from_results (total . total , results )
142
143
143
144
144
145
@r .post ("/vectorsearch/text/user" ,
@@ -163,8 +164,10 @@ async def find_products_by_user_text(similarity_request: UserTextSimilarityReque
163
164
vector = TEXT_MODEL .encode (similarity_request .user_text )
164
165
165
166
# obtain results of the query
166
- total = (await redis_client .ft ().search (count_query )).total
167
- results = await redis_client .ft ().search (query , query_params = {"vec_param" : vector })
167
+ total , results = await asyncio .gather (
168
+ redis_client .ft (config .INDEX_NAME ).search (count_query ),
169
+ redis_client .ft (config .INDEX_NAME ).search (query , query_params = {"vec_param" : vector })
170
+ )
168
171
169
172
# Get Product records of those results
170
- return await products_from_results (total , results )
173
+ return await products_from_results (total . total , results )
0 commit comments