Find Set Intersection Within an Array
Find set intersection within a single flat array using the array:intersection()
function
Query
array:intersection("mailto[]", as=unique_mails)
Introduction
Set intersection refers to finding the common elements within array. The result of the intersection operation is a new dataset containing only the elements that are common to all the input sets (similar to a join operation).
In this example, the
array:intersection()
function will return
the unique email addresses from an array. The intersection of
the array is based on the intersection of unique values within
each element.
Example incoming data might look like this:
mailto[0]=foo@example.com
mailto[1]=bar@example.com
mailto[2]=bar@example.com
Step-by-Step
Starting with the source repository events.
- flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0[[Array Manipulation]] result{{Result Set}} repo --> 0 0 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;logscale
array:intersection("mailto[]", as=unique_mails)
Returns the intersection of element values in the array mailto[], in this case email addresses, storing the result in a new intersection set array unique_mails and stores them as unique values.
Event Result set.
Summary and Results
The result of the intersection operation is a new dataset that consists of all the common elements occurring in an array. The query is used to simplify data in an array for any common values and make a new array with only the unique values in it. This can be useful when processing a set of values and looking for the unique list, for example to use as labels within a graph, or as input parameters to a filter.
Sample output from the incoming example data:
unique_mails[0]=foo@example.com
unique_mails[1]=bar@example.com