Currently, I am fetching the list of Immunization resources for a Patient using the below code. Is this efficient method of fetching the data from FHIR Server? How to use the FHIRPATH in this case?
// Create a client
IGenericClient client = ctx.newRestfulGenericClient("https://hapi.fhir.org/baseR4");
// Read a patient with the given ID
Patient patient = client.read().resource(Patient.class).withId(patientId).execute();
String id = patient.getId();
List<IBaseResource> immunizations = new ArrayList<IBaseResource>();
Bundle bundle = client.search().forResource(Immunization.class).where(Immunization.PATIENT.hasId(id))
.returnBundle(Bundle.class).execute();
immunizations.addAll(BundleUtil.toListOfResources(ctx, bundle));
// Load the subsequent pages
while (bundle.getLink(IBaseBundle.LINK_NEXT) != null) {
bundle = client.loadPage().next(bundle).execute();
immunizations.addAll(BundleUtil.toListOfResources(ctx, bundle));
}
String fhirResource = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient);
This code looks efficient to me, yes.
Since you are offloading the entire set of Immunizations for the patient, I would pay attention to the page size. Since, by default, a FHIR RESTful Server will not page (see HAPI FHIR Official documentation on Paging search), you would be better off explicitly fine tuning the count size according to your dataset, to avoid unnecessary round trips to the server as much as possible and, on the other hand, avoid timeouts due to an enormous response Bundle.
You can specify the count size like this:
Bundle bundle = client.search()
.forResource(Immunization.class)
.where(Immunization.PATIENT.hasId(id))
.returnBundle(Bundle.class)
.count(200)
.execute();
Please, notice that the FHIR Server may not support paging, so you should also take that into account.
Now, regarding FHIRPath: I am not sure how exactly you imagined using it for this case, but in any imaginable way (for instance, obtaining the whole set of resources for this patient, regardless of the resource type, and performing a FHIRPath operation on this Bundle?), it would be far less performant than with the code you presented. If you want to know how that would be written, an example of a FHIRPath expression that would look into a Bundle of varied resources and return the Immunization resources would look like this:
entry.resource.where(resourceType='Immunization')
As a side note, if you are looking for an extra push for performance, there is always the option to develop a custom query (see HAPI FHIR Official documentation on Named queries), but that would require developments on the server.