Couchbase is a JSON database that has now become an alternative to the traditional RDBMS. It has achieved this, not only by providing the industry leading Key Value store and the same query and ACID translation capabilities that organizations have come to expect, but also the flexibility of natively stored its data in JSON format. In addition, Couchbase also includes distributed storage, multi dimensional scalability, high availability, and cross data center replication(XDCR). All this in a single Couchbase Data Platform that extends to the Edge with the Couchbase Mobile.
Couchbase is also committed to extend its capabilities well beyond that are needed in traditional RDBMS applications. With Couchbase Cloud, where everything is a fully managed and automated, providing a secure Database-as-a-Service (DBaaS) that simplifies database management by deploying, managing, and operating Couchbase Server across multi-cloud environments with just a few clicks. Couchbase Cloud is flexible, highly available, and provides a differentiated economical option for enterprises looking to reduce operational tasks while taking advantage of the most powerful NoSQL technology.
We are also doing this by collaborating with many of our customers across different industries to understand their business needs and how Couchbase can help with those challenges. These industries extend from Retail & E-Commerce, Travel & Hospitality, Financial Services, Energy & Utilities, and also High Tech and Gaming.
In this blog, I’d like to focus on how the Couchbase database can be used in the Healthcare industry. Specifically, the focus will be on the Fast Healthcare Interoperability Resources (FHIR) specification as defined by HL7 FHIR. The reason for this blog is because of the level of interest from many of our customers who are currently planning or implementing their FHIR compliant applications using the Couchbase database.
Why is FHIR with Couchbase of interest to you?
- Your organization is planning to develop FHIR applications, and/or looking into taking advantage of the many benefits of NoSQL database, such as distributed, high availability, XDCR, and multi-dimensional scalability.
- You are an architect, or developer and like understand how Couchbase JSON database can greatly reduce the complexity of your applications, by relegating the complex FHIR Search [https://www.hl7.org/fhir/searchparameter-registry.html] processing to the database server layer.
- You are a data analyst and like to understand how you can leverage your SQL knowledge to query FHIR data directly with Couchbase N1QL and Full-Text-Search.
- You are interested in finding out how Couchbase N1QL, a SQL for JSON, can provide an efficient way to query a data model, as defined by FHIR, that is both JSON hierarchical and relational in nature.
A brief description of FHIR
FHIR® – Fast Healthcare Interoperability Resources (hl7.org/fhir) – is a next generation standards framework created by HL7. FHIR combines the best features for HL7 and leverages the latest web standards and applying a tight focus on implementability. FHIR applications are based on the concept of “Resources”. These resources are assembled into specific healthcare applications, spanning from the administrative, clinical to patient care functions. FHIR is suitable for use in a wide variety of contexts – mobile phone apps, cloud communications, EHR-based data sharing, server communication for healthcare providers. One of the key objectives of FHIR is to specify a standard in which different healthcare applications can adopt, thus allowing interoperability between different functional applications and ultimately drive down the cost of implementations.
Please refer to the FHIR website for further information https://www.hl7.org/fhir/overview.html.
The FHIR data model
At the heart of FHIR is the common definition of the different resource types that encompasses all the different aspects of healthcare. From the administrative function of Patient, Practitioner, CareTeam, Device, Organization, Location, Healthcare Service, to the Clinical, Diagnostic, Medications, as well as the Financial functions such as Insurance Coverage.
For the purpose of this article, I am using the sample synthetic data set available from Synthetic Mass that has been generated by SyntheaTM [https://synthetichealth.github.io/synthea/)], an open-source patient population simulation made available by The MITRE Corporation.
The data was made available as a resource bundle for each synthetic patient, which are then imported into an instance of Couchbase 6.5 database.
SELECT resourceType, COUNT(1) doc_count
WHERE resourceType IS NOT MISSING
GROUP BY resourceType
A few notable points about the FHIR data model
- The model centers around the ‘patient’ object
- The object type is represented by the ‘resourceType’ field
- The JSON structure supports a richer set of information that can be kept about the patient, such as contact details, addresses, communications, etc..
- Each object has a uniquely identifiable ‘id’ field.
- The objects are referencing each other. Most are have references to the ‘patient’ object.
Why Couchbase is well suited for FHIR
As mentioned above, Couchbase stores its data natively in JSON, which is a readable document format. Applications can either use the Key Value APIs, or leverage Couchbase N1QL Query Language to manipulate the documents. The advantage of storing the data in the JSON format, instead of as a column type as in many RDBMS, is the ease with which users can access the information therein. JSON document elements, such as field, array object, and hierarchical object can be accessed directly from the parent object without requiring additional syntax. Here is an example of the Patient document as defined by the FHIR.
What it means for the FHIR application is that users with RDBMS experience who have a basic working knowledge of SQL can access the FHIR patient and the associated resources in the exact same way as with the traditional RDBMS.
Couchbase N1QL to search FHIR data
Query patients by ID
FROM fhir_admin res
Query patients by age group
FROM fhir_admin p
agegroup = CASE WHEN age <= 4 THEN 'A - 0-4'
WHEN age <= 17 THEN 'B - 5-17'
WHEN age <= 49 THEN 'C - 18-49'
WHEN age <= 64 THEN 'D - 50-64'
ELSE 'E - > 65' END
GROUP BY agegroup
- The patient’s age has to be calculated from taking the current date – DOB.
- The query uses the N1QL LET construct to define ‘age’ and bucket the age into ‘agegroup’.
- A standard ANSI GROUP BY and COUNT returns the number of patients by age group.
In addition, the ability to JOIN and UNNEST documents with Couchbase N1QL allows users to query related information of the patient.
Query patients and their current unresolved conditions
SELECT ARRAY v FOR v IN p.name WHEN v.`use`='official' END fullname ,
FROM fhir_admin c
INNER JOIN fhir_admin p ON split(c.subject.reference,':') = p.id AND p.resourceType='Patient'
UNNEST c.code.coding as cc
AND EVERY s in c.clinical.status SATISFIES s.code != 'resolved' END
- A patient can have multiple names, which is represented in the ‘name’ array. This query uses the ARRAY operator to filter out only the official ‘name’ array element.
- The query joins the ‘condition’ to the ‘patient’ object using part the ‘subject.reference’ field of the ‘condition’ object to the ‘patient.id’.
- Each ‘patient’ may have multiple conditions, hence the need to flatten (UNNEST) the ‘condition->code’ array.
- There could be multiple ‘clinical.status’ for the ‘condition’, but the query only needs to return the conditions that are not resolved
List of all patients who was at Beverley hospital on the week beginning of 2011-08-18
SELECT ARRAY v FOR v IN p.name WHEN v.`use`='official' END fullname ,
FROM fhir_admin m
INNER JOIN fhir_admin p ON split(m.subject.reference,':') = p.id AND p.resourceType='Patient'
AND DATE_FORMAT_STR(m.period.`start`, '1111-11-11') BETWEEN '2011-08-18' AND '2011-08-25'
AND m.serviceProvider.display = 'BEVERLY HOSPITAL CORPORATION'
- The query examines the ‘encounter’ object to find out all the ‘patient’ treatment at this specific hospital.
- ARRAY operator is used to get the ‘patient’ official name.
List of patients with their insurance claim detail – services and cost
SELECT c.patient.reference, cis.display,SUM( c.total.`value`)
FROM fhir_admin c
INNER JOIN fhir_admin p ON split(c.patient.reference,':') = p.id AND p.resourceType='Patient'
UNNEST c.item as ci
UNNEST ci.productOrService.coding as cis
GROUP by cis.display,c.patient.reference
- The ‘claim’ object is JOINED to the ‘patient’ object to access the patient’s detail.
- The ‘claim’ object has nested array for ‘claim->item’.
- The ‘claim->item’ is associated to a ‘productOrService’ with an array coding that contain the description of the treatment.
- The cost is at the claim level.
Rank the Procedure count by Hospital
SELECT e.serviceProvider.display hospital,
RANK() OVER ( PARTITION BY e.serviceProvider.display ORDER BY COUNT(p.code.text) DESC ) serviceRank
FROM fhir_admin p
INNER JOIN fhir_admin e USE HASH(PROBE) ON split(p.encounter.reference,':') = e.id and e.resourceType='Encounter'
GROUP by e.serviceProvider.display,p.code.text
- The ‘procedure’ object is JOINED to the ‘encounter’ object to access the hospital where the procedure was administered.
- The N1QL Window RANK function is used to assign a value by the count of the procedure.
- Hash Join hint is used to improve the performance for the document JOIN.
Couchbase is well suited for the FHIR application implementation because of the approach it uses to store the data in JSON format. Its powerful N1QL query language allows developers with SQL experience to navigate the FHIR data model to perform all the CRUD operations that users would expect to see in a database. With its ability to perform document JOIN to advanced processing of embedded array objects, the N1QL query language relegates all the processing needs to the Couchbase Query and Indexing Service, thus reducing the burden of the FHIR applications to having to perform all the object associations.
As with any new applications, there will always be a need to perform direct examinations of the underlying data, either by the application administrators or the data analysts who may have the need to perform searches beyond what are supported by the FHIR search capability with FHIR REST API server.
In the next blog, I will examine what it takes to set up a FHIR REST API server that can communicate with the Couchbase server to provide the basic FHIR search requirements as specified in https://www.hl7.org/fhir/search.html
- Fast Healthcare Interoperability Resource: https://www.hl7.org/fhir/index.html
- SyntheticMass: “Jason Walonoski, Mark Kramer, Joseph Nichols, Andre Quina, Chris Moesel, Dylan Hall, Carlton Duffett, Kudakwashe Dube, Thomas Gallagher, Scott McLachlan, Synthea: An approach, method, and software mechanism for generating synthetic patients and the synthetic electronic health care record, Journal of the American Medical Informatics Association, Volume 25, Issue 3, March 2018, Pages 230–238, https://doi.org/10.1093/jamia/ocx079“
- N1QL Tutorial: https://query-tutorial.couchbase.com/tutorial/#1
- FHIR Search Parameters: https://www.hl7.org/fhir/searchparameter-registry.html