简体   繁体   English

Neo4j:层次结构查找密码查询

[英]Neo4j: hierarchy finding cypher query

I am trying to find the hierarchy - both upward and downward for the following domain model. 我正在尝试查找层次结构-对于以下领域模型,它既向上又向下。 As being new to neo4j I am having some hurdle to get the right hierarchy with the cypher query. 作为neo4j的新手,我遇到了一些障碍,无法通过密码查询获得正确的层次结构。

Any assistance or sample would be a great help to me... 任何帮助或样品都会对我有很大帮助。

(:Person{id=1})-[HAS_CHILD]->(:Person{id=2})-[HAS_CHILD]->(:Person{id=3})-[HAS_CHILD]->(:Person{id=4})-[HAS_CHILD]->(:Person{id=5})

....like that to a very deep level & one person can have multiple children. ....就像是一个非常深的层次,一个人可以有多个孩子。

I am trying to build some query to find 我正在尝试建立一些查询来查找

  1. A Person and all his children hierarchy 一个人及其所有子级

  2. A person and his parent hierarchy 一个人及其父级

  3. Relation ship between two person - if any 两人之间的关系-如果有的话

  4. A Person and all his children hierarchy - up to a specific depth 一个人及其所有子级-达到特定深度

These should work. 这些应该工作。

The WHERE clauses are for eliminating subpaths. WHERE子句用于消除子路径。 The answer for #4 cannot use the same WHERE clause, so the results will contain subpaths. #4的答案不能使用相同的WHERE子句,因此结果将包含子路径。

1. 1。

MATCH p=(:Person{id:1})-[:HAS_CHILD*]->(c)
WHERE NOT (c)-[:HAS_CHILD]->()
RETURN NODES(p);

2. 2。

MATCH p=(:Person { id:5 })<-[:HAS_CHILD*]-(ancestor)
WHERE NOT (ancestor)<-[:HAS_CHILD]-()
RETURN NODES(p);

3. 3。

MATCH p=(p5:Person { id:5 })-[:HAS_CHILD*]-(p1:Person { id:1 })
RETURN NODES(p);

4. Using a depth up to 5: 4.使用最多5个深度:

MATCH p=(:Person{id:1})-[:HAS_CHILD*1..5]->(c)
RETURN NODES(p);

Using your datamodel consisting of (:Person)-[:HAS_CHILD]->(:Person) these queries should return the data you are looking for (let's also assume you have a unique name property on each Person node to facilitate a lookup by name, but you could also use any unique id/property): 使用由(:Person)-[:HAS_CHILD]->(:Person)组成的数据模型,这些查询应返回您要查找的数据(我们还假设您在每个Person节点上都有一个唯一的name属性,以方便按名称查找,但您也可以使用任何唯一的ID /属性):

A person and all his children 一个人和他所有的孩子

We can use a variable length path pattern here to match on patterns containing multiple HAS_CHILD relationships. 我们可以在此处使用可变长度路径模式来匹配包含多个HAS_CHILD关系的模式。

MATCH (p:Person)-[:HAS_CHILD*]->(child:Person)
WHERE p.name = "Bob Loblaw"
RETURN child;

A person and his parent hierarchy 一个人及其父级

Very similar to the above query, but we just reverse the relationship direction. 与上面的查询非常相似,但是我们只是反转关系方向。

MATCH (p:Person)<-[:HAS_CHILD*]-(ancestor:Person)
WHERE p.name = "Bob Loblaw"
RETURN ancestor;

Relationship between two person - if any 两人之间的关系-如果有的话

We can use the shortestPath function to find the shortest path between two nodes in the graph. 我们可以使用shortestPath函数找到图中两个节点之间的最短路径。 This query will return no rows if no path is found. 如果找不到路径,此查询将不返回任何行。

MATCH path=shortestPath((p:Person {name: "Bob Loblaw"})-[*]-(o:Person {name: "Louise Bluth"}))
RETURN path

A person and their child hierarchy - up to a specific depth 一个人及其子级-达到特定深度

This query is very similar to the previous "person and all his children query" however we can specify bounds on the variable length path. 该查询与先前的“人及其所有子查询”非常相似,但是我们可以在可变长度路径上指定范围。 Note : an upper bound should always be specified with variable length paths to avoid very long running queries. 注意 :应始终使用可变长度路径指定上限,以避免运行时间很长的查询。

MATCH (p:Person)-[:HAS_CHILD*1..5]->(child:Person)
WHERE p.name = "Bob Loblaw"
RETURN child;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM