diff --git a/api/search.py b/api/search.py index 7f8c3786b..ab4c28c0f 100644 --- a/api/search.py +++ b/api/search.py @@ -17,7 +17,7 @@ from fastapi_pagination import paginate from fastapi_pagination.utils import disable_installed_extensions_check from sqlalchemy import select, func, text -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, selectinload from api.pagination import CustomPage from core.dependencies import session_dependency, viewer_dependency @@ -26,6 +26,7 @@ Email, Phone, Address, + ThingContactAssociation, Thing, WellCasingMaterial, WellPurpose, @@ -47,7 +48,18 @@ def _get_contact_results(session: Session, q: str, limit: int) -> list[dict]: ) query = search( - select(Contact).outerjoin(Email).outerjoin(Phone).outerjoin(Address), + select(Contact) + .outerjoin(Email) + .outerjoin(Phone) + .outerjoin(Address) + .options( + selectinload(Contact.emails), + selectinload(Contact.phones), + selectinload(Contact.addresses), + selectinload(Contact.thing_associations).selectinload( + ThingContactAssociation.thing + ), + ), q, vector=vector, limit=limit, @@ -61,8 +73,11 @@ def _get_contact_results(session: Session, q: str, limit: int) -> list[dict]: "email": [e.email for e in c.emails], "phone": [p.phone_number for p in c.phones], "address": [a.address_line_1 for a in c.addresses], - # 'address': c.address, - # 'location_id': c.location_id + "things": [ + {"label": t.name, "id": t.id, "thing_type": t.thing_type} + for t in c.things + ], + "id": c.id, }, } for c in contacts @@ -81,7 +96,11 @@ def _get_thing_results(session: Session, q: str, limit: int) -> list[dict]: select(Thing) .outerjoin(WellCasingMaterial) .outerjoin(WellPurpose) - .where(Thing.thing_type == "water well"), + .where(Thing.thing_type == "water well") + .options( + selectinload(Thing.well_casing_materials), + selectinload(Thing.well_purposes), + ), q, vector=well_vector, limit=limit, @@ -117,7 +136,7 @@ def make_well_response(thing: Thing) -> dict: "Wells", thing, { - "well_purpose": thing.well_purpose, + "well_purposes": [wp.purpose for wp in thing.well_purposes], "well_depth": thing.well_depth, "hole_depth": thing.hole_depth, }, diff --git a/db/thing.py b/db/thing.py index 286372242..0015cfa2b 100644 --- a/db/thing.py +++ b/db/thing.py @@ -100,10 +100,6 @@ class Thing( info={"unit": "feet below ground surface"}, comment="Depth of the drilled hole, from ground surface to the bottom of the borehole (in feet).", ) - well_purpose: Mapped[str] = lexicon_term( - nullable=True, - comment="A controlled vocabulary field defining the primary function of the well (e.g., 'Monitoring', 'Irrigation', 'Domestic', 'Livestock', 'Remediation').", - ) well_casing_diameter: Mapped[float] = mapped_column( Float, nullable=True, diff --git a/tests/test_search.py b/tests/test_search.py index acf45c29e..137f82263 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -48,6 +48,20 @@ def test_search_api(water_well_thing, spring_thing, contact): assert isinstance(items, list) assert len(items) == 3 + # Check the contacts returned + contact_items = [item for item in items if item["group"] == "Contacts"] + assert len(contact_items) == 1 + contact_item = contact_items[0] + assert contact_item["label"] == contact.name + assert contact_item["properties"]["id"] == contact.id + assert contact_item["properties"]["things"] == [ + { + "label": water_well_thing.name, + "id": water_well_thing.id, + "thing_type": water_well_thing.thing_type, + }, + ] + @pytest.mark.skip(reason="This test is not working .") def test_search_api2():