Search for similar vectors using cosine distance Args: collection: Collection name query_embedding: Query vector k: Number of top results to return Returns: Dictionary with search results in Chroma-compatible format
(
self,
collection: str,
query_embedding: list[float],
k: int = 5,
search_type: str = 'vector',
query_text: str = '',
filter: dict[str, Any] | None = None,
vector_weight: float | None = None,
)
| 185 | raise |
| 186 | |
| 187 | async def search( |
| 188 | self, |
| 189 | collection: str, |
| 190 | query_embedding: list[float], |
| 191 | k: int = 5, |
| 192 | search_type: str = 'vector', |
| 193 | query_text: str = '', |
| 194 | filter: dict[str, Any] | None = None, |
| 195 | vector_weight: float | None = None, |
| 196 | ) -> Dict[str, Any]: |
| 197 | """Search for similar vectors using cosine distance |
| 198 | |
| 199 | Args: |
| 200 | collection: Collection name |
| 201 | query_embedding: Query vector |
| 202 | k: Number of top results to return |
| 203 | |
| 204 | Returns: |
| 205 | Dictionary with search results in Chroma-compatible format |
| 206 | """ |
| 207 | await self.get_or_create_collection(collection) |
| 208 | |
| 209 | async with self.AsyncSessionLocal() as session: |
| 210 | try: |
| 211 | # Use cosine distance for similarity search |
| 212 | from sqlalchemy import select |
| 213 | |
| 214 | # Query for similar vectors |
| 215 | stmt = ( |
| 216 | select( |
| 217 | PgVectorEntry.id, |
| 218 | PgVectorEntry.text, |
| 219 | PgVectorEntry.file_id, |
| 220 | PgVectorEntry.chunk_uuid, |
| 221 | PgVectorEntry.embedding.cosine_distance(query_embedding).label('distance'), |
| 222 | ) |
| 223 | .filter(PgVectorEntry.collection == collection) |
| 224 | .order_by(PgVectorEntry.embedding.cosine_distance(query_embedding)) |
| 225 | .limit(k) |
| 226 | ) |
| 227 | |
| 228 | if filter: |
| 229 | for cond in _build_pg_conditions(filter): |
| 230 | stmt = stmt.filter(cond) |
| 231 | |
| 232 | result = await session.execute(stmt) |
| 233 | rows = result.fetchall() |
| 234 | |
| 235 | # Convert to Chroma-compatible format |
| 236 | ids = [] |
| 237 | distances = [] |
| 238 | metadatas = [] |
| 239 | |
| 240 | for row in rows: |
| 241 | ids.append(row.id) |
| 242 | distances.append(float(row.distance)) |
| 243 | metadatas.append( |
| 244 | {'text': row.text or '', 'file_id': row.file_id or '', 'uuid': row.chunk_uuid or ''} |
nothing calls this directly
no test coverage detected