1

I am using the latest release of HapiFHIR (both server and client side) which should be HL7 FHIR compliant.

As part of the project I am working on, I need to retrieve the latest observation for each device. Having experience in SQL, I naturally expected to use an operation such as GROUP BY to achieve this. But it seems, from reading the documentation, that there is no such operation in HapiFHIR!

Am I missing anything? And if not, what would be a good workaround in order to retrieve the latest (i.e. sort by date, limit 1) for each device?

Alexandre Cassagne
  • 2,384
  • 23
  • 40

1 Answers1

2

As you are using FHIR, wich has a nice REST search API, there is no need to implement your own query.

{{baseurl}}/Observation?device:Device=123&_sort=-_lastUpdated&_count=1

gets you the latest Observation of the Device with the ID of 123.

FHIR search api

edit: as you are also using the HAPI Client, your query code would look like this:

Bundle results = myFhirCtx.newRestfulGenericClient("yourFHIRbaseURL")
        .search()
        .forResource(Observation.class)
        .where(Observation.DEVICE.hasId("123"))
        .sort().descending(Observation.DATE)
        .count(1)
        .returnBundle(Bundle.class)
        .execute();

The GET from above searches for the latest updated Observation of a device, the JAVA code gets the newest Observation based on its date. I think you meant the second.

obsolete as this is returning the newest n Observation over all devices not N for every Device, for multiple Devices:

If you want to query multiple devices at once you could either aggregate the query by querying Observations with Devices of Type X, or the latest Observation for Devices of Manufacturer or Model, see: search parameters for the Device resource

If you want to query the latest n Observations of a Set of known device IDs you can use the OR operator, which is just a colon.

{{baseurl}}/Observation?device:Device=123,345,678&_sort=-_lastUpdated&_count=n

in hapi client code:

    Bundle results = myFhirCtx.newRestfulGenericClient("ttt")
        .search()
        .forResource(Observation.class)
        .where(Observation.DEVICE.hasAnyOfIds(new ArrayList<String>(Arrays.asList("123", "345", "678" ))))
        .sort().descending(Observation.DATE)
        .count(n)
        .returnBundle(Bundle.class)
        .execute();

Conclusion

If you want to have the n latest Observation of each device of a device subset you can either:

  • use the first query of this example multiple times
  • or if you prefer a single REST call you can batch these GET calls together in batch bundle like in this example: Batch Bundle search. This will return a batch response bundle with entries for each search.
Patrick Werner
  • 1,106
  • 9
  • 24
  • Thank you for your answer. However, if that was not clear from my question, I meant to say that I am trying get the latest observation, for each device (simultaneously). e.g. for 50 different devices in one request. – Alexandre Cassagne Jun 19 '18 at 01:29
  • 1
    Hi elnin, thanks again for your time. Unfortunately the code that you uploaded does not perform as described. It only returns _one single observation_ -- the one that is most recent in the devices supplied. This is not the same as my use case – I would like, in a single request, to get some function that for devices `[a, b, c...]` returns the latest observations of each device [latest observation of `a`, latest observation of `b`, latest observation of `c`...]. – Alexandre Cassagne Jun 19 '18 at 19:14
  • @AlexandreCassagne have you found any workaround on this? I have a similar scenario. I have to "get only the latest issued DiagnosticReport resources of all the Patient resources" – Abubakar Ikram May 30 '20 at 13:26