Union

Union( group, ... )
Union( group, ... )
Union( group, ... )
Union( group, ... )
union( group, ... )
Union( 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.

client.Query(
  Paginate(
    Union(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))));
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" },
    { "@ref": "classes/spells/181388642088911360" }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "union": [
              {
                "match": { "index": "spells_by_element" },
                "terms": "fire"
              },
              {
                "match": { "index": "spells_by_element" },
                "terms": "water"
              }
            ]
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      { "@ref": "classes/spells/181388642046968320" },
      { "@ref": "classes/spells/181388642071085568" },
      { "@ref": "classes/spells/181388642088911360" }
    ]
  }
}
result, _ := client.Query(
    f.Paginate(
        f.Union(
            f.MatchTerm(f.Index("spells_by_element"), "fire"),
            f.MatchTerm(f.Index("spells_by_element"), "water"),
        ),
    ),
)

fmt.Println(result)
map[data:[{181388642046968320 0xc42028d3c0 <nil>} {181388642071085568 0xc42028d5e0 <nil>} {181388642088911360 0xc42028d820 <nil>}]]
System.out.println(
      client.query(
        Paginate(
          Union(
            Match(Index(Value("spells_by_element")), Value("fire")),
            Match(Index(Value("spells_by_element")), Value("water"))))
      ).get());
{
  data: [
    ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))),
    ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))),
    ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections")))
  ]
}
client.query(
  q.Paginate(
    q.Union(
      q.Match(q.Index('spells_by_element'), 'fire'),
      q.Match(q.Index('spells_by_element'), 'water'),
    )
  )
)
.then((ret) => console.log(ret))
{ data:
   [ Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))),
     Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))),
     Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))) ] }
client.query(
  q.paginate(
    q.union(
      q.match(q.index("spells_by_element"), "fire"),
      q.match(q.index("spells_by_element"), "water")
    )
  ))
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" },
    { "@ref": "classes/spells/181388642088911360" }
  ]
}
client.query(
  Paginate(
    Union(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))))
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" },
    { "@ref": "classes/spells/181388642088911360" }
  ]
}

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

client.Query(
  Paginate(
    Union(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water")),
    events: true));
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" }
    }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "union": [
              {
                "match": { "index": "spells_by_element" },
                "terms": "fire"
              },
              {
                "match": { "index": "spells_by_element" },
                "terms": "water"
              }
            ]
          },
          "events": true
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      {
        "ts": 1509244539203043,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642046968320" }
      },
      {
        "ts": 1509244539223511,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642071085568" }
      },
      {
        "ts": 1509244539235128,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642088911360" }
      }
    ]
  }
}
result, _ := client.Query(
    f.Paginate(
        f.Events(
            f.Union(
                f.MatchTerm(f.Index("spells_by_element"), "fire"),
                f.MatchTerm(f.Index("spells_by_element"), "water"),
            ),
        ),
    ),
)

fmt.Println(result)
map[data:[
  map[ts:1509244539203043 action:add document:{181388642046968320 0xc4202daf40 <nil>}]
  map[ts:1509244539223511 action:add document:{181388642071085568 0xc4202db200 <nil>}]
  map[ts:1509244539235128 action:add document:{181388642088911360 0xc4202db4e0 <nil>}]
]]
System.out.println(
        client.query(
          Paginate(
            Events(
              Union(
                Match(Index(Value("spells_by_element")), Value("fire")),
                Match(Index(Value("spells_by_element")), Value("water"))
              )
            )
          )
        ).get());
{
  data: [
    {
      ts: 1536673654653856, 
      action: "add", 
      document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))
    },
    {
      ts: 1536673654738429, 
      action: "add", 
      document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections")))
    },
    {
      ts: 1536673654755090, 
      action: "add", 
      document: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections")))
    }
  ]
}
client.query(
  q.Paginate(
    q.Events(
      q.Union(
        q.Match(q.Index('spells_by_element'), 'fire'),
        q.Match(q.Index('spells_by_element'), 'water'),
      )
    )
  )
)
.then((ret) => console.log(ret))
{ data:
   [ { ts: 1526677776479051,
       action: 'add',
       document:
        Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))) },
     { ts: 1526677776479051,
       action: 'add',
       document:
        Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) },
     { ts: 1527095201753208,
       action: 'add',
       document:
        Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))) } ] }
