-1

Can anyone explain with just words how this query works?

SELECT * FROM cypher('graph_name', $$
    MATCH p = (actor {name: 'Willam Defoe'})-[:ACTED_IN*2]-(co_actor)
    RETURN relationships(p)
$$) as (r agtype);

It's quite confusing and not very clear just reading the docs.

cybersam
  • 63,203
  • 6
  • 53
  • 76
Peter
  • 43
  • 4

3 Answers3

1

It finds all paths that have:

  • on one end, a node having a name property whose value is "William Defoe",
  • on the other end, any node,
  • and 2 consecutive ACTED_IN relationships (separated by any node) in between.

The directionality of the relationships are unspecified, so directionality does not matter. The node labels are also unspecified, so it does not matter how any of the nodes are labeled. And you should know that the db always omits MATCH results in which the same relationship appears more than once.

The query then returns for each path a list of the (2) relationships in each path, starting with the one connected to the "William Defoe" node.

cybersam
  • 63,203
  • 6
  • 53
  • 76
0

In this MATCH query, we are using Variable Length Edges. If there is a scenario where you have lets say vertex A (vertex has a property 'name' whose value is 'A') that has an edge with vertex B which has an edge with vertex C

A -> B -> C

This means there is a variable length edge between A and C. To query such an edge, you could formulate the following query:

SELECT * FROM cypher('graph_name', $$
MATCH p = (a: Vertex{name : 'A'})-[e: EDGE]->(b: Vertex{name : 'B'})-[e1: EDGE]->(c: Vertex
{name: 'C'})
RETURN p
$$) AS (path agtype);

And it would output the path (path is simply a traversal from one vertex to another and would contain all the vertices and the edges connecting the vertices)from vertex A to C with all the edges in between.

But, another way to query such an edge is using a more compact form and that is

SELECT * FROM cypher('graph_name', $$
MATCH p = (a: Vertex {name : 'A'})-[e: EDGE*2]->(c: Vertex {name : 'C'})
RETURN p
$$) AS (path agtype);

Here *2 means you want to travel only 2 edges from A. This also outputs the path from Vertex A to Vertex C with the two edges and one vertex it has traversed while going from A to C.

Coming to the query you have given, you are matching all the paths that start from

(actor {name: 'Willam Defoe'})

and end at

(co_actor)

That means all the paths that originate from any vertex with the property name: 'William Defoe' which is connected to a vertex with edge label 'ACTED_IN' and that vertex is connected with another vertex with label 'ACTED_IN' will be matched

I hope it helps!

Zainab Saad
  • 728
  • 1
  • 2
  • 8
0

In this query it means like you want to get all the co-actors that ACTED_IN in a movie(s) with Willam Defoe and for each co-actor you want the actors that ACTED_IN the same movie with them.

So, suppose Willam Defoe acted in movie M1 and actor A1 and A2 acted in movie M1 and M2.

Also actor A3 and A4 acted in movie M2.

Then the output of the query will be include path from Willam Defoe actor and all the co-actors (A1,A2) that ACTED_IN the same movie (M1) and for every co-actor (A1 , A2 in this example) we will get the co-actors that ACTED_IN the same movie (M2 in this example) which are A3 and A4.

So, now all the co-actors that will be included in the path in this example are A1,A2,A3,A4 because A1 and A2 ACTED_IN the same movie M1 with Willam Defoe and also A3 and A4 ACTED_IN the same movie M2 with A1 and A2.

Omar Saad
  • 349
  • 3
  • 8