Union

Copied!
( group, ... )

Description

The Union function combines the results of one or more groups, which can be Arrays or Set References.

Parameters

Argument Type Definition and Requirements

group

One or more arrays or set references which should have their results OR’d together. All provided group items must be of the same type.

Returns

When group is an array, an array of the items that appear in any provided group.

When group is a set reference, a set reference of the items that appear in any provided group.

Examples

The following query combines the Set Reference (i.e. set reference) returned by locating the search term "fire" in the index named "spells_by_element" and the Set Reference returned by locating the search term "water" in the Index named "spells_by_element". The Paginate function materialized the results of the Union operation into an array of type Page.

Copied!
client.query(
  q.(
    q.(
      q.(q.('spells_by_element'), 'fire'),
      q.(q.('spells_by_element'), 'water'),
    )
  )
)
.then((ret) => console.log(ret))
{ data:
   [ (id=181388642046968320, collection=(id=spells, collection=(id=collections))),
     (id=181388642071085568, collection=(id=spells, collection=(id=collections))),
     (id=181388642088911360, collection=(id=spells, collection=(id=collections))) ] }

The query below is similar to the example above, but it returns document events instead of the index tuples.

Copied!
client.query(
  q.(
    q.(
      q.(
        q.(q.('spells_by_element'), 'fire'),
        q.(q.('spells_by_element'), 'water'),
      )
    )
  )
)
.then((ret) => console.log(ret))
{ data:
   [ { ts: 1526677776479051,
       action: 'add',
       document:
        (id=181388642046968320, collection=(id=spells, collection=(id=collections))) },
     { ts: 1526677776479051,
       action: 'add',
       document:
        (id=181388642071085568, collection=(id=spells, collection=(id=collections))) },
     { ts: 1527095201753208,
       action: 'add',
       document:
        (id=181388642088911360, collection=(id=spells, collection=(id=collections))) } ] }

The following query demonstrates how various arrays are evaluated:

Copied!
client.query([
  q.(['A', 'B'], ['C', 'D']),
  q.(['A', 'B'], ['B', 'C']),
  q.(['A', 'B', 'C'], ['B', 'C'], ['B', 'C', 'D']),
  q.(['A', 'B', 'C'], ['B', 'B'], ['B']),
])
.then((ret) => console.log(ret))
[ [ 'A', 'B', 'C', 'D' ],
  [ 'A', 'B', 'C' ],
  [ 'A', 'B', 'C', 'D' ],
  [ 'A', 'B', 'B', 'C' ] ]

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!