Union

Copied!
( group, ... )

Description

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

Parameters

Parameter 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 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 a Page.

Copied!
client.query(
  q.(
    q.(
      q.(q.('spells_by_element'), 'fire'),
      q.(q.('spells_by_element'), 'water'),
    )
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  data: [
    (("spells"), "181388642046968320"),
    (("spells"), "181388642071085568"),
    (("spells"), "181388642088911360")
  ]
}
Query metrics:
  •    bytesIn: 135

  •   bytesOut: 377

  • computeOps:   1

  •    readOps:   2

  •   writeOps:   0

  •  readBytes: 211

  • writeBytes:   0

  •  queryTime: 7ms

  •    retries:   0

The following query 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))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  data: [
    {
      ts: 1592112265440000,
      action: 'add',
      document: (("spells"), "181388642046968320")
    },
    {
      ts: 1592112265440000,
      action: 'add',
      document: (("spells"), "181388642071085568")
    },
    {
      ts: 1592112265440000,
      action: 'add',
      document: (("spells"), "181388642088911360")
    }
  ]
}
Query metrics:
  •    bytesIn: 146

  •   bytesOut: 527

  • computeOps:   1

  •    readOps:   2

  •   writeOps:   0

  •  readBytes: 211

  • writeBytes:   0

  •  queryTime: 8ms

  •    retries:   0

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))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ [ 'A', 'B', 'C', 'D' ],
  [ 'A', 'B', 'C' ],
  [ 'A', 'B', 'C', 'D' ],
  [ 'A', 'B', 'B', 'C' ] ]
Query metrics:
  •    bytesIn: 157

  •   bytesOut:  82

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 4ms

  •    retries:   0

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!