密码查询,用于按路径顺序返回节点



我有一个neo4j graphdb,它存储节点的有序集合(假设Person节点),每个人都有一个Talent节点:

(p:Person)-[:HAS_TALENT]->(t:Talent)

我正在组织一个才艺表演,并有人们表演顺序的时间表:

(p:Person)-[:FOLLOWS*]->(q:Person)

我可以编写一个查询来返回表示人员执行顺序的路径,当我返回该路径时,Person 节点将按照它们在路径中出现的顺序显示。 但是,当我执行子查询以返回人员正在执行的才能时,它们不再按路径排序显示。

有没有办法根据节点在路径中出现的顺序对节点进行排序? 我尝试使用 COLLECT 来获取 Person 节点的列表,但我似乎找不到使用该集合来获取 Talent 节点的有序列表的方法。

我当前的路径查询看起来像这样,有一个特殊的开始和结束节点,所以我知道谁是第一个和最后一个。(我现在面前没有数据库,所以如果它不是正确的,请道歉):

MATCH p=(s:StartNode {side:"start"})-[:FOLLOWS*1..10]->(s:StartNode {side:"end"}) RETURN p

我已经从头到尾浏览了很多不同的路径检索选项,它们各有利弊,但我找不到按相同顺序检索 Talent 节点的方法。


编辑:看来我可能过度简化了我的示例场景。 我实际使用的数据库从头到尾通过 Person 节点有多个路径,我有一个查询,首先从头到尾只选择一条路径,然后再继续匹配 Talent 节点。 向霍斯滕@Dirk道歉,他回答了我提出的问题,但不是我需要回答的问题。

由于我已经在编辑它,@Dave Bennett 的答案解决了我的问题,因为 unwind 函数似乎为未来的 MATCH 子句保持路径的顺序不变。

假设表演者是人 1..5,他们每个人都拥有单一的天赋 A..E这样Person 1Talent APerson 5Talent E。才艺表演从人物 1 开始,以人物 5 结束。

//match all of the possible show paths where the show starts
// with the first performer 
MATCH show=(:Person {name:'Person 1'})<-[:FOLLOWS*]-(:Person) 
// pass on the performers and the number of performers
// as identifiers with the WITH clause
WITH nodes(show) AS performers, length(show) AS num
// order the possible shows in descending order by number of performers
ORDER BY num DESC
// limit the list to the show with the most performances
LIMIT 1
// unwind the performers collection as individual performer
UNWIND performers AS p
// match the talent that matches the performers
MATCH p-[:HAS_TALENT]->(t:Talent)
// return the name of the performer and their talent
RETURN p.name, t.name

如果我在前面添加一个(显示)节点

create (s:show{date:'2014-01-31'})
create  (s)<-[:STARTS]-(p1:person{pId:1})-[:HAS_TALENT]->(:talent{tId:1})
create (p1)<-[:FOLLOWS]-(p2:person{pId:2})-[:HAS_TALENT]->(:talent{tId:2})
create (p2)<-[:FOLLOWS]-(p3:person{pId:3})-[:HAS_TALENT]->(:talent{tId:3})
create (p3)<-[:FOLLOWS]-(p4:person{pId:4})-[:HAS_TALENT]->(:talent{tId:4})
create (p4)<-[:FOLLOWS]-(p5:person{pId:5})-[:HAS_TALENT]->(:talent{tId:5})

那么这应该可以完成工作:

match spt=(s:show{date:'2014-01-31'})<-[:STARTS|FOLLOWS *]-(p:person)-[:HAS_TALENT]->(t:talent) 
return p.id as performer,t.tId as act order by length(spt)

当然,您也可以返回完整的t,您可以collect(t.tId)或任何您喜欢的东西(将在装有Neo4j的PC上进行测试)

最新更新