Terminology Services
OctoFHIR integrates with external terminology servers to provide ValueSet expansion, code system validation, and advanced search modifiers like :in, :not-in, :below, and :above.
Configuration
Section titled “Configuration”Terminology services are always on. Set the external terminology server in your octofhir.toml:
[terminology]server_url = "https://tx.fhir.org/r4"cache_ttl_secs = 3600 # Cache expansions for 1 hourLookups try the installed FHIR packages first, then the external server. Any FHIR R4 compatible terminology server works. The default is https://tx.fhir.org/r4.
ValueSet Search Modifiers
Section titled “ValueSet Search Modifiers”:in Modifier
Section titled “:in Modifier”Search for resources with codes that are members of a ValueSet:
# Find observations with codes in the vital-signs ValueSetGET /fhir/Observation?code:in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
# Find conditions with ICD-10 diabetes codesGET /fhir/Condition?code:in=http://example.org/ValueSet/diabetes-icd10:not-in Modifier
Section titled “:not-in Modifier”Exclude resources with codes in a ValueSet:
# Find observations that are NOT vital signsGET /fhir/Observation?code:not-in=http://hl7.org/fhir/ValueSet/observation-vitalsignresultHierarchy Modifiers (SNOMED CT)
Section titled “Hierarchy Modifiers (SNOMED CT)”:below Modifier
Section titled “:below Modifier”Search for a code and all its descendants (subsumption):
# Find conditions with diabetes or any subtype# 73211009 = Diabetes mellitusGET /fhir/Condition?code:below=http://snomed.info/sct|73211009This matches:
- 73211009 - Diabetes mellitus
- 44054006 - Type 2 diabetes mellitus
- 46635009 - Type 1 diabetes mellitus
- And all other descendants
:above Modifier
Section titled “:above Modifier”Search for a code and all its ancestors:
# Find conditions that subsume Type 2 diabetesGET /fhir/Condition?code:above=http://snomed.info/sct|44054006This matches:
- 44054006 - Type 2 diabetes mellitus
- 73211009 - Diabetes mellitus (parent)
- And all other ancestors up to the root
Code Systems
Section titled “Code Systems”SNOMED CT
Section titled “SNOMED CT”# Exact SNOMED CT code matchGET /fhir/Condition?code=http://snomed.info/sct|73211009
# Hierarchy searchGET /fhir/Condition?code:below=http://snomed.info/sct|73211009SNOMED CT hierarchy is resolved using ECL (Expression Constraint Language):
:belowuses<< code(descendants and self):aboveuses>> code(ancestors and self)
# Exact LOINC codeGET /fhir/Observation?code=http://loinc.org|8867-4
# LOINC panel members (via ValueSet)GET /fhir/Observation?code:in=http://loinc.org/vs/LP200060-1ICD-10
Section titled “ICD-10”# ICD-10-CM diagnosis codeGET /fhir/Condition?code=http://hl7.org/fhir/sid/icd-10-cm|E11.9Performance Optimization
Section titled “Performance Optimization”Expansion Limit
Section titled “Expansion Limit”:in, :not-in, :above and :below expand the ValueSet or hierarchy first and put every code into the search query. search.max_valueset_expansion limits how many codes this may be:
[search]max_valueset_expansion = 500 # defaultA larger expansion is rejected with ExpansionTooLarge.
Caching
Section titled “Caching”ValueSet expansions are cached based on cache_ttl_secs:
[terminology]cache_ttl_secs = 3600 # 1 hourCache behavior:
- The first request expands the ValueSet
- Subsequent requests use the cached expansion
- The cache entry expires after the TTL
$expand Operation
Section titled “$expand Operation”Explicitly expand a ValueSet:
GET /fhir/ValueSet/$expand?url=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
POST /fhir/ValueSet/$expandContent-Type: application/fhir+json
{ "resourceType": "Parameters", "parameter": [ {"name": "url", "valueUri": "http://hl7.org/fhir/ValueSet/observation-vitalsignresult"}, {"name": "filter", "valueString": "heart"}, {"name": "count", "valueInteger": 100} ]}GET with query parameters and POST with a Parameters resource behave the same for
$expand, $lookup, $validate-code, $subsumes, $translate and $closure. Value sets,
code systems and concept maps are resolved from installed packages.
A value set whose definition can be computed here takes compose.lockedDate into account: what is
not tied to a version comes in the latest version dated by then, and a version named outright has to
be dated by then too. A code system with caseSensitive = false matches codes in any case, and the
supplements named by the valueset-supplement extension add their properties and designations. What
cannot be determined, such as a supplement that is not found, fails the expansion rather than giving
a wrong one.
Expansion Parameters
Section titled “Expansion Parameters”| Parameter | Type | Description |
|---|---|---|
url |
uri | ValueSet URL to expand |
filter |
string | Filter expansion by display text |
count |
integer | Limit number of codes returned |
offset |
integer | Pagination offset |
includeDesignations |
boolean | Include alternate designations |
$validate-code Operation
Section titled “$validate-code Operation”Validate a code against a ValueSet:
GET /fhir/ValueSet/$validate-code?url=http://hl7.org/fhir/ValueSet/observation-vitalsignresult&system=http://loinc.org&code=8867-4
POST /fhir/ValueSet/$validate-codeContent-Type: application/fhir+json
{ "resourceType": "Parameters", "parameter": [ {"name": "url", "valueUri": "http://hl7.org/fhir/ValueSet/observation-vitalsignresult"}, {"name": "coding", "valueCoding": { "system": "http://loinc.org", "code": "8867-4" }} ]}Response:
{ "resourceType": "Parameters", "parameter": [ {"name": "result", "valueBoolean": true}, {"name": "display", "valueString": "Heart rate"} ]}Which definitions decided the answer
Section titled “Which definitions decided the answer”When several package versions are loaded, the same code can pass in one place and fail in another.
With _explain=true, $validate-code adds an explain output parameter that names the definitions it
used and where they came from. It works at every level the operation has:
GET /fhir/ValueSet/$validate-code?url=http://hl7.org/fhir/ValueSet/administrative-gender&code=male&_explain=trueIn a POST, send {"name": "_explain", "valueBoolean": true}.
{ "resourceType": "Parameters", "parameter": [ {"name": "result", "valueBoolean": true}, {"name": "explain", "part": [ {"name": "type", "valueCode": "ValueSet"}, {"name": "url", "valueUri": "http://hl7.org/fhir/ValueSet/administrative-gender"}, {"name": "version", "valueString": "4.0.1"}, {"name": "source", "valueString": "hl7.fhir.r4.core#4.0.1"}, {"name": "method", "valueCode": "compose"}, {"name": "codeSystem", "part": [ {"name": "type", "valueCode": "CodeSystem"}, {"name": "url", "valueUri": "http://hl7.org/fhir/administrative-gender"}, {"name": "version", "valueString": "4.0.1"}, {"name": "source", "valueString": "hl7.fhir.r4.core#4.0.1"} ]} ]} ]}| Part | Meaning |
|---|---|
type |
ValueSet or CodeSystem: the kind of definition that decided the result |
url, version |
The definition’s canonical URL and version, when it has them |
source |
Where the definition came from: the package as name#version, or stored for a resource read by id (ValueSet/{id}/$validate-code) |
method |
ValueSet only: expansion when the definition carries an expansion, compose otherwise |
codeSystem |
ValueSet only: the same parts for the code system, from the system parameter or inferred from the value set. When no definition of it is loaded, the parts are url and source with the value not loaded: the code was checked against the value set’s listing alone |
The Terminology page of the web console asks for _explain when it checks a
code and shows these definitions under the result.
Error Handling
Section titled “Error Handling”Terminology Server Unavailable
Section titled “Terminology Server Unavailable”When the terminology server is unavailable:
{ "resourceType": "OperationOutcome", "issue": [{ "severity": "error", "code": "exception", "diagnostics": "Terminology server unavailable: connection timeout" }]}Invalid ValueSet
Section titled “Invalid ValueSet”{ "resourceType": "OperationOutcome", "issue": [{ "severity": "error", "code": "not-found", "diagnostics": "ValueSet not found: http://example.org/ValueSet/invalid" }]}Examples
Section titled “Examples”Search for Vital Signs
Section titled “Search for Vital Signs”# All vital sign observationsGET /fhir/Observation?code:in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
# Heart rate observations (LOINC 8867-4) and childrenGET /fhir/Observation?code:below=http://loinc.org|8867-4Search for Diabetes Conditions
Section titled “Search for Diabetes Conditions”# All diabetes and subtypesGET /fhir/Condition?code:below=http://snomed.info/sct|73211009
# Only Type 2 diabetes specificallyGET /fhir/Condition?code=http://snomed.info/sct|44054006Exclude Specific Codes
Section titled “Exclude Specific Codes”# Observations that are NOT in the vitals ValueSetGET /fhir/Observation?code:not-in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
# Conditions that are NOT diabetes-relatedGET /fhir/Condition?code:not-in=http://example.org/ValueSet/diabetes-allCombined with Other Parameters
Section titled “Combined with Other Parameters”# Final vital sign observations from January 2024GET /fhir/Observation?status=final&code:in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult&date=ge2024-01-01&date=lt2024-02-01
# Active diabetes conditions for a specific patientGET /fhir/Condition?patient=Patient/123&clinical-status=active&code:below=http://snomed.info/sct|73211009Troubleshooting
Section titled “Troubleshooting”Slow Expansions
Section titled “Slow Expansions”If ValueSet expansions are slow:
- Check terminology server latency
- Increase
cache_ttl_secsfor frequently used ValueSets - Consider using a local terminology server
Rejected Searches
Section titled “Rejected Searches”A search that fails with ExpansionTooLarge expands to more codes than search.max_valueset_expansion allows. Narrow the ValueSet or raise the limit.
Debugging
Section titled “Debugging”Enable debug logging:
[logging]level = "debug"