FHIR Chat · Update multiple resources with one query · implementers

Stream: implementers

Topic: Update multiple resources with one query


view this post on Zulip Jeffrey Taylor (Oct 12 2018 at 01:43):

Hello, I'd like to update resources matching a query with one PUT (or PATCH?) command. I'm playing with a HAPI FHIR server and executing something that looks like

PUT base/Encounter?_lastUpdated=gt2018-10-11T20:29:26.166-04:00 with a body of
{ "resourceType": "Encounter", "status": "finished" }

This returns

{ "resourceType": "OperationOutcome", "text": { "status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">ERROR</td><td>[]</td><td><pre>Failed to UPDATE resource with match URL \"Encounter?_lastUpdated=gt2018-10-11T20:29:26.166-04:00\" because this search matched 5 resources</pre></td>\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t</tr>\n\t\t</table>\n\t</div>" }, "issue": [ { "severity": "error", "code": "processing", "diagnostics": "Failed to UPDATE resource with match URL \"Encounter?_lastUpdated=gt2018-10-11T20:29:26.166-04:00\" because this search matched 5 resources" } ] }

Is there a way to update all (5 resources in the example above) resources that match my query in a single go?

I see the docs (http://build.fhir.org/http.html#patch) say that 412 is the right response here, as my query isn't selective enough, but I can't find an example of a query/HTTP Verb that allows for this kind of behavior

view this post on Zulip Andrew Tropin (Oct 12 2018 at 10:25):

The simplest solution here is to retrieve all resources with search query (you will get a bundle) update the bundle and do a transaction with PUT requests inside. https://www.hl7.org/fhir/http.html#transaction

view this post on Zulip Jeffrey Taylor (Oct 12 2018 at 12:27):

I thought a simple transaction like this (see below) may work, but no luck. I guess I'll have to include each and every resource separately as entries in this transaction? Imagine there are many many resources here - is there an easier way to do this 'bulk' update?

{
    "resourceType": "Bundle",
    "id": "bundle-transaction",
    "type": "transaction",
    "entry": [
        {
            "resource": {
                "resourceType": "Encounter",
                "status": "finished"
            },
            "request": {
                "method": "PUT",
                "url": "Encounter?_lastUpdated=gt2018-10-11T20:39:22.977-04:00"
            }
        }
    ]
}

view this post on Zulip Jeffrey Taylor (Oct 12 2018 at 12:32):

Meaning - I can 'bulk delete' a bunch of records like so

{
    "resourceType": "Bundle",
    "id": "bundle-transaction",
    "type": "transaction",
    "entry": [
        {
            "request": {
                "method": "DELETE",
                "url": "Encounter?_lastUpdated=gt2018-10-11T20:39:22.977-04:00"
            }
        }
    ]
}

How would I select the same encounters here and just update the status?

view this post on Zulip Andrew Tropin (Oct 12 2018 at 14:08):

Actually operation in your example called Conditional update and delete respectively. The behavior of such request described here:
- https://www.hl7.org/fhir/http.html#cond-update
- https://www.hl7.org/fhir/http.html#2.21.0.13.1

For each conditional operation you can find few different cases: no match, one match, multiple match.
Lets take a closer look:
- for conditional update in case of multiple match server SHOULD respond with 412.
- for conditional delete in case of multiple match server may respond with 412 or delete all matched resource (depends on opinion of developers of your fhir server).

That mean that some fhir servers can do bulk delete using conditional delete like in your second example, but according to fhir spec it's not possible to do bulk update with conditional update like in your first example.

https://www.hl7.org/fhir/http.html#transaction
In more details batch is just a list of requests to fhir server, but sent together. Transaction like a batch, but with guarantee that all operation will succeed or all changes will be rolled back.

Therefore there is no difference if you send one PUT separately or inside transaction.

The only way I can see right now is to make a GET /Encounter?_lastUpdated=gt2018-10-11T20:39:22.977-04:00 query, obtain bundle, update entities inside that bundle with right values and add

"request": {
  "method": "PUT",
  "url": "Encounter"
 }

to each entity and POST a transaction bundle to your fhir server. At least now I don't see simpler way.

Here can arise another question about atomicity of such update, but a solution for this question also exist.

If you want we can make a call and try to write a simple program to solve this problem or even make a small online seminar about transaction and bulk updates if few more people are interested.

view this post on Zulip Bob Milius (Sep 17 2021 at 18:25):

I'm wondering if there is any new information on this front? I created and posted a transaction bundle with an example patient and about 200 observations (purely for testing purposes). I discovered I miss-typed a code system and would like to patch these observations to correct the typo. From what I gather, the only way is to write another transaction/batch bundle and explicitly include the id of each resource. I can write a script to search for these observations, record the ids, and write/submit bundle with patch request do this, but am hoping there was a simpler way.

view this post on Zulip John Silva (Sep 17 2021 at 18:57):

If your server supports it you can use a (transaction) bundle and have the request.method="PUT" and request.ifMatch with a search expression that could find these Observations by "business id" rather than the FHIR server's "logical" (internal) id. Of course with that you'd still need to have and know the unique "business ids" for the Observations you want to update.


Last updated: Apr 12 2022 at 19:14 UTC