Skip to content

Commit

Permalink
add partial word search
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Dec 27, 2023
1 parent 3b1b496 commit f4159aa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
14 changes: 10 additions & 4 deletions care/facility/api/viewsets/icd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ def serialize_data(self, objects: list[ICD11]):
return [diagnosis.get_representation() for diagnosis in objects]

def list(self, request):
try:
limit = min(int(request.query_params.get("limit")), 20)
except (ValueError, TypeError):
limit = 20

queryset = ICD11
if query := request.query_params.get("query"):
queryset = queryset.find(ICD11.label % query.lower()).page(0, 100)
# todo: add partial word search
queryset = queryset.find(
ICD11.label % f"{'* '.join(query.strip().split())}*"
)
else:
queryset = queryset.find().sort_by("label").page(0, 100)
queryset = queryset.find().sort_by("label")

return Response(self.serialize_data(list(queryset)))
return Response(self.serialize_data(list(queryset.page(0, limit))))
21 changes: 10 additions & 11 deletions care/facility/api/viewsets/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,24 @@ def serialize_data(self, objects: list[MedibaseMedicine]):
return [medicine.get_representation() for medicine in objects]

def list(self, request):
try:
limit = min(int(request.query_params.get("limit")), 30)
except (ValueError, TypeError):
limit = 30

query = None
if type := request.query_params.get("type"):
query = MedibaseMedicine.type == type

if search_query := request.query_params.get("query"):
q = (MedibaseMedicine.name == search_query) | (
MedibaseMedicine.vec % search_query
MedibaseMedicine.vec % f"{'* '.join(search_query.strip().split())}*"
)
# todo: add partial word search
query = query & q if query else q

try:
limit = min(int(request.query_params.get("limit")), 100)
except ValueError:
limit = 30

if not query:
return Response([])
if query:
queryset = MedibaseMedicine.find(query)
else:
queryset = MedibaseMedicine.find(query).page(0, limit)
queryset = MedibaseMedicine.find()

return Response(self.serialize_data(list(queryset)))
return Response(self.serialize_data(list(queryset.page(0, limit))))
9 changes: 5 additions & 4 deletions care/hcx/api/viewsets/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,14 @@ def serialize_data(self, objects: list[PMJYPackage]):
def pmjy_packages(self, request):
try:
limit = min(int(request.query_params.get("limit")), 20)
except ValueError:
except (ValueError, TypeError):
limit = 20

queryset = PMJYPackage
if search_query := request.query_params.get("query"):
queryset = queryset.find(PMJYPackage.vec % search_query.lower())
# todo: add partial word search
if query := request.query_params.get("query"):
queryset = queryset.find(
PMJYPackage.vec % f"{'* '.join(query.strip().split())}*"
)
else:
queryset = queryset.find()

Expand Down

0 comments on commit f4159aa

Please sign in to comment.