Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +26,7 @@
Email,
Phone,
Address,
ThingContactAssociation,
Thing,
WellCasingMaterial,
WellPurpose,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down
4 changes: 0 additions & 4 deletions db/thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading