1616from fastapi import APIRouter , Depends
1717from sqlalchemy import select
1818from sqlalchemy .orm import Session
19+ from api .pagination import CustomPage
20+ from fastapi_pagination import paginate
21+ from fastapi_pagination .utils import disable_installed_extensions_check
1922
23+ from core .dependencies import session_dependency
2024from db import (
2125 Contact ,
2226 Email ,
2731 AssetThingAssociation ,
2832 search ,
2933)
30- from db .engine import get_db_session
3134
35+
36+ disable_installed_extensions_check ()
3237router = APIRouter (prefix = "/search" , tags = ["search" ])
3338
3439
35- def _get_contact_results (session : Session , q : str ) -> list [dict ]:
40+ def _get_contact_results (session : Session , q : str , limit : int ) -> list [dict ]:
3641 vector = (
3742 Contact .search_vector
3843 | Email .search_vector
@@ -41,7 +46,8 @@ def _get_contact_results(session: Session, q: str) -> list[dict]:
4146 )
4247
4348 query = search (
44- select (Contact ).join (Email ).join (Phone ).join (Address ), q , vector = vector
49+ select (Contact ).join (Email ).join (Phone ).join (Address ), q , vector = vector ,
50+ limit = limit
4551 )
4652 contacts = session .scalars (query ).all ()
4753 results = [
@@ -62,13 +68,15 @@ def _get_contact_results(session: Session, q: str) -> list[dict]:
6268 return results
6369
6470
65- def _get_thing_results (session : Session , q : str ) -> list [dict ]:
71+ def _get_thing_results (session : Session , q : str , limit : int ) -> list [dict ]:
6672 vector = Thing .search_vector
6773 water_well_query = search (
68- select (Thing ).where (Thing .thing_type == "water well" ), q , vector = vector
74+ select (Thing ).where (Thing .thing_type == "water well" ), q , vector = vector ,
75+ limit = limit
6976 )
7077 spring_well_query = search (
71- select (Thing ).where (Thing .thing_type == "spring" ), q , vector = vector
78+ select (Thing ).where (Thing .thing_type == "spring" ), q , vector = vector ,
79+ limit = limit
7280 )
7381
7482 wells = session .scalars (water_well_query ).all ()
@@ -117,10 +125,11 @@ def make_spring_response(thing: Thing) -> dict:
117125 ]
118126
119127
120- def _get_asset_results (session : Session , q : str ) -> list [dict ]:
128+ def _get_asset_results (session : Session , q : str , limit : int ) -> list [dict ]:
121129 vector = Asset .search_vector
122130 query = search (
123- select (Asset ).join (AssetThingAssociation ).join (Thing ), q , vector = vector
131+ select (Asset ).join (AssetThingAssociation ).join (Thing ), q , vector = vector ,
132+ limit = limit
124133 )
125134
126135 assets = session .scalars (query ).all ()
@@ -143,16 +152,17 @@ def _get_asset_results(session: Session, q: str) -> list[dict]:
143152
144153
145154@router .get ("" )
146- def search_api (q : str , session : Session = Depends ( get_db_session )) :
155+ def search_api (session : session_dependency , q : str , limit : int = 25 , ) -> CustomPage [ dict ] :
147156 """
148157 Search endpoint for the collaborative network.
149158 """
150159
151- results = _get_contact_results (session , q )
152- results .extend (_get_thing_results (session , q ))
153- results .extend (_get_asset_results (session , q ))
160+ results = _get_contact_results (session , q , limit )
161+ results .extend (_get_thing_results (session , q , limit ))
162+ results .extend (_get_asset_results (session , q , limit ))
154163
155- return {"items" : results , "total" : len (results )}
164+ return paginate (results )
165+ # return {"items": results, "total": len(results)}
156166
157167
158168# ============= EOF =============================================
0 commit comments