What I want to do
Find the intersection within an array A and an array B of edges.
Let’s suppose that we have 2 movie genres: Thriller and Comedy. These genres are unique and are themselves nodes, so the movie will have edges to these nodes depending on the number of genres that it has.
I want to find only the movies that contain these genres exclusively as follows:
movies(func: eq(dgraph.type, "Movies")) @cascade{
id
genre @filter( eq(name, "Thriller") AND eq(name, "Comedy") )
}
This returns an empty result as consequence, which is different from the OR
operator. But this makes sense because a name
is a unique predicate and it does not behave as a list. You can have a name or the other one, but you can’t have both.
So my question is, how can I find the intersection of these 2 edges results having this previous statement as a fact without consuming too many resources?. Below you’ll see my solution and will notice that it might become a very expensive operation if the transaction is made with too many elements at the same time.
What I did
{
movies1 as var(func: eq(dgraph.type, "Movies")) @cascade {
genre @filter(eq(name, "Thriller"))
}
movies2 as var(func: uid(movies1)) @cascade{
genre @filter(eq(name, "Comedy"))
}
movies3 as var(func: uid(movies2)) @cascade{
genre @filter(eq(name, "Drama"))
}
result(func: uid(movies3)) {
id
}
}
I hope that my explanation is clear, above is a solution to the problem, but I would love to get help to make this more efficient.
I appreciate a lot your contribution in advance.
Have a nice evening.
Dgraph metadata
dgraph version
v20.11.1