2
2
3
3
import numpy as np
4
4
from fastapi import APIRouter , Depends
5
- from redis .commands .search .document import Document
6
- from redis .commands .search .query import Query
7
5
from redisvl .index import AsyncSearchIndex
8
- from redisvl .query import FilterQuery , VectorQuery
9
- from redisvl .query .filter import FilterExpression , Tag
6
+ from redisvl .query import CountQuery , FilterQuery , VectorQuery
7
+ from redisvl .query .filter import Tag
10
8
11
9
from productsearch import config
12
10
from productsearch .api .schema .product import (
13
11
ProductSearchResponse ,
14
12
ProductVectorSearchResponse ,
15
13
SimilarityRequest ,
16
14
)
17
- from productsearch .db import redis_helpers
18
-
15
+ from productsearch .db import utils
19
16
20
17
router = APIRouter ()
21
18
22
19
23
- def create_count_query (filter_expression : FilterExpression ) -> Query :
24
- """
25
- Create a "count" query where simply want to know how many records
26
- match a particular filter expression
27
-
28
- Args:
29
- filter_expression (FilterExpression): The filter expression for the query.
30
-
31
- Returns:
32
- Query: The Redis query object.
33
- """
34
- return Query (str (filter_expression )).no_content ().dialect (2 )
35
-
36
-
37
20
@router .get (
38
21
"/" ,
39
22
response_model = ProductSearchResponse ,
@@ -45,7 +28,7 @@ async def get_products(
45
28
skip : int = 0 ,
46
29
gender : str = "" ,
47
30
category : str = "" ,
48
- index : AsyncSearchIndex = Depends (redis_helpers .get_async_index ),
31
+ index : AsyncSearchIndex = Depends (utils .get_async_index ),
49
32
) -> ProductSearchResponse :
50
33
"""Fetch and return products based on gender and category fields
51
34
@@ -76,7 +59,7 @@ async def get_products(
76
59
)
77
60
async def find_products_by_image (
78
61
similarity_request : SimilarityRequest ,
79
- index : AsyncSearchIndex = Depends (redis_helpers .get_async_index ),
62
+ index : AsyncSearchIndex = Depends (utils .get_async_index ),
80
63
) -> ProductVectorSearchResponse :
81
64
"""Fetch and return products based on image similarity
82
65
@@ -116,14 +99,14 @@ async def find_products_by_image(
116
99
return_fields = config .RETURN_FIELDS ,
117
100
filter_expression = filter_expression ,
118
101
)
119
- count_query = create_count_query (filter_expression )
102
+ count_query = CountQuery (filter_expression )
120
103
121
104
# Execute search
122
105
count , result_papers = await asyncio .gather (
123
- index .search (count_query ), index .query (paper_similarity_query )
106
+ index .query (count_query ), index .query (paper_similarity_query )
124
107
)
125
108
# Get Paper records of those results
126
- return ProductVectorSearchResponse (total = count . total , products = result_papers )
109
+ return ProductVectorSearchResponse (total = count , products = result_papers )
127
110
128
111
129
112
@router .post (
@@ -134,7 +117,7 @@ async def find_products_by_image(
134
117
)
135
118
async def find_products_by_text (
136
119
similarity_request : SimilarityRequest ,
137
- index : AsyncSearchIndex = Depends (redis_helpers .get_async_index ),
120
+ index : AsyncSearchIndex = Depends (utils .get_async_index ),
138
121
) -> ProductVectorSearchResponse :
139
122
"""Fetch and return products based on image similarity
140
123
@@ -174,11 +157,11 @@ async def find_products_by_text(
174
157
return_fields = config .RETURN_FIELDS ,
175
158
filter_expression = filter_expression ,
176
159
)
177
- count_query = create_count_query (filter_expression )
160
+ count_query = CountQuery (filter_expression )
178
161
179
162
# Execute search
180
163
count , result_papers = await asyncio .gather (
181
- index .search (count_query ), index .query (paper_similarity_query )
164
+ index .query (count_query ), index .query (paper_similarity_query )
182
165
)
183
166
# Get Paper records of those results
184
- return ProductVectorSearchResponse (total = count . total , products = result_papers )
167
+ return ProductVectorSearchResponse (total = count , products = result_papers )
0 commit comments