View Source HL7.Query (elixir_hl7 v0.9.3)
Queries and modifies HL7 messages using Field and Segment Grammar Notations with a pipeline-friendly API and set-based operations.
Similar to libraries such as jQuery and D3, HL7.Query
is designed around the concept of selecting and sub-selecting
elements (segments or segment groups) in an HL7 message. The full message context is retained in the HL7.Query
struct so that messages can be modified piecemeal and then reconstructed as strings.
In general, use HL7.Query.select/2
with a segment selector (similar to CSS selectors) to select lists of segment groups.
The segment selector is written as a string of ordered segment names. Curly braces surround repeating elements. Square brackets enclose optional elements. These can be nested to create selectors that can select complex groups of segments or validate entire HL7 message layouts.
For example, an ORU_R01 HL7 message's Order Group selector could be written as:
"[ORC] OBR {[NTE]} {[OBX {[NTE]}]}"
.
Note that this would look for OBRs, optionally preceded by an ORC, possibly followed by one or more NTEs, maybe followed again by one or more OBRs with their own optional NTE sets.
To reference data within segments, there is a field selector format that can access fields, repetitions, components and sub-components across one or more segments. All indices start at one, and the repetition index defaults to one unless specified within brackets after the field number.
Field Selector | Description |
---|---|
"PID-11[2].4" | PID segments, 11th field, 2nd repetition, 4th component |
"OBX-2.2.1" | OBX segments, 2nd field, 2nd component, 1st sub-component |
"1" | All segments, 1st field |
"3[4].2" | All segments, 3rd field, 4th repetition, 2nd component |
Summary
Functions
Appends a segment or segments to the currently selected segments in each selection.
Returns the current selection count.
Associates data with each selection. This data remains accessible to child selections.
Deletes all selections in the query document.
Filters (deletes) segments within selections by whitelisting one or more segment types.
Accepts either a segment name, list of acceptable names, or a function that takes an HL7.Query
(containing one sub-selected segment at a time) and returns a boolean filter value.
Returns a flattened list of segment parts from selected segments across all selections using
the given field_selector
.
Returns a segment part from the first selected segment (of the given name, if specified)
across all selections using the given field_selector
.
Returns a list containing an associated data map for each selection in the given HL7.Query
.
Returns the associated key-value of the first (or only) selection for the given HL7.Query
.
Returns the selection index of the first (or only) selection for the given HL7.Query
.
Returns a list containing a list of selected segments for each selection.
Returns a flattened list of segment names across all selections.
Returns a flattened list of selected segments across all selections.
Maps across each selection with a fun
that accepts an HL7.Query
(handling one selection at a time) and returns a results list.
Creates an HL7.Query
struct that selects an entire HL7 Message. This step is implicitly carried out by most other
functions in this module. As it involves parsing an HL7 message, one should
cache this result if invoked repeatedly.
Updates the set numbers in each segment's first field to be their respective selection indices.
Prepends a segment or list of segments to the segments in each of the current selections.
Rejects (deletes) segments within selections by blacklisting one or more segment types.
Accepts either a segment name, list of acceptable names, or a function that takes an HL7.Query
(containing one sub-selected segment at a time) and returns a boolean reject value.
Rejects (deletes) Z-segments in all selections.
Replaces all segments in each selection. The given fun
should
accept an HL7.Query
(referencing a single selection) and return a list of replacement segments
(in parsed list format).
Selects the entire an HL7.Query
into an HL7.Message
.
Selects or sub-selects segment groups in an HL7 message using a segment selector or filter function.
Checks the format of an hl7 path at compile time and returns an Path
Outputs an ANSI representation of an HL7.Query
with corresponding selection indices on the left.
Converts an HL7.Query
into an HL7.Message
.
Updates segment parts of all selected segments, iterating through each selection.
A replacement function accepts an HL7.Query
containing one selection with its
part
property set to the value found using the field_selector
.
Types
@type content_hl7() :: raw_hl7() | parsed_hl7()
@type content_or_query_hl7() :: content_hl7() | t()
@type parsed_hl7() :: [list()] | HL7.Message.t()
@type parsed_or_query_hl7() :: parsed_hl7() | t()
@type raw_hl7() :: String.t() | HL7.RawMessage.t()
Functions
Appends a segment or segments to the currently selected segments in each selection.
@spec count(t()) :: non_neg_integer()
Returns the current selection count.
Associates data with each selection. This data remains accessible to child selections.
Each selection stores a map of data. The supplied fun
should
accept an HL7.Query
and return a map of key-values to be merged into the
existing map of data.
Selections automatically include the index of the current selection, i.e., %{index: 5}
.
For HL7 numbering, the index values begin at 1.
Examples
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> select("ORC RXA RXR {[OBX]}")
...> |> data(fn q -> %{order_num: find_first(q, ~p"ORC-3.1")} end)
...> |> select("OBX")
...> |> update(~p"6", fn q -> get_datum(q, :order_num) end)
...> |> root()
...> |> find_first(~p"OBX-6")
"IZ-783278"
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> select("ORC RXA RXR {[OBX]}")
...> |> data(fn q -> %{group_num: get_index(q)} end)
...> |> select("OBX")
...> |> update(~p"6", fn q -> get_datum(q, :group_num) end)
...> |> root()
...> |> find_all(~p"OBX-6")
[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
Deletes all selections in the query document.
@spec filter(content_or_query_hl7(), (t() -> as_boolean(term()))) :: t()
@spec filter(content_or_query_hl7(), binary()) :: t()
@spec filter(content_or_query_hl7(), [binary()]) :: t()
Filters (deletes) segments within selections by whitelisting one or more segment types.
Accepts either a segment name, list of acceptable names, or a function that takes an HL7.Query
(containing one sub-selected segment at a time) and returns a boolean filter value.
Examples
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> filter(["MSH", "PID", "ORC"])
...> |> root()
...> |> get_segment_names()
["MSH", "PID", "ORC", "ORC", "ORC"]
@spec find_all(content_or_query_hl7(), HL7.Path.t()) :: list()
Returns a flattened list of segment parts from selected segments across all selections using
the given field_selector
.
PID-3[2].1.2
PID segments, field 3, repetition 2, component 1, subcomponent 2
OBX-5
OBX segments, field 5
2.3
All segments, field 2, component 3
@spec find_first(content_or_query_hl7(), HL7.Path.t()) :: nil | iodata()
Returns a segment part from the first selected segment (of the given name, if specified)
across all selections using the given field_selector
.
PID-3[2].1.2
PID segments, field 3, repetition 2, component 1, subcomponent 2
OBX-5
OBX segments, field 5
2.3
All segments, field 2, component 3
Returns a list containing an associated data map for each selection in the given HL7.Query
.
Returns the associated key-value of the first (or only) selection for the given HL7.Query
.
@spec get_index(t()) :: non_neg_integer()
Returns the selection index of the first (or only) selection for the given HL7.Query
.
Examples
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> select("ORC RXA RXR {[OBX]}")
...> |> map(fn q -> get_index(q) end)
[1, 2]
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> select("ORC RXA RXR {[OBX]}")
...> |> select("OBX")
...> |> map(fn q -> get_index(q) end)
[1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> select("OBX")
...> |> map(fn q -> get_index(q) end)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
@spec get_part(content_or_query_hl7(), String.t()) :: nil | iodata()
@spec get_parts(content_or_query_hl7(), String.t()) :: list()
Returns a list containing a list of selected segments for each selection.
@spec get_segment_names(content_or_query_hl7()) :: [String.t()]
Returns a flattened list of segment names across all selections.
Returns a flattened list of selected segments across all selections.
Maps across each selection with a fun
that accepts an HL7.Query
(handling one selection at a time) and returns a results list.
Examples
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> select("RXA")
...> |> map(fn q -> find_first(q, ~p"5.2") end)
["Influenza", "PCV 13", "DTaP-Hep B-IPV"]
@spec new(content_or_query_hl7()) :: t()
Creates an HL7.Query
struct that selects an entire HL7 Message. This step is implicitly carried out by most other
functions in this module. As it involves parsing an HL7 message, one should
cache this result if invoked repeatedly.
Updates the set numbers in each segment's first field to be their respective selection indices.
Prepends a segment or list of segments to the segments in each of the current selections.
@spec reject(t(), binary()) :: t()
@spec reject(content_or_query_hl7(), [binary()]) :: t()
@spec reject(content_or_query_hl7(), (t() -> as_boolean(term()))) :: t()
Rejects (deletes) segments within selections by blacklisting one or more segment types.
Accepts either a segment name, list of acceptable names, or a function that takes an HL7.Query
(containing one sub-selected segment at a time) and returns a boolean reject value.
Examples
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> reject(["OBX", "RXA", "RXR"])
...> |> root()
...> |> get_segment_names()
["MSH", "PID", "ORC", "ORC", "ORC"]
Rejects (deletes) Z-segments in all selections.
@spec replace(t(), (t() -> [HL7.Segment.segment_hl7()])) :: t()
Replaces all segments in each selection. The given fun
should
accept an HL7.Query
(referencing a single selection) and return a list of replacement segments
(in parsed list format).
Selects the entire an HL7.Query
into an HL7.Message
.
@spec select(content_or_query_hl7(), binary()) :: t()
Selects or sub-selects segment groups in an HL7 message using a segment selector or filter function.
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> select("OBX")
...> |> select(fn q -> find_first(q, ~p"1") != "1" end)
...> |> delete()
...> |> root()
...> |> get_segment_names()
["MSH", "PID", "ORC", "RXA", "RXR", "OBX", "ORC", "RXA", "ORC", "RXA", "RXR", "OBX"]
Checks the format of an hl7 path at compile time and returns an Path
Outputs an ANSI representation of an HL7.Query
with corresponding selection indices on the left.
@spec to_message(t()) :: HL7.Message.t()
Converts an HL7.Query
into an HL7.Message
.
@spec update( content_or_query_hl7(), HL7.Path.t(), (t() -> String.t()) | String.t() | list() ) :: t()
Updates segment parts of all selected segments, iterating through each selection.
A replacement function accepts an HL7.Query
containing one selection with its
part
property set to the value found using the field_selector
.
Examples
iex> import HL7.Query
iex> HL7.Examples.nist_immunization_hl7()
...> |> update(~p"OBX-3.2", fn q -> "TEST: " <> q.part end)
...> |> find_all(~p"OBX-3.2")
...> |> List.first()
"TEST: Vaccine funding program eligibility category"
iex> import HL7.Query
iex> HL7.Examples.wikipedia_sample_hl7()
...> |> select("PID")
...> |> update(~p"5.2", "UNKNOWN")
...> |> find_first(~p"PID-5.2")
"UNKNOWN"