Aidbox Search
Sometimes, FHIR search is not enough. Aidbox provides more ways to search:
Search resource - similar to SearchParameter, but allows defining SQL. It is composable with other SearchParameters and Search resources.
AidboxQuery - a more general way to search using SQL. DSL to build complex queries using the new endpoint. It is not composable with other SearchParameters and Search resources.
Dot expressions - search without SearchParameters.
$lookup - efficient lookup for resources by key attributes.
Also see special search parameters defined by Aidbox, e.g. _explain
and _timeout
.
Search resource
Aidbox Search resource defines a search parameter or overrides the existing one. Search resources take precedence over FHIR SearchParameters. This may be useful for the performance optimization of built-in FHIR SearchParameters or for the implementation of complicated custom searches.
Example
Search resource structure
Value parsing
In case of reference or identifier search, the value must be parsed:
resourceType
andid
orurl
in case of reference searchcode
andsystem
in case of identifier search
Reference search
Allows the use of different reference types in the "where" expression. Reference can be defined in several ways:
{{param.resourceType}}
forResourceType
and{{param.id}}
for resourceid
{{param.id}}
for resourceid
{{param.url}}
for resourceurl
Token search
To refer to the system and code in the SQL query, use {{param.system}}
and {{param.code}}
accordingly.
To refer to the value of the parameter 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 that will be applied to{{param.text}}
before inserting it into the SQL query. It is useful for wrapping text with %
for like
or ilike.
For example text-format: '%?%'
See tutorial:
Create custom Aidbox Search resourceAidboxQuery
With the AidboxQuery resource, you can turn your SQL query into a REST endpoint.
Example
For example, let's create a simple aggregation report for encounters parameterised by date. Create an AidboxQuery
resource:
Call it like this (data and query keys):
Or like this (Search Bundle):
The main difference is that such a query can use an additional variable available in the context of {{resourceType}}
.
Query types
AidboxQuery has type
field, which can be either query
or execute
. Default type is query. This means that SELECT statement in query parameter is expected. If you want to make SQL query with execute statements e.g. TRUNCATE, use execute
type.
Return links
You can use enable-links
parameter to include links in the response. Here is simple example how to use paging with AidboxQuery and include links.
AidboxQuery expects that parameters _count
and _page
(exactly such names) are defined, otherwise links won't be attached.
Use like this:
Design AidboxQuery
To design the aidbox query, you can use POST /$query/$debug
endpoint without the need to create an AidboxQuery resource:
Debug AidboxQuery
You can debug AidboxQuery with _explain=true
parameter:
Dot expressions
With parameters started with .
, you can provide the exact path for the element, optionally provide coercing after ::
using PostgreSQL types and the operator after $
.
Example
Note: expressions with typecast require user input to be correct by PostgreSQL syntax. For example, for timestamptz
values you must use 2015-01-01T00:00:00Z
format.
$lookup
There are scenarios when you want to quickly look up patients or practitioners with the prefix search by multiple key elements like a family name, date of birth, and identifier. Prefix search means you want to say in the query string jo do 79
and find John Doe with 1979 birthdate
. Sometimes, there are millions of patients in your database, and you want to do it efficiently to show type-ahead dropdown choices in your UI.
$lookup operations are especially designed to be an efficient implementation for this case.
There is no way to implement an efficient multidimensional prefix search with ranking and sorting in a relational database. $lookup based on specific assumptions to find the right trade-off: if the search returns more than count (by default 50) results, we consider that the search failed and results can have some anomalies, for example, not complete sorting.
Here is how it works.
First of all, you have to describe priority groups of attributes with by parameter. Groups are separated by ;
and inside group, you specify the list of paths separated by ,
. Each path expression consists of dot separated elements and indexes and should end with primitive type (examples: name.given
or identifier.value
).
The result will be sorted according to an order of priority groups. For example, if you want to rate first matches of name, identifier, and birth of data, and second matches in address and telecom, you will use the following expression:name.family,name.given,identifier.value,birthDate;address.state,address.city,address.line,telecom.value
.
Let's say you are searching joh 1979 ny
Aidbox will initially search in first priority group by expression like this:
If this query returns 50 records, Aidbox will respond with these records.
Parameters
Note: Each path expression should point to a primitive element!
by
:;
-separated list of priority groups. Each group is,
-separated list of path expressions.sort
:,
-separated list of path expressions to sort byq
: is+
or space separated term (prefixes) to searchlimit
: is internal search limit (default 200)count
: number results to return (default 50)mode
: if mode =index
Aidbox returns index DDL for a specific search
Aidbox special search parameters
_explain
Use _explain
parameter to inspect the search query execution plan.
Response:
If your query is slow and you see Seq Scans , it's time to build indexes. Do not forget to run a vacuum analyze on the tables involved in query. Read more about PostgreSQL Explain.
This parameter can be used for debugging too. If an SQL error happens, _explain
will show the original query:
_timeout
With _timeout
parameter, you can control the search query timeout in seconds. If the query takes more than the timeout value, it will be cancelled. The default timeout value is 60 seconds.
_createdAt
Search by the creation time of the resource meta.createdAt
(cts
column in the database table).
You can use operators lt,le,gt,ge
like in other date search parameters.
_result
By default, the search result is returned as a FHIR Bundle. You can change this behavior by setting _result=array
and your search result will be returned as JSON array with resources, without the Bundle envelope:
_search-language
_search-language
is experimental SearchParameter. It can be used to search for a specified language.
Any string search parameters (e.g. name) will search in the desired language if _search-language
is specified in the query. Specifying only _search-language
without any other string search parameters won't affect anything (except _sort
).
You may use locales that are specified in FHIR.
Resources with Translation Extension
Aidbox searches for Translation Extension and if the resource contains it and the language is correct, then searches by the content of this translation.
Structure of a resource containing translation extension:
Last updated
Was this helpful?