首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在neo4j中检查按有序路径排列的节点列表?

如何在neo4j中检查按有序路径排列的节点列表?
EN

Stack Overflow用户
提问于 2015-03-24 09:41:37
回答 1查看 237关注 0票数 1

在我的neo4j图形数据库中,有一个节点列表作为链存储,如下所示:

根节点1->新4j->图->database。 根节点2 ->新4j->图->搜索。 根节3->新4j->内部。

如果我给出一个节点列表(动态填充)作为输入,例如“neo4j图数据库”,我希望检查给定的节点序列并返回根文件夹。

neo4j图形数据库->根node1.neo4j图->root node1,根node2.neo4j ->root node1,root node2,root node3.

EN

回答 1

Stack Overflow用户

发布于 2015-03-24 17:06:25

首先,这是一些看起来像你的样本的数据。我做了一些假设,认为列出的数据都是独立的。

代码语言:javascript
复制
create (r1:Root {name: 'Root 1'})
create (n1:Node {name: 'Neo4j'})
create (n2:Node {name: 'Graph'})
create (n3:Node {name: 'Database'})
create r1-[:CONNECTED]->n1-[:CONNECTED]->n2-[:CONNECTED]->n3
create (r2:Root {name: 'Root 2'})
create (n4:Node {name: 'Neo4j'})
create (n5:Node {name: 'Graph'})
create (n6:Node {name: 'Search'})
create r2-[:CONNECTED]->n4-[:CONNECTED]->n5-[:CONNECTED]->n6
create (r3:Root {name: 'Root 3'})
create (n7:Node {name: 'Neo4j'})
create (n8:Node {name: 'Internals'})
create r3-[:CONNECTED]->n7-[:CONNECTED]->n8
return *

现在,这里有一个对有序列表有效的解决方案。0..。使它能够处理只有一个节点的有序列表。

代码语言:javascript
复制
// start with the ordered list of node names
with ['Neo4j', 'Graph', 'Database'] as ordered_list
// start by matching paths that begin with the first node
// in the ordered list and end with the last node
match p=((a:Node {name: ordered_list[0]})-[:CONNECTED*0..]-(b:Node {name: ordered_list[length(ordered_list)-1]}))
// collect these as the list of potential candidates
with collect(p) as candidates, ordered_list
// pass through the list and only keep the candidates that are 
// the same length as the ordered list
unwind candidates as candidate
with case when (length(nodes(candidate)) = length(ordered_list)) 
  then candidate
end as candidate, ordered_list
// recollect the filtered candidates
with collect(candidate) as candidates, ordered_list
// pass through the candidates now again and compare the names
// of the ordered list to the original ordered list
// only keep those that match as matched
unwind candidates as candidate
with case 
  when reduce(names = [], n in nodes(candidate) | names + n.name) = ordered_list 
  then candidate
end as matched
// with the matched nodes find the root node that is attached to
// first node in each matched path
match (r:Root)--(head:Node)
where id(head) = id(nodes(matched)[0])
// return the root and the matched path nodes
return r.name, nodes(matched)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29229259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档