Variable Edges Query

huangapple go评论62阅读模式
英文:

Variable Edges Query

问题

这个查询如何工作,可以用文字解释吗?

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

阅读文档后,仍然相当令人困惑,不太清晰。

英文:

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.

答案1

得分: 1

它找到了所有具有以下特点的路径:

  • 一端是具有 name 属性值为 "William Defoe" 的节点,
  • 另一端是任意节点,
  • 中间有两个连续的 ACTED_IN 关系(由任意节点分隔)。

关系的方向未指定,因此方向不重要。节点标签也未指定,因此节点的标签不重要。您应该知道,数据库始终会省略包含相同关系多次出现的 MATCH 结果。

然后,查询为每个路径返回一个列表,其中包含每个路径中的(2)关系,以从与 "William Defoe" 节点连接的关系开始。

英文:

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.

答案2

得分: 0

以下是已翻译的内容:

在此MATCH查询中,我们正在使用可变长度边。如果存在这样的情况,即您有一个顶点A(顶点具有属性'name',其值为'A'),它与顶点B相连,而顶点B又与顶点C相连

A -> B -> C
这意味着A和C之间存在可变长度的边。要查询这样的边,您可以制定以下查询:

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);
它将输出从顶点A到C的路径(路径只是从一个顶点到另一个顶点的遍历,包含所有顶点和连接顶点的边缘)以及中间的所有边缘。

但是,查询这样的边的另一种方法是使用更紧凑的形式,即

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

这里的*2表示您只想从A经过2条边。这也会输出从顶点A到顶点C的路径,包括两条边和一个在从A到C时遍历的顶点。

至于您提供的查询,您正在匹配从

(actor {name: 'Willam Defoe'})

并且结束于

(co_actor)

这意味着所有起源于具有属性'name: 'William Defoe''的任何顶点并且连接到具有边缘标签'ACTED_IN'的顶点,并且该顶点连接到另一个具有标签'ACTED_IN'的顶点的路径都将匹配

希望这有所帮助!

英文:

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!

答案3

得分: 0

在此查询中,它的意思是你想要获取所有与 Willam Defoe 在一部或多部电影中一同出演过的合作演员,并且对于每位合作演员,你想要获取那些与他们在同一部电影中一同出演过的演员。

所以,假设 Willam Defoe 出演了电影 M1,演员 A1A2 出演了电影 M1M2

而演员 A3A4 出演了电影 M2

那么查询的输出将包括从 Willam Defoe 演员开始的路径,以及所有与他在同一部电影中出演过的合作演员(例如,A1A2)以及对于每位合作演员(例如,在这个例子中是 A1A2),我们将获取与他们在同一部电影中出演过的合作演员(在这个例子中是 A3A4)。

因此,现在在这个例子中包含在路径中的所有合作演员是 A1A2A3A4,因为 A1A2Willam Defoe 在同一部电影 M1 中出演过,而 A3A4A1A2 在同一部电影 M2 中出演过。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月6日 15:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定