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.

The run time of Intersection is dependent on the size of the underlying set or page, and the exclusivity of the result. For large sets or pages with many non-exclusive items, executing Intersection might result in a query timeout error.

To work around this, you may specify a larger query timeout via the driver that you are using.

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.

client.Query(
  Paginate(
    Intersection(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))));
{ "data": [ { "@ref": "classes/spells/181388642071085568" } ] }
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "intersection": [
              {
                "match": { "index": "spells_by_element" },
                "terms": "fire"
              },
              {
                "match": { "index": "spells_by_element" },
                "terms": "water"
              }
            ]
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [ { "@ref": "classes/spells/181388642071085568" } ]
  }
}
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)
map[data:[{181388642071085568 0xc4202920a0 <nil>}]]
System.out.println(
   client.query(
     Paginate(
       Intersection(
         Match(Index(Value("spells_by_element")), Value("fire")),
         Match(Index(Value("spells_by_element")), Value("water"))
       )
     )
   ).get());
{
  data: [
    ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))
  ]
}
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))
{ data: [ Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) ] }
client.query(
  q.paginate(
    q.intersection(
      q.match(q.index("spells_by_element"), "fire"),
      q.match(q.index("spells_by_element"), "water")
    )
  ))
{ "data": [ { "@ref": "classes/spells/181388642071085568" } ] }
client.query(
  Paginate(
    Intersection(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))))
{ "data": [ { "@ref": "classes/spells/181388642071085568" } ] }

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"))
  )
);
[
  [],
  [
    "B"
  ],
  [
    "B",
    "C"
  ],
  [
    "B"
  ]
]
Not available in this language yet.
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)
[[] [B] [B C] [B]]
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());
[[], ["B"], ["B", "C"], ["B"]]
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))
[ [], [ 'B' ], [ 'B', 'C' ], [ 'B' ] ]
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"]),
  ]
))
[[], ['B'], ['B', 'C'], ['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
))
[[], ["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!