用Neo4J创建家谱 [英] Creating Family Tree with Neo4J

查看:383
本文介绍了用Neo4J创建家谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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密码嵌套的收藏,最终将所有父节点归为我要搜索的主要子节点.

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...

推荐答案

您也可以看看关于您的查询

您已经在此处创建了路径模式:(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创建家谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