Search code examples
hl7-fhir

How to search on FHIR using complex nested queries


I haven't really found examples or instructions on how a complex nested query should look like when searching a FHIR resource.

Some examples (pseudo-code):

  • (name=Mary AND gender=female) OR (address-city=Springfield AND address-state=NY)
  • ((name=Mary AND gender=female) OR (address-city=Springfield & address-state=NY)) AND active=true

Is that even possible? If yes, how?


Solution

  • FHIR supports quite an elaborate search syntax, but it isn't a query language. The searches you want cannot be done in 1 go with this, unless you have access to the server and can implement the queries on that yourself.

    If you have access/influence server side, you can implement a named query, and then use the _query search parameter to execute that (see http://www.hl7.org/fhir/search.html#query).

    If you don't have that access, you can perform your queries in a couple of steps. For example your first one would take 2 queries:

    GET [fhir endpoint]/Patient?name=Mary&gender=female
    GET [fhir endpoint]/Patient?address-city=Springfield&address-state=NY
    

    Both would give you a Bundle of results. The two Bundles together would be the complete list of matching resources you were looking for.

    For the second example query, you would need to supply both GETs with &active=true.