问题描述
我有一组 Neo4J 中的家谱数据,我正在尝试构建一个 Cypher 查询,该查询生成类似于以下内容的 JSON 数据集:
I have a set of data for a family tree in Neo4J and am trying to build a Cypher query that produces a JSON data set similar to the following:
{Name: "Bob",
parents: [
{Name: "Roger",
parents: [
Name: "Robert",
Name: "Jessica"
]},
{Name: "Susan",
parents: [
Name: "George",
Name: "Susan"
]}
]}
我的图在 MEMBER 节点之间存在 PARENT 关系(即 MATCH (p.Member)-[:PARENT]->(c.Member) ).我发现 密码中的嵌套 has_many 关系 和 neo4j cypher 嵌套收集 最终将所有父节点分组到我正在搜索的主要子节点.
My graph has a relationship of PARENT between MEMBER nodes (i.e. MATCH (p.Member)-[:PARENT]->(c.Member) ). I found Nested has_many relationships in cypher and neo4j cypher nested collect which ends up grouping all parents together for the main child node I am searching for.
根据反馈增加一些清晰度:
Adding some clarity based on feedback:
每个成员都有一个唯一的标识符.工会目前都与 PARENT 关系相关联.一切都被索引,所以性能不会受到影响.当我运行查询以获取节点图时,我得到了预期的结果.我正在尝试返回一个可以用于 D3 可视化目的的输出.理想情况下,这将通过 Cypher 查询完成,因为我正在使用 API 从正在构建的前端访问 neo4j.
Every member has a unique identifier. The unions are currently all associated with the PARENT relationship. Everything is indexed so that performance will not suffer. When I run a query to just get back the node graph I get the results I expect. I'm trying to return an output that I can use for visualization purposes with D3. Ideally this will be done with a Cypher query as I'm using the API to access neo4j from the frontend being built.
添加示例查询:
MATCH (p:Person)-[:PARENT*1..5]->(c:Person)
WHERE c.FirstName = 'Bob'
RETURN p.FirstName, c.FirstName
此查询返回五代人的每个父代的列表,但它没有显示层次结构,而是将鲍勃"列为每个关系的子代.是否有一个 Cypher 查询至少可以显示数据中的每个关系?我可以根据需要从那里格式化它...
This query returns a list of each parent for five generations, but instead of showing the hierarchy, it's listing 'Bob' as the child for each relationship. Is there a Cypher query that would show each relationship in the data at least? I can format it as I need to from there...
推荐答案
您可能还可以查看 Rik van Bruggens 关于他的家庭数据的博客:
关于您的查询
您已经在此处创建了路径模式:(p:Person)-[:PARENT*1..5]->(c:Person)
您可以将其分配给变量 tree
然后对该变量进行操作,例如返回树,或 nodes(tree)
或 rels(tree)
或以其他方式对该集合进行操作:
You already create a path pattern here: (p:Person)-[:PARENT*1..5]->(c:Person)
you can assign it to a variable tree
and then operate on that variable, e.g. returning the tree, or nodes(tree)
or rels(tree)
or operate on that collection in other ways:
MATCH tree = (p:Person)-[:PARENT*1..5]->(c:Person)
WHERE c.FirstName = 'Bob'
RETURN nodes(tree), rels(tree), tree, length(tree),
[n in nodes(tree) | n.FirstName] as names
另见密码参考卡:http://neo4j.com/docs/stable/cypher-refcard 和在线培训 http://neo4j.com/online-training 学习关于 Cypher 的更多信息.
See also the cypher reference card: http://neo4j.com/docs/stable/cypher-refcard and the online training http://neo4j.com/online-training to learn more about Cypher.
别忘了
create index on :Person(FirstName);
这篇关于使用 Neo4J 创建家谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!