How to Retrieve Multiple Resources in a FHIR Server Query

Hi everyone,

I’m trying to query a FHIR server and have multiple “levels” of contained resources returned in the response.

For example:
{
      “resourceType”: “Bundle”,
            “id”: “bundle-request”,
             “type”: “batch”,
             “entry”: [{
             “request”: {
                   “method”: “GET”,
                   “url”: /ServiceRequest?_include=ServiceRequest:requester”
            }
       }]
}

This request will return a ServiceRequest Resource with the resource used in ServiceRequest.requester. In this scenario, it will be a PractitionerRole resource.
This PractitionerRole resource will have a location reference (from PractitonerRple.location) that I also want to be included in the query response. The desired structure of the response would look something like this:

{
      “resourceType”: “Bundle”,
       “id”: “response-id”,
       “type”: “searchset”,
       “entry”: [
             {
             “resource”: {
                   “resourceType”: “ServiceRequest”,
                   “requester”: {
                         “reference”: “#1
                   },
                   “contained”: [
                   {
                         “resourceType”: “PractitionerRole”,
                          “id”: “1”,
                          “location”: [
                           {
                               “reference”: “#2
                           }
                           ]
                   },
                   {
                          “resourceType”: “Location”,
                          “id”: “2”
                   }
                   ]
            }
           }
       ]
}

Is there a way to request all of this from a FHIR server? We can use multiple queries, but the goal is for everything to be done in one searchset bundle (a single transaction). The example request above will return only the ServiceRequest and PractitionerRole, but not the referenced Location resource (from PractitionerRole.location)
I’ve looked into using _contained but I’m not sure if this will solve my problem. Any guidance is appreciated!

First, ‘contained’ isn’t a transport mechanism. It’s a means for conveying data that cannot be represented as a stand-alone resource. If you send something as a contained resource, you should expect the receiver to store it as a contained resource - not create it as a separate instance or resolve it against existing data.

The mechanism for transmitting multiple resources at once is to use Bundle - transaction, batch, query responses, etc. If you’re trying to query data, you should look at the _include and _revinclude search parameters.

If you do actually need to use contained, there’s no hierarchy. The root resource is the ‘owning’ resource. It’s the one that has independent identity that can be created, updated, etc. Everything that’s not stand-alone that is tied to that resource is contained within that one resource. Everything else simply points by reference. Contained resources can point to other contained resources using #[id].

Hi Lloyd, thanks for the response.
I think I did a poor job phrasing my question originally. You can see in my example request that I’m making a query on the ServiceRequest resource and using _include to get the ServiceRequest.requester reference in the response. If this were the query by itself, I would get back something like this:

In this scenario I would receive the PractitionerRole resource in the response. However, I want to include the location resource that is referenced here: PractitionerRole.location. I’m trying to do this in 1 or more queries that are all in the same bundle searchset.

Hopefully that better explains what I am trying to do. Thanks for the help!

_include does not produce contained resources. All “included” resources (from _include or _revinclude) are sent as sibling entries in the Bundle. (They are differentiated by having entry.search.mode set to ‘include’ instead of ‘match’). For your use-case, simply search
ServiceRequest?_include=ServiceRequest:requester,PractitionerRole:location

Ah I see. I did not know _include would not return contained resources. This query will work, thank you!