Webservice search results order (how are they sorted?)

Hello, when I search for patients:

/patient?q=John&v=default&limit=20
/patient?q=John&v=default&limit=20&startIndex21

How are the results ordered by? I can’t find any clue on the docs. Only the List Concept Proposals has a sortOn and sortOrder parameters.

Can I suppose that as the underlying language is Java, pagination index is 1-based?

Thank you in adnvance.

J.

We do not provide sorting for the patient resource.

Thank you for your answer @dkayiwa.

To better understand the way OpenMRS works, if no order is provided, the search results are non deterministic across pagination queries, possibly having duplicates or missing values, as SQL does not guarantee any order. Is this correct?

Thank you again.

J.

Do you mind explaining a bit more, what you mean by this?

Sure, let’s assume there is no creation of a patient between queries:

translating the REST API call to SQL the system makes a query limiting the number of results, something like: SELECT ... LIMIT 20 OFFSET 20. If there is no ORDER BY clause the results will not have a specific order. This is how I understand SQL and relational databases work. So you can’t make any assumption on how the results will appear. If you issue two exact queries (without ORDER BY) you can have two different result sets.

If there is an ORDER BY clause, something like SELECT .... LIMIT 20 OFFSET 20 ORDER BY display_name, birth_date, uuid, then, thanks to the ORDER BY clause, the same query will always have the same results because they are ordered.

What I like to know is if there is an ORDER BY or not, and if there is an ORDER BY, on which fields. This will have an effect on how the application I’m developing will use and present searches.

Of course, if there is a concurrent INSERT or DELETE the results will be always affected.

So using an ORDER BY or not in the queries is just a system design decision I would like to understand. Although I’ve begun to look at the source code I’m too new to OpenMRS to understand it.

Thank you for your patience.

For patient searches, we do not order them at all. And FWIW, from OpenMRS platform version 2.1.0 and above, we use Apache Lucene for patient searches.

I have once been there too. So do not worry, we shall move along together, as you ask questions for clarifications here and there. And just in case you find this useful: [TRUNK-425] - OpenMRS Issues

Thanks @dkayiwa. Hi @jaume.figueras, alternatively you could make us of the FHIR api. For this you’ll have to load the fhir2 module on the OpenMRS instance you are running. You can refer to the FHIR docs search section. Not all of the options are supported as of fhir2 1.9.0-SNAPSHOT but most of the useful search operations are. You can do some on-the-fly testing with the dev server @ https://dev3.openmrs.org

For example;

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

would yield a bundle of Patients with the ‘M’ in one of the names in descending order of patient names. Sorting is also possible on a few other fields like birthDate

2 Likes