Skip to content

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.

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 hour

Lookups 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.

Search for resources with codes that are members of a ValueSet:

Terminal window
# Find observations with codes in the vital-signs ValueSet
GET /fhir/Observation?code:in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
# Find conditions with ICD-10 diabetes codes
GET /fhir/Condition?code:in=http://example.org/ValueSet/diabetes-icd10

Exclude resources with codes in a ValueSet:

Terminal window
# Find observations that are NOT vital signs
GET /fhir/Observation?code:not-in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult

Search for a code and all its descendants (subsumption):

Terminal window
# Find conditions with diabetes or any subtype
# 73211009 = Diabetes mellitus
GET /fhir/Condition?code:below=http://snomed.info/sct|73211009

This matches:

  • 73211009 - Diabetes mellitus
  • 44054006 - Type 2 diabetes mellitus
  • 46635009 - Type 1 diabetes mellitus
  • And all other descendants

Search for a code and all its ancestors:

Terminal window
# Find conditions that subsume Type 2 diabetes
GET /fhir/Condition?code:above=http://snomed.info/sct|44054006

This matches:

  • 44054006 - Type 2 diabetes mellitus
  • 73211009 - Diabetes mellitus (parent)
  • And all other ancestors up to the root
Terminal window
# Exact SNOMED CT code match
GET /fhir/Condition?code=http://snomed.info/sct|73211009
# Hierarchy search
GET /fhir/Condition?code:below=http://snomed.info/sct|73211009

SNOMED CT hierarchy is resolved using ECL (Expression Constraint Language):

  • :below uses << code (descendants and self)
  • :above uses >> code (ancestors and self)
Terminal window
# Exact LOINC code
GET /fhir/Observation?code=http://loinc.org|8867-4
# LOINC panel members (via ValueSet)
GET /fhir/Observation?code:in=http://loinc.org/vs/LP200060-1
Terminal window
# ICD-10-CM diagnosis code
GET /fhir/Condition?code=http://hl7.org/fhir/sid/icd-10-cm|E11.9

: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 # default

A larger expansion is rejected with ExpansionTooLarge.

ValueSet expansions are cached based on cache_ttl_secs:

[terminology]
cache_ttl_secs = 3600 # 1 hour

Cache behavior:

  • The first request expands the ValueSet
  • Subsequent requests use the cached expansion
  • The cache entry expires after the TTL

Explicitly expand a ValueSet:

Terminal window
GET /fhir/ValueSet/$expand?url=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
POST /fhir/ValueSet/$expand
Content-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.

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 a code against a ValueSet:

Terminal window
GET /fhir/ValueSet/$validate-code?url=http://hl7.org/fhir/ValueSet/observation-vitalsignresult&system=http://loinc.org&code=8867-4
POST /fhir/ValueSet/$validate-code
Content-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"}
]
}

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:

Terminal window
GET /fhir/ValueSet/$validate-code?url=http://hl7.org/fhir/ValueSet/administrative-gender&code=male&_explain=true

In 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.

When the terminology server is unavailable:

{
"resourceType": "OperationOutcome",
"issue": [{
"severity": "error",
"code": "exception",
"diagnostics": "Terminology server unavailable: connection timeout"
}]
}
{
"resourceType": "OperationOutcome",
"issue": [{
"severity": "error",
"code": "not-found",
"diagnostics": "ValueSet not found: http://example.org/ValueSet/invalid"
}]
}
Terminal window
# All vital sign observations
GET /fhir/Observation?code:in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
# Heart rate observations (LOINC 8867-4) and children
GET /fhir/Observation?code:below=http://loinc.org|8867-4
Terminal window
# All diabetes and subtypes
GET /fhir/Condition?code:below=http://snomed.info/sct|73211009
# Only Type 2 diabetes specifically
GET /fhir/Condition?code=http://snomed.info/sct|44054006
Terminal window
# Observations that are NOT in the vitals ValueSet
GET /fhir/Observation?code:not-in=http://hl7.org/fhir/ValueSet/observation-vitalsignresult
# Conditions that are NOT diabetes-related
GET /fhir/Condition?code:not-in=http://example.org/ValueSet/diabetes-all
Terminal window
# Final vital sign observations from January 2024
GET /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 patient
GET /fhir/Condition?patient=Patient/123&clinical-status=active&code:below=http://snomed.info/sct|73211009

If ValueSet expansions are slow:

  1. Check terminology server latency
  2. Increase cache_ttl_secs for frequently used ValueSets
  3. Consider using a local terminology server

A search that fails with ExpansionTooLarge expands to more codes than search.max_valueset_expansion allows. Narrow the ValueSet or raise the limit.

Enable debug logging:

[logging]
level = "debug"