Intersection

Intersection( group, ... )
Intersection( group, ... )
Intersection( group, ... )
Intersection( group, ... )
intersection( group, ... )
Intersection( group, ... )

Description

The Intersection function compares all of the items in each group, which can be an Array or SetRef, and returns the elements that appear in every provided group.

Parameters

Argument Type Definition and Requirements

group

Array or SetRef

One or more arrays or setrefs to be intersected. All provided group items must be of the same type.

Returns

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

When group is a setref, a setref of the items that appear in every provided group.

Examples

The following query intersects the SetRef (i.e. set reference) returned by locating the search term "fire" in the index named "spells_by_element" with the SetRef returned by locating the search term "water" in the Index named "spells_by_element". The Paginate function executes the SetRef and returns the results of the Intersect operation in an array of type Page.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "intersection": [
              {
                "match": { "index": "spells_by_element" },
                "terms": "fire"
              },
              {
                "match": { "index": "spells_by_element" },
                "terms": "water"
              }
            ]
          }
        }'
client.Query(
  Paginate(
    Intersection(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))));
System.out.println(
   client.query(
     Paginate(
       Intersection(
         Match(Index(Value("spells_by_element")), Value("fire")),
         Match(Index(Value("spells_by_element")), Value("water"))
       )
     )
   ).get());
result, _ := client.Query(
    f.Paginate(
        f.Intersection(
            f.MatchTerm(f.Index("spells_by_element"), "fire"),
            f.MatchTerm(f.Index("spells_by_element"), "water"),
        ),
    ),
)

fmt.Println(result)
client.query(
  Paginate(
    Intersection(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))))
client.query(
  q.paginate(
    q.intersection(
      q.match(q.index("spells_by_element"), "fire"),
      q.match(q.index("spells_by_element"), "water")
    )
  ))
client.query(
  q.Paginate(
    q.Intersection(
      q.Match(q.Index('spells_by_element'), 'fire'),
      q.Match(q.Index('spells_by_element'), 'water'),
    )
  )
)
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
  "resource": {
    "data": [ { "@ref": "classes/spells/181388642071085568" } ]
  }
}
{ "data": [ { "@ref": "classes/spells/181388642071085568" } ] }
{
  data: [
    ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))
  ]
}
map[data:[{181388642071085568 0xc4202920a0 <nil>}]]
{ "data": [ { "@ref": "classes/spells/181388642071085568" } ] }
{ "data": [ { "@ref": "classes/spells/181388642071085568" } ] }
{ data: [ Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) ] }

The following query demonstrates how various arrays are evaluated:

Value result = await client.Query(
  Arr(
    Intersection(Arr("A", "B"), Arr("C", "D")),
    Intersection(Arr("A", "B"), Arr("B", "C")),
    Intersection(Arr("A", "B", "C"), Arr("B", "C"), Arr("B", "C", "D")),
    Intersection(Arr("A", "B", "C"), Arr("B", "B"), Arr("B"))
  )
);
result, err := client.Query(
  f.Arr{
    f.Intersection(f.Arr{"A", "B"}, f.Arr{"C", "D"}),
    f.Intersection(f.Arr{"A", "B"}, f.Arr{"B", "C"}),
    f.Intersection(f.Arr{"A", "B", "C"}, f.Arr{"B", "C"}, f.Arr{"B", "C", "D"}),
    f.Intersection(f.Arr{"A", "B", "C"}, f.Arr{"B", "B"}, f.Arr{"B"})})
fmt.Println(result)
System.out.println(
    client.query(
        Arr(
            Intersection(
                Arr(Value("A"), Value("B")),
                Arr(Value("C"), Value("D"))
            ),
            Intersection(
                Arr(Value("A"), Value("B")),
                Arr(Value("B"), Value("C"))
            ),
            Intersection(
                Arr(Value("A"), Value("B"), Value("C")),
                Arr(Value("B"), Value("C")),
                Arr(Value("B"), Value("C"), Value("D"))
            ),
            Intersection(
                Arr(Value("A"), Value("B"), Value("C")),
                Arr(Value("B"), Value("B")),
                Arr(Value("B"))
            )
        )
    ).get());
client.query([
  q.Intersection(['A', 'B'], ['C', 'D']),
  q.Intersection(['A', 'B'], ['B', 'C']),
  q.Intersection(['A', 'B', 'C'], ['B', 'C'], ['B', 'C', 'D']),
  q.Intersection(['A', 'B', 'C'], ['B', 'B'], ['B']),
])
.then((ret) => console.log(ret))
print(client.query(
  [
    q.intersection(["A", "B"], ["C", "D"]),
    q.intersection(["A", "B"], ["B", "C"]),
    q.intersection(["A", "B", "C"], ["B", "C"], ["B", "C", "D"]),
    q.intersection(["A", "B", "C"], ["B", "B"], ["B"]),
  ]
))
println(Await.result(
  client.query(
    Arr(
      Intersection(Arr("A", "B"), Arr("C", "D")),
      Intersection(Arr("A", "B"), Arr("B", "C")),
      Intersection(Arr("A", "B", "C"), Arr("B", "C"), Arr("B", "C", "D")),
      Intersection(Arr("A", "B", "C"), Arr("B", "B"), Arr("B"))
    )
  ),
  5.seconds
))
Not available in this language yet.
[
  [],
  [
    "B"
  ],
  [
    "B",
    "C"
  ],
  [
    "B"
  ]
]
[[] [B] [B C] [B]]
[[], ["B"], ["B", "C"], ["B"]]
[ [], [ 'B' ], [ 'B', 'C' ], [ 'B' ] ]
[[], ['B'], ['B', 'C'], ['B']]
[[], ["B"], ["B", "C"], ["B"]]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!