Search Resource
New Search resource provides fine-grained control over search parameters
You can define search parameters or override the existing one with Search meta-resource. Search resource takes precedence over SearchParameter. This may be useful for performance optimization of built-in FHIR SearchParameters or for the implementation of complicated custom searches.
1
PUT /Search/Patient.name
2
​
3
resourceType: Search
4
id: Patient.name # id of Search resource
5
name: name # name of the new search parameter
6
resource: # link to the Patient resource
7
id: Patient
8
resourceType: Entity
9
where: "{{table}}.resource->>'name' ilike {{param}}" # sql for search
10
format: "%?%" # parameter format for ilike
11
order-by: "{{table}}.resource#>>'{name,0,family}'" # sql for ordering
Copied!

SQL Templating

In "where" and "order-by" expressions you can use {{table}} for table name and {{param}} for parameter.

format

You can provide the format string for value where ? will be replaced with the value of parameter. This feature may be useful for ilike expressions
You can define search parameters for different token syntax forms and :text modifier (other modifiers may be implemented in future). 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}} For :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: '%?%'
1
PUT /Search/<resourceType>.<parameter>
2
​
3
resourceType: Serach
4
name: <parameter>
5
resource: {id: <resourceType>, resourceType: Entity}
6
param-parser: token
7
token:
8
only-code: <SQL query for parameter={{param.code}}>
9
no-system: <SQL query for parameter=|{{param.code}}>
10
only-system: <SQL query for parameter={{param.system}}|>
11
both: <SQL query for parameter={{param.system}}|{{param.code}}>
12
text: <SQL query for parameter:text={{param.text}}>
13
text-format: <format string {{param.text}}>
Copied!

multi: array

If you set multi = 'array', parameters will be coerced as PostgreSQL array.

Examples (executable in REST console)

Search patient name with SQL ilike:

1
# create patient resource
2
​
3
PUT /Patient/my-patient
4
​
5
resourceType: Patient
6
id: my-patient
7
name:
8
- family: johnson
9
10
11
# create search resource
12
​
13
PUT /Search/Patient.name
14
​
15
resourceType: Search
16
id: Patient.name
17
name: name
18
resource:
19
id: Patient
20
resourceType: Entity
21
where: "{{table}}.resource->>'name' ilike {{param}}"
22
format: "%?%"
23
order-by: "{{table}}.resource#>>'{name,0,family}'"
24
​
25
​
26
# execute search for new parameter
27
# check query-sql field in response bundle
28
​
29
GET /Patient?name=john
30
​
31
GET /Patient?_sort=name
Copied!

Search patient identifiers with array search parameter:

1
# create patient resources (one query at a time)
2
​
3
PUT /Patient/my-patient
4
​
5
resourceType: Patient
6
id: my-patient
7
identifier:
8
- value: id1
9
10
###
11
​
12
PUT /Patient/my-patient-1
13
​
14
resourceType: Patient
15
id: my-patient-1
16
identifier:
17
- value: id2
18
19
20
# create search resource
21
​
22
PUT /Search/Patient.identifier
23
​
24
resourceType: Search
25
id: Patient.identifier
26
resource:
27
resourceType: Entity
28
id: Patient
29
where: knife_extract_text({{table}}.resource, '[["identifier","value"]]') && {{param}}
30
multi: array
31
​
32
​
33
# execute searches and retrieve two patients
34
# check query-sql field in response bundle
35
​
36
GET /Patient?identifier=id1,id2,id3
Copied!

token search:

1
PUT /
2
ServiceRequest.identifier
3
​
4
​
5
resourceType: Search
6
id: ServiceRequest.identifier
7
name: identifier
8
param-parser: token
9
token-sql:
10
only-code: 'knife_extract_text({{table}}.resource, ''[["identifier","value"]]'') && ARRAY[{{param.code}}]'
11
only-system: 'knife_extract_text({{table}}.resource, ''[["identifier", "system"]]'') && ARRAY[{{param.system}}]'
12
no-system: 'knife_extract_text({{table}}.resource, ''[["identifier","value"]]'') && ARRAY[{{param.code}}]'
13
both: '(knife_extract_text({{table}}.resource, ''[["identifier","value"]]'') && ARRAY[{{param.code}}]) AND ({{table}}.resource->''identifier'' @> jsonb_build_array(jsonb_build_object(''system'', {{param.system}}::text, ''value'', {{param.code}}::text)))'
14
text: 'array_to_string(knife_extract({{table}}.resource, ''[["identifier"]]''), '''') ilike {{param.text}}'
15
text-format: '%?%'
16
where: '(knife_extract_text({{table}}.resource, ''[["identifier","value"]]'') && ARRAY[{{param.code}}]) AND ({{table}}.resource->''identifier'' @> jsonb_build_array(jsonb_build_object(''system'', {{param.system}}::text, ''value'', {{param.code}}::text)))'
17
resource: {id: ServiceRequest, resourceType: Entity}
18
​
19
​
20
GET /ServiceRequest?identifier=foo
21
# will result in querying with knife_extract(...) && ARRAY['foo']
22
​
23
GET /ServiceRequest?identifier:text=foo
24
# will result in querying with array_to_string(knife_extract (...)) ilike '%foo%'
25
​
26
GET /ServiceRequest?identifier:not=foo
27
# will result fallback to default implementation NOT resource @> '{"identifier": [{"value": "foo"}]}'
Copied!
Last modified 8mo ago