On this page ⌄
Extract search parameters
Use the generated search-parameter extractors to pull indexable values from resources, and know which parameters they cover.
Goal and scope
This page shows how to get search parameter values from a resource, for example to build a search index, and how to handle parameters the library cannot extract. Examples use R4. For how the extractors are made, see How the model maps FHIR.
Extract one parameter
Each resource type has a SearchParams object in the search package, such as dev.ohs.fhir.model.r4.search.PatientSearchParams. Each parameter in it is a SearchParam<R, T>. R is the resource type, and T is the type of the values it returns. A parameter also has its name, type, FHIRPath expression, and target resource types.
import dev.ohs.fhir.model.r4.search.PatientSearchParams
val birthDates: List<Date> = PatientSearchParams.birthdate.extractFrom(patient)
You can also call extract() on the resource. The result is the same.
import dev.ohs.fhir.model.r4.search.extract
val birthDates: List<Date> = patient.extract(PatientSearchParams.birthdate)
Extract every supported parameter
Each SearchParams object has two lists.
allcontains the parameters that can be extracted. You can safely callextractFrom()on each of them.unsupportedcontains the parameters whose expressions the generator cannot handle. TheirextractFrom()throwsNotImplementedError.
To build an index, iterate all.
PatientSearchParams.all.forEach { searchParam ->
val values = searchParam.extractFrom(patient)
// add the values to your index
}
Check unsupported for the parameters your app needs. They are not in all, so an index built from all does not contain them.
Get values for an unsupported parameter
Most unsupported parameters still have their full FHIRPath expression. Evaluate it with Kotlin FHIRPath to get the values.
Some unsupported parameters have no usable expression, so Kotlin FHIRPath cannot get their values either. If your app needs them, compute the values in your own code.
- An empty expression, because the specification does not define one. For example,
ageon Patient. - Only the resource name, such as
Observation. Composite parameters such ascode-value-quantitylook like this, because their values come from several other parameters.
Some parameters use a union of several paths, such as (Observation.value as string) | (Observation.value as CodeableConcept).text for value-string. If only some paths are supported, extractFrom() returns values from those paths and skips the rest, without an error. If your app needs the skipped paths, evaluate the full expression with Kotlin FHIRPath instead.
The repository lists which expressions are supported, which unions are only partly extracted, and every unsupported parameter by name. These lists change as support grows.
Know how extracted values behave
Three kinds of expressions return values you might not expect.
- A choice element without a type, such as
ChargeItem.occurrence, returns the choice interface, such asChargeItem.Occurrence, not the value inside it. where(resolve() is Patient)does not look up the referenced resource. It checks if the reference text containsPatient/, such asPatient/123. It does not match URNs, contained resources, or references without a URL.- A union returns the values of all its supported paths, in order, without duplicates. If the paths return different types, such as
dateTimeandPeriod, the values have the typeAny.
Choose generated extraction or Kotlin FHIRPath
Use the generated functions when every parameter you need is in all. They run as plain Kotlin code and need no other library.
Use Kotlin FHIRPath when you need parameters from unsupported, the skipped paths of a union, or your own expressions. Kotlin FHIR Engine takes this approach and evaluates every search parameter with Kotlin FHIRPath.
Next step
Troubleshooting lists common errors and how to fix them.