client.query(
  q.paginate(
    q.union(
      q.match(q.index("spells_by_element"), "fire"),
      q.match(q.index("spells_by_element"), "water")
    ),
    events=True
  ))
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" }
    }
  ]
}
client.query(
  Paginate(
    Union(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water")),
    events = true))
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" }
    }
  ]
}

The following query demonstrates how various arrays are evaluated:

Value result = await client.Query(
  Arr(
    Union(Arr("A", "B"), Arr("C", "D")),
    Union(Arr("A", "B"), Arr("B", "C")),
    Union(Arr("A", "B", "C"), Arr("B", "C"), Arr("B", "C", "D")),
    Union(Arr("A", "B", "C"), Arr("B", "B"), Arr("B"))
  )
);
[
  [
    "A",
    "B",
    "C",
    "D"
  ],
  [
    "A",
    "B",
    "C"
  ],
  [
    "A",
    "B",
    "C",
    "D"
  ],
  [
    "A",
    "B",
    "B",
    "C"
  ]
]
Not available in this language yet.
result, err := client.Query(
  f.Arr{
    f.Union(f.Arr{"A", "B"}, f.Arr{"C", "D"}),
    f.Union(f.Arr{"A", "B"}, f.Arr{"B", "C"}),
    f.Union(f.Arr{"A", "B", "C"}, f.Arr{"B", "C"}, f.Arr{"B", "C", "D"}),
    f.Union(f.Arr{"A", "B", "C"}, f.Arr{"B", "B"}, f.Arr{"B"})})
fmt.Println(result)
[[A B C D] [A B C] [A B C D] [A B B C]]
System.out.println(
    client.query(
        Arr(
            Union(
                Arr(Value("A"), Value("B")),
                Arr(Value("C"), Value("D"))
            ),
            Union(
                Arr(Value("A"), Value("B")),
                Arr(Value("B"), Value("C"))
            ),
            Union(
                Arr(Value("A"), Value("B"), Value("C")),
                Arr(Value("B"), Value("C")),
                Arr(Value("B"), Value("C"), Value("D"))
            ),
            Union(
                Arr(Value("A"), Value("B"), Value("C")),
                Arr(Value("B"), Value("B")),
                Arr(Value("B"))
            )
        )
    ).get());
[["A", "B", "C", "D"], ["A", "B", "C"], ["A", "B", "C", "D"], ["A", "B", "B", "C"]]
client.query([
  q.Union(['A', 'B'], ['C', 'D']),
  q.Union(['A', 'B'], ['B', 'C']),
  q.Union(['A', 'B', 'C'], ['B', 'C'], ['B', 'C', 'D']),
  q.Union(['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' ] ]
print(client.query(
  [
    q.union(["A", "B"], ["C", "D"]),
    q.union(["A", "B"], ["B", "C"]),
    q.union(["A", "B", "C"], ["B", "C"], ["B", "C", "D"]),
    q.union(["A", "B", "C"], ["B", "B"], ["B"]),
  ]
))
[['A', 'B', 'C', 'D'], ['A', 'B', 'C'], ['A', 'B', 'C', 'D'], ['A', 'B', 'B', 'C']]
println(Await.result(
  client.query(
    Arr(
      Union(Arr("A", "B"), Arr("C", "D")),
      Union(Arr("A", "B"), Arr("B", "C")),
      Union(Arr("A", "B", "C"), Arr("B", "C"), Arr("B", "C", "D")),
      Union(Arr("A", "B", "C"), Arr("B", "B"), Arr("B"))
    )
  ),
  5.seconds
))
[["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!