FHIRPath: Custom Search On Date Difference

207 views
Skip to first unread message

Ben Li-Sauerwine

unread,
Nov 10, 2021, 1:17:10 AM11/10/21
to HAPI FHIR
I'd like to make a custom SearchParameter that allows me to search Specimens on the Patient's age at the time the Specimen was collected.

I've tried several permutations, but the general idea is something like this:

{
  "resourceType" : "SearchParameter",
   <...snip...>
  "type" : "quantity",
  "expression": "Specimen.iif(collection.collectedDateTime.exists(), collection.collectedDateTime - subject.resolve().birthDate, Specimen.iif(receivedTime.exists(), receivedDateTime - subject.resolve().birthDate, ''))"
}

While HAPI gladly accepts this SearchParameter, it doesn't actually seem to give any results for a search:  Specimen?subjectage=gt20%7C%7Cyears

Is my FHIRPath flawed, is my understanding of how to query a duration in time incorrect, or both?

James Agnew

unread,
Nov 10, 2021, 10:15:49 AM11/10/21
to Ben Li-Sauerwine, HAPI FHIR
Wow, this is a neat idea. A few thoughts:

- SearchParam expressions can not use the resolve() function currently. We did not implement this because it has obvious performance implications. I could certainly see making this a configurable thing since there are use cases where it would be helpful.

- I'm not actually sure what the search indexer would do with the output of this FHIRPath expression - If you have a SP with a type of quantity and you pass it an expression that resolves to a date, I don't actually know what would be indexed. In this case, it's not even a date, but rather a date calculation by the FHIRPath evaluator. 

- It doesn't look like the FHIRPath evaluator currently supports calculations on dates: http://hapi.fhir.org/baseR4/Specimen/248881?_fhirpath=Specimen.processing.time.start-Specimen.collection.collected.start - I don't know if this is supposed to be valid or not, TBH.

Cheers,
James




--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/42cb58f8-fb97-4066-a746-4f4c8c1fe19bn%40googlegroups.com.

Ben Li-Sauerwine

unread,
Nov 10, 2021, 3:34:03 PM11/10/21
to HAPI FHIR
Thank you for that information, James.  Your conclusions roughly align with my research.  I am surprised that this type of question isn't something that was raised to HL7 by now.

One route forward for me would have been to resolve the dates to a UNIX epoch, then do math on the epoch.  Unfortunately, no such conversion is defined in FHIRPath.  Also, as you point out, it appears that date + duration is defined in FHIRPath but not date - date.  I was hoping that this was just an oversight, but it seems clear that to add this functionality is HL7's job, not HAPI's.  

We do know someone on the HL7 FHIR steering committee that I can reach out to, but my guess is that to get the specification updated to include dateDiff or Date.toEpoch() is going to be a long road.  

Reply all
Reply to author
Forward
0 new messages