Additions to Patient Search API

Hi Platform team, There are a few additions I want to have in the patient search API (REST).

1. Sending total count of patients I want to have the total count of results to be sent along with the results when performing a patient search. For e.g. there are 200 patients with name “John” and when I search for John, according to the pagination, I will only get the top 50 results. What I want from the API is to send the total count, i.e. 200, along with the 50 results at a time.

2. Add sorting feature in the results According to the needs of the design, we need to sort the patients according to one of the 4 cases below: a. First Name b. Family Name c. Oldest first d. Youngest first

Please do let me know about the same asap. Thanks alot!

CC @Platform_Team

1 Like

The addition of 1st feature is urgent and the second one is not so much. Thanks!

Hi @vasharma05, would the FHIR api work for you? In which case the the "total" property on received bundle would suffice for point 1. For point 2, you’d leverage the _sort parameter in a search, for example something along the following line should be able to yield your desired results;

https://dev3.openmrs.org/openmrs/ws/fhir2/R4/Patient?name=m&_sort=given,family,birthdate 

However the sort feature might not yet be fully implemented for the Patient resource but could be added through a PR. See also the Patient FHIR resource search docs.

cc @ibacher

We tried FHIR… unfortunately, I need to write up a ticket for this. The problem is that the query we use is (roughly) where $q is the string the user typed:

SELECT p
FROM Patient p
WHERE p.name = $q OR p.identifiers.identifier = $q

For some implementations and use-cases, being able to search by identifier is actually more important than name.

1 Like

@vasharma05 Just append &totalCount=true to the end of the query and you’ll get the count.

Sorting is going to be harder to implement. Under the covers, we use Lucene to return the most relevant results according to a scoring algorithm that’s a bit complex. Essentially, though, exact matches come before non-exact matches. I’m not sure that sorting based on name or age is necessarily the right thing to do here unless we’ve received a lot of feedback that OpenMRS’s searching is wrong.