Last updated
Was this helpful?
Last updated
Was this helpful?
Sometimes, FHIR search is not enough. Aidbox provides more ways to search:
- similar to SearchParameter, but allows defining SQL. It is composable with other SearchParameters and Search resources.
- 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.
- search without SearchParameters.
- efficient lookup for resources by key attributes.
Also see , e.g. _explain
and _timeout
.
Aidbox Search resource defines a search parameter or overrides the existing one. Search resources take precedence over FHIR . This may be useful for the performance optimization of built-in FHIR SearchParameters or for the implementation of complicated custom searches.
In case of reference or identifier search, the value must be parsed:
resourceType
and id
or url
in case of reference search
code
and system
in case of identifier search
{{param.resourceType}}
for ResourceType
and {{param.id}}
for resource id
{{param.id}}
for resource id
{{param.url}}
for resource url
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:
With the AidboxQuery resource, you can turn your SQL query into a REST endpoint.
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}}
.
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.
AidboxQuery expects that parameters _count
and _page
(exactly such names) are defined, otherwise links won't be attached.
Use like this:
To design the aidbox query, you can use POST /$query/$debug
endpoint without the need to create an AidboxQuery resource:
You can debug AidboxQuery with _explain=true
parameter:
With parameters started with .
, you can provide the exact path for the element, optionally provide coercing after ::
using PostgreSQL types and the operator after $
.
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.
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.
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 by
q
: is +
or space separated term (prefixes) to search
limit
: 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
Use _explain
parameter to inspect the search query execution plan.
Response:
This parameter can be used for debugging too. If an SQL error happens, _explain
will show the original query:
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.
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.
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
is experimental SearchParameter. It can be used to search for a specified language.
Structure of a resource containing translation extension:
See .
Allows the use of different reference types in the "where" expression. Reference can be defined :
You can use enable-links
parameter to include in the response. Here is simple example how to use paging with AidboxQuery and include links.
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 .
See also setting.
Any (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 in FHIR.
Aidbox searches for and if the resource contains it and the language is correct, then searches by the content of this translation.