Skip to content

Search Parameters

OctoFHIR supports comprehensive FHIR search capabilities including all standard modifiers, date prefixes, and advanced features like _include and _revinclude.

For indexing behavior and performance-oriented query design, see Search Indexing.

You can use FHIR search normally; each parameter becomes a predicate over the stored resource JSON, and PostgreSQL indexes speed it up. There are no side tables of extracted values.

Search Pattern How it runs
reference params (subject, patient, etc.) Overlap on the references in the JSON; a functional GIN index when the parameter is in search.indexed_params
date params (including precision and ranges) Range overlap, with a functional GiST index when listed
string params Match on the extracted text, with a functional trigram GIN index when listed
token params JSON containment, on an index of the element when listed, or on the whole-document GIN index
_include, _revinclude, chained search Follow the references in the JSON

A parameter that is not listed still works. See Search Indexing for the full table.

  1. Prefer typed references when possible: subject=Patient/123 is clearer than subject=123.
  2. Use bounded date ranges for large datasets: ge... + lt....
  3. Use :exact only when you really need exact text matching semantics.
  4. Keep _include/_revinclude focused (paginate aggressively on wide relationships).

Search for resources using query parameters:

Terminal window
# Search for patients by family name
GET /fhir/Patient?family=Smith
# Search with multiple parameters (AND logic)
GET /fhir/Patient?family=Smith&gender=female
# Search with multiple values (OR logic)
GET /fhir/Patient?family=Smith,Johnson

String search parameters support the following modifiers:

Terminal window
# Matches "Smith", "Smithson", "Smithfield"
GET /fhir/Patient?family=Smith

Case-sensitive exact match:

Terminal window
# Matches only "Smith" (not "smith" or "SMITH")
GET /fhir/Patient?family:exact=Smith

Substring match anywhere in the value:

Terminal window
# Matches "Smith", "Blacksmith", "Smithson"
GET /fhir/Patient?family:contains=mit

Find resources where the element is present or absent:

Terminal window
# Patients without an identifier
GET /fhir/Patient?identifier:missing=true
# Patients with at least one identifier
GET /fhir/Patient?identifier:missing=false

Token search parameters (for codes, identifiers, etc.) support:

Terminal window
# Search by system and code
GET /fhir/Patient?identifier=http://hospital.org/mrn|MRN-001
# Search by code only (any system)
GET /fhir/Patient?identifier=MRN-001
# Search by system only (any code)
GET /fhir/Patient?identifier=http://hospital.org/mrn|

:text on a token or string parameter is refused with an error. The server has no index path for it and does not fall back to walking the JSON. Use :contains on a string parameter, or search by code.

Exclude resources with matching codes:

Terminal window
# Patients that are NOT male
GET /fhir/Patient?gender:not=male

For Identifier searches, match by identifier type:

Terminal window
# Find patients by MRN type identifier
GET /fhir/Patient?identifier:of-type=http://terminology.hl7.org/CodeSystem/v2-0203|MR

Date search parameters support precision-aware comparisons:

Prefix Meaning Example
eq Equal (default) birthdate=eq1980-01-15
ne Not equal birthdate=ne1980-01-15
lt Less than birthdate=lt1990-01-01
le Less than or equal birthdate=le1990-01-01
gt Greater than birthdate=gt1980-01-01
ge Greater than or equal birthdate=ge1980-01-01
sa Starts after date=sa2024-01-01
eb Ends before date=eb2024-12-31
ap Approximately date=ap2024-06-15

Combine prefixes to search within ranges:

Terminal window
# Patients born in the 1980s
GET /fhir/Patient?birthdate=ge1980-01-01&birthdate=lt1990-01-01
# Observations from January 2024
GET /fhir/Observation?date=ge2024-01-01&date=lt2024-02-01

Dates match at their precision level:

Terminal window
# Matches any date in 1980
GET /fhir/Patient?birthdate=1980
# Matches any date in January 1980
GET /fhir/Patient?birthdate=1980-01
# Matches exactly January 15, 1980
GET /fhir/Patient?birthdate=1980-01-15

