Stream: cql
Topic: FHIR search syntax in CQL?
Paul Lynch (May 05 2021 at 21:57):
Is it possible to use the FHIR search syntax from within CQL? I am thinking of something like:
define LatestObservation:
query('Observation?_sort=-date&subject=' + patientRef)
Corey Sanders (May 06 2021 at 15:44):
Paul Lynch said:
Is it possible to use the FHIR search syntax from within CQL? I am thinking of something like:
define LatestObservation: query('Observation?_sort=-date&subject=' + patientRef)
CQL is data model agnostic. You won't find anything in the language that directly exposes FHIR features. You can, however, construct the same type of query as above using the CQL language.
1) CQL has the notion of a context that scopes all the queries. Most likely, you are going to want to set "context Patient" for your queries. You can read about context in the author's guide here. Once you've set the context, your Observation query will automatically have subject appended to it.
context Patient
2) You can order the query results of your Observation query as described in the sorting section of the CQL author's guide. Note the First and Last operators which are probably going to be useful if you are using sorting.
define DateLimitedObservation:
[Observation] o sort by o.effective
Paul Lynch (May 06 2021 at 17:04):
Thanks, that is pretty much the answer I expected, but gives me more information than I had before.
The link to the author's guide is giving me a 403 error at the moment, though I have seen that work before, so hopefully that is temporary.
If you use CQL syntax to write that query, does it actually query a FHIR server? The JavaScript CQL engine I was looking at (https://github.com/cqframework/cql-execution), says it "does not currently rely on any backend database for storing patient records" and I gather you are supposed to pass in the needed data when you run the CQL. I don't know how that would work for Observations, but maybe that is just a limitation of that particular engine?
Corey Sanders (May 06 2021 at 17:19):
I have a limited understanding of the Javascript engine. I believe it uses FHIR Bundle resources as input and can execute ELM against those bundles as if they were retrieved from a FHIR server. @Chris Moesel would be a good person to check with for more details on that.
The Java engine does actually query a FHIR server. The data providers are a pluggable mechanism, but the provided RestFhirRetrieveProvider will use the FHIR search interface to retrieve data. You might also consider looking at the cqf-ruler project which is Java-based, rides on top of a HAPI FHIR server, and uses the HAPI JPA interface to retrieve data.
JP (May 06 2021 at 17:21):
There's also a Bundle provider for the Java engine that allows the use of data that has been fetched ahead of time, in a similar fashion to the JS engine:
Chris Moesel (May 06 2021 at 17:26):
Just popping in to confirm that the JavaScript engine only works w/ FHIR bundles passed into it -- but as Corey and JP mentioned above, that's a limitation of the JavaScript engine; not a limitation of CQL (although, truly, how data is physically accessed is outside the scope of CQL).
We've used the JavaScript engine in SMART apps where code in the app does the FHIR queries and then assembles them into a bundle to pass on to the JavaScript engine.
I'd like to build querying capability into the cql-exec-fhir
module that the JavaScript engine uses, but have not yet had the time or opportunity.
Last updated: Apr 12 2022 at 19:14 UTC