Search resource provides fine-grained control over search parameters
Search meta-resource can be used to define search parameters or override the existing one. Search resources take precedence over SearchParameters. This may be useful for performance optimization of built-in FHIR SearchParameters or for the implementation of complicated custom searches.
PUT /Search/Patient.name
content-type: text/yaml
accept: text/yaml
resourceType: Search
id: Patient.name # id of Search resource
name: name # name of the new search parameter
resource: # link to the Patient resource
id: Patient
resourceType: Entity
where: "{{table}}.resource->>'name' ilike {{param}}" # sql for search
format: "%?%" # parameter format for ilike
order-by: "{{table}}.resource#>>'{name,0,family}'" # sql for ordering
Use {{table}} for table name and {{param}} for parameter in "where" and "order-by" expressions.
Provided format parameter will be passed to {{param}} (? will be replaced with the value of parameter). This feature may be useful writing ilike expressions.
Token search
You can define Search resources for different token syntax forms and :text modifier.
To refer to system and code in SQL query use {{param.system}} and {{param.code}} accordingly.
To refer to value of param with :text modifier use {{param.text}}
When using the :text modifier you also need to specify "text-format", refer to {{param.text}} with ?.
"text-format" is a format string which will be applied to{{param.text}} before inserting into SQL query. It is useful for wrapping text with % for like or ilike. For example text-format: '%?%'
PUT /Search/<resourceType>.<parameter>
content-type: text/yaml
accept: text/yaml
resourceType: Search
name: <parameter>
resource: {id: <resourceType>, resourceType: Entity}
param-parser: token
token:
only-code: <SQL query for parameter={{param.code}}>
no-system: <SQL query for parameter=|{{param.code}}>
only-system: <SQL query for parameter={{param.system}}|>
both: <SQL query for parameter={{param.system}}|{{param.code}}>
text: <SQL query for parameter:text={{param.text}}>
text-format: <format string {{param.text}}>
Reference search
Allows use different reference types in "where" expression. Reference can be defined in several ways:
{{param.resourceType}} for ResourceType and {{param.id}} for resource id
# execute searches and retrieve two patients
# check query-sql field in response bundle
GET /Patient?identifier=id1,id2,id3
content-type: text/yaml
accept: text/yaml
Examples
Search patient name with SQL ilike
# create patient resource
PUT /Patient/my-patient
content-type: text/yaml
accept: text/yaml
resourceType: Patient
id: my-patient
name:
- family: johnson
# execute search for new parameter
# check query-sql field in response bundle
GET /Patient?name=john
content-type: text/yaml
accept: text/yaml
GET /Patient?_sort=name
content-type: text/yaml
accept: text/yaml
GET /ServiceRequest?identifier=foo
content-type: text/yaml
accept: text/yaml
# will result in querying with knife_extract(...) && ARRAY['foo']
GET /ServiceRequest?identifier:text=foo
content-type: text/yaml
accept: text/yaml
# will result in querying with array_to_string(knife_extract (...)) ilike '%foo%'
GET /ServiceRequest?identifier:not=foo
content-type: text/yaml
accept: text/yaml
# will result fallback to default implementation NOT resource @> '{"identifier": [{"value": "foo"}]}'