Bundle parameters such as composition chain into the resource inside the entry, not through a reference: Bundle?composition.subject=Patient/123. See FHIR R6.

Include referenced resources in the response:

Terminal window
# Search observations and include referenced Patient
GET /fhir/Observation?code=8867-4&_include=Observation:subject
# Include multiple reference types
GET /fhir/Observation?_include=Observation:subject&_include=Observation:performer

Include resources that reference the search results:

Terminal window
# Search patients and include their observations
GET /fhir/Patient?_revinclude=Observation:subject
# Include multiple reverse references
GET /fhir/Patient?_revinclude=Observation:subject&_revinclude=Condition:subject

For recursive includes (follow reference chains):

Terminal window
# Include subject and the subject's organization
GET /fhir/Observation?_include=Observation:subject&_include:iterate=Patient:organization

Return only specific elements:

Terminal window
# Return only id, name, and birthDate
GET /fhir/Patient?_elements=id,name,birthDate

Resources returned with _elements are tagged with SUBSETTED in their meta.

Return summarized resources:

Value Description
true Return summary elements only
false Return full resources (default)
text Return text, id, meta, and mandatory elements
data Return data elements only (no text)
count Return only the count, no entries
Terminal window
# Get summary view
GET /fhir/Patient?_summary=true
# Get just the count
GET /fhir/Patient?_summary=count

Limit the number of results:

Terminal window
# Return first 10 results
GET /fhir/Patient?_count=10

Use the link elements in the Bundle response rather than building paging URLs:

{
"resourceType": "Bundle",
"type": "searchset",
"link": [
{"relation": "self", "url": "https://fhir.example.org/fhir/Patient?_count=10&name=smith"},
{"relation": "first", "url": "https://fhir.example.org/fhir/Patient?_count=10&_offset=0&name=smith"},
{"relation": "next", "url": "https://fhir.example.org/fhir/Patient?_count=10&_offset=10&name=smith"}
]
}

A search pages in one of two ways. Keyset pages continue from the last row of the page before instead of counting rows to skip.

Search Next page
Sorted by exactly one of _id, -_id, _lastUpdated, -_lastUpdated _cursor
A listing: no search criteria and no _sort (_count, _include, _summary and the like are fine) _cursor, in _id order
Any other search _offset
GET /fhir/Patient?_sort=-_lastUpdated&_count=50

The next link of the response carries the cursor:

{"relation": "next", "url": "https://fhir.example.org/fhir/Patient?_sort=-_lastUpdated&_count=50&_cursor=..."}
  • _cursor comes from a next link and is opaque: do not parse or build one. A cursor is valid only with the search that produced it. On a search that cannot seek, _cursor is rejected.
  • Keyset pages have no last link, since a seek has no way there. self repeats the cursor of the page and first starts over. A page reached by cursor has no previous link.
  • A listing with _offset keeps paging by offset.
  • _id order seeks on the primary key, so a page costs the same at any depth. Resource tables have no index on the last update time by default; for _lastUpdated order on a large table, see what the index advisor suggests.

POST /fhir/{type}/_search can carry more criteria than fit in a URL. When a paging link would be longer than 2048 characters, the links carry a _page token instead of the criteria, next to _count and _offset or _cursor. The server keeps the query for 30 minutes after the last page was read. An expired token is 410: run the search again.

Sort results by one or more parameters:

Terminal window
# Sort by birthDate ascending
GET /fhir/Patient?_sort=birthdate
# Sort by birthDate descending
GET /fhir/Patient?_sort=-birthdate
# Sort by multiple fields
GET /fhir/Patient?_sort=family,-birthdate

Search using composite parameters that combine multiple values:

Terminal window
# Search by code AND value (composite parameter)
GET /fhir/Observation?code-value-quantity=http://loinc.org|8867-4$gt60

Search by properties of referenced resources:

Terminal window
# Observations for patients named "Smith"
GET /fhir/Observation?subject:Patient.family=Smith
# Observations for patients born after 1980
GET /fhir/Observation?subject:Patient.birthdate=gt1980-01-01

These parameters work on all resource types:

Parameter Type Description
_id token Resource logical ID
_lastUpdated date When the resource was last updated
_profile uri Profiles the resource claims to conform to
_tag token Tags applied to the resource
_security token Security labels on the resource
_source uri Source URI for the resource
Terminal window
# Active female patients named Smith, born in the 1980s,
# sorted by birthdate, returning only id and name
GET /fhir/Patient?family=Smith&gender=female&active=true&birthdate=ge1980-01-01&birthdate=lt1990-01-01&_sort=birthdate&_elements=id,name
Terminal window
# Final observations with heart rate code,
# include the patient, return first 20
GET /fhir/Observation?status=final&code=http://loinc.org|8867-4&_include=Observation:subject&_count=20
Terminal window
# Patients without phone numbers
GET /fhir/Patient?telecom:missing=true
# Observations without values
GET /fhir/Observation?value-quantity:missing=true

OctoFHIR supports creating custom SearchParameter resources that are automatically registered and immediately available for search operations.

Custom search parameters can be created in two ways:

  1. Via FHIR Packages - Include SearchParameter resources in your Implementation Guide
  2. Via REST API - Create SearchParameter resources directly through the FHIR API

Create a SearchParameter resource using the standard FHIR create operation:

Terminal window
POST /fhir/SearchParameter
Content-Type: application/fhir+json
{
"resourceType": "SearchParameter",
"url": "http://example.org/SearchParameter/patient-custom-field",
"name": "PatientCustomField",
"code": "custom-field",
"status": "active",
"description": "Search by custom patient field",
"base": ["Patient"],
"type": "string",
"expression": "Patient.extension.where(url='http://example.org/custom-field').valueString"
}

Response:

{
"resourceType": "SearchParameter",
"id": "12345",
"meta": {
"versionId": "1",
"lastUpdated": "2025-01-02T10:30:00Z"
},
"url": "http://example.org/SearchParameter/patient-custom-field",
"code": "custom-field",
...
}

When you create, update, or delete a SearchParameter resource:

  1. FHIRPath Validation - The expression is validated for syntax correctness
  2. Immediate Registration - The parameter is registered in the search registry
  3. Zero Downtime - Available immediately without server restart
  4. Lock-Free - No impact on concurrent search operations
Terminal window
# Create the SearchParameter
POST /fhir/SearchParameter { ... }
# Immediately use it in search (no restart needed!)
GET /fhir/Patient?custom-field=some-value

Invalid FHIRPath expressions are rejected:

Terminal window
POST /fhir/SearchParameter
{
"resourceType": "SearchParameter",
"expression": "Patient..invalid..syntax", # Invalid!
...
}

Response (400 Bad Request):

{
"resourceType": "OperationOutcome",
"issue": [{
"severity": "error",
"code": "invalid",
"diagnostics": "Invalid FHIRPath expression: unexpected token"
}]
}

Required fields:

  • code - The search parameter name (used in queries)
  • url - Canonical URL (must be unique)
  • type - Parameter type: string, token, reference, date, number, quantity, uri, composite, special
  • base - Resource types this parameter applies to (array)

Optional but recommended:

  • expression - FHIRPath expression defining what to search
  • description - Human-readable description
  • modifier - Supported modifiers (:exact, :contains, etc.)
  • comparator - Supported comparators for numbers/dates (eq, gt, lt, etc.)
  • target - Target resource types (for reference type parameters)

Search by extension values:

{
"resourceType": "SearchParameter",
"url": "http://example.org/SearchParameter/patient-ethnicity",
"code": "ethnicity",
"type": "token",
"base": ["Patient"],
"expression": "Patient.extension.where(url='http://example.org/ethnicity').valueCoding"
}

Usage:

Terminal window
GET /fhir/Patient?ethnicity=http://example.org/ethnicity-codes|hispanic

Search by nested fields:

{
"resourceType": "SearchParameter",
"url": "http://example.org/SearchParameter/patient-home-city",
"code": "home-city",
"type": "string",
"base": ["Patient"],
"expression": "Patient.address.where(use='home').city"
}

Usage:

Terminal window
GET /fhir/Patient?home-city=Boston

Apply the same parameter to multiple resource types:

{
"resourceType": "SearchParameter",
"url": "http://example.org/SearchParameter/clinical-custom-status",
"code": "custom-status",
"type": "token",
"base": ["Observation", "Condition", "Procedure"],
"expression": "%resource.extension.where(url='http://example.org/custom-status').valueCode"
}

Usage:

Terminal window
GET /fhir/Observation?custom-status=reviewed
GET /fhir/Condition?custom-status=reviewed
GET /fhir/Procedure?custom-status=reviewed

Search by custom references:

{
"resourceType": "SearchParameter",
"url": "http://example.org/SearchParameter/observation-custom-performer",
"code": "custom-performer",
"type": "reference",
"base": ["Observation"],
"target": ["Practitioner", "Organization"],
"expression": "Observation.extension.where(url='http://example.org/custom-performer').valueReference"
}

Usage:

Terminal window
GET /fhir/Observation?custom-performer=Practitioner/12345

Update a SearchParameter to modify its behavior:

Terminal window
PUT /fhir/SearchParameter/12345
{
"resourceType": "SearchParameter",
"id": "12345",
"url": "http://example.org/SearchParameter/patient-custom-field",
"code": "custom-field",
"type": "string",
"base": ["Patient"],
"expression": "Patient.extension.where(url='http://example.org/updated-field').valueString",
"modifier": ["exact", "contains"] # Added modifiers
}

The updated parameter is immediately available with new behavior.

Delete a SearchParameter when no longer needed:

Terminal window
DELETE /fhir/SearchParameter/12345

After deletion:

  • The parameter is removed from the search registry
  • Queries using this parameter will fail with “unknown search parameter” error
  • Existing data is unchanged (only the search capability is removed)

Lock-Free Reads:

  • Search queries never block on registry access
  • A writer publishes a new snapshot; readers keep the one they hold

Incremental Updates:

  • SearchParameter CRUD operations update registry incrementally
  • No full registry reload needed

Query Cache:

  • Cleared when parameters change

With Redis enabled ([redis] enabled = true), a SearchParameter created on one instance reaches the others through Redis pub/sub. Each instance applies it to its registry.

Without Redis there is no such sync. Other instances pick up the parameter at their next start.

  1. Use Canonical URLs - Follow URI naming conventions (e.g., http://your-org.org/SearchParameter/name)
  2. Test FHIRPath Expressions - Validate expressions work correctly before deployment
  3. Document Custom Parameters - Add clear descriptions and examples
  4. Version Your Parameters - Include version in URL when making breaking changes
  • No Semantic Validation - FHIRPath expressions are checked for syntax, not semantics
  • No Runtime Type Checking - Expression type mismatches fail gracefully at query time
  • Extension-Only - Custom parameters typically target extensions (core FHIR fields already have parameters)

Parameter not found:

Terminal window
GET /fhir/Patient?my-param=value
# Error: Unknown search parameter 'my-param'
  • Verify SearchParameter was created successfully
  • Check the code field matches the query parameter name
  • Ensure base includes the resource type you’re querying

No results with valid parameter:

Terminal window
GET /fhir/Patient?custom-field=value
# Returns empty Bundle
  • Verify the FHIRPath expression matches your data structure
  • Check extension URLs match exactly (case-sensitive)
  • Test the expression against sample resources

Invalid expression error:

Terminal window
POST /fhir/SearchParameter
# Error: Invalid FHIRPath expression
  • Review FHIRPath syntax
  • Use parentheses for complex expressions
  • Test expression with FHIRPath evaluator tools