Sort or filter by revincluded value

I want to get the 30 patients who most-recently signed a contract. (If there are 1000 patients total, I don’t want the other 970 patients who signed contracts less-recently to be returned.)

Here is a query to get all Patients, with their Contracts and QuestionnaireResponses:

Patient?_revinclude=Contract:signer&_revinclude=QuestionnaireResponse:patient&_revinclude:Task:owner

is it now possible to sort the result by Contract.signer.signature.when?
how about filtering? Is it possible to filter by Contract.signer.signature.when

I can’t just get all results and post-process, because the number of patients is too large to hold in memory.

I’m aware that I could do this:

Contract?_include=Contract:signer

But I’d rather not because this is part of a larger application that needs to sort by various values on either Patient, QuestionnaireResponse or Contract. If possible, I’d prefer to write a single query with varying parameters for each type of sorting, rather than 3 separate queries depending on what is being sorted on. Plus then I’d need references from Contract to QuestionnaireResponse as well.

1 Like

You can do a _has on a custom search parameter on Contract to filter by when, but you’d probably be better to just make your search on Contract and do an _include on Patient.