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 Set Reference, 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

One or more arrays or Set References 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 Set Reference, a Set Reference of the items that appear in every provided group.

Examples

The following query intersects the Set Reference returned by locating the search term "fire" in the index named "spells_by_element" with the Set Reference returned by locating the search term "water" in the Index named "spells_by_element". The Paginate function executes the Set Reference and returns the results of the Intersect operation in a Page.

try
{
    Value result = await client.Query(
        Paginate(
            Intersection(
                Match(Index("spells_by_element"), "fire"),
                Match(Index("spells_by_element"), "water")
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(data: Arr(RefV(id = "181388642071085568", collection = RefV(id = "spells", collection = RefV(id = "collections")))))
result, err := client.Query(
	f.Paginate(
		f.Intersection(
			f.MatchTerm(f.Index("spells_by_element"), "fire"),
			f.MatchTerm(f.Index("spells_by_element"), "water"))))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:[{181388642071085568 0xc0001421e0 0xc0001421e0 <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))
.catch((err) => console.error('Error: %s', err))
{ data: [ Ref(Collection("spells"), "181388642071085568") ] }
result = client.query(
  q.paginate(
    q.intersection(
      q.match(q.index("spells_by_element"), "fire"),
      q.match(q.index("spells_by_element"), "water")
    )
  )
)
print(result)
{'data': [Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections)))]}
try {
  println(Await.result(
    client.query(
  Paginate(
    Intersection(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water")))),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{data: [ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))]}

The following query demonstrates how various arrays are evaluated:

try
{
    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"))
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(Arr(), Arr(StringV(B)), Arr(StringV(B), StringV(C)), Arr(StringV(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"}),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	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))
.catch((err) => console.error('Error: %s', err))
[ [], [ 'B' ], [ 'B', 'C' ], [ 'B' ] ]
result = 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"]),
  ]
)
print(result)
[[], ['B'], ['B', 'C'], ['B']]
try {
  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
))} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
[[], ["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!