Neo4j 路径查询/排序路径中的属性
Neo4j path query / sorting properties in path
电影数据集上的非常简单的最短路径查询:
MATCH path=shortestPath((p1:Person)-[*0..15]-(p2:Person)) WHERE toLower(p1.name) = toLower('Halle Berry') AND toLower(p2.name) = toLower('Tom Hanks')
RETURN relationships(path) AS relationships, nodes(path) AS nodes
如何对角色数组进行排序? IE。在这部特别的电影中,他们扮演了很多角色。我可以在应用程序端对其进行排序,但最好在 Neo 端进行排序。说到节点,我一直在用 UNWIND 做这件事,但这似乎对路径属性不起作用。
您可以访问路径的元素并一次获取一个属性。这很乏味,但它完成了工作。
elements[index] 为起始节点; elements[index+1] 是关系,elements[index+2] 是结束节点。
ID(node)是节点id,type(node)是关系类型
MATCH path=shortestPath((p1:Person)-[*0..15]-(p2:Person))
WHERE toLower(p1.name) = toLower('Halle Berry') AND toLower(p2.name) = toLower('Tom Hanks')
WITH apoc.path.elements(path) AS elements, nodes(path) AS nodes
UNWIND range(0, size(elements)-2) AS index
WITH nodes, elements, index WHERE index %2 = 0
WITH nodes, ID(elements[index]) AS start, ID(elements[index+1]) AS identity, ID(elements[index+2]) AS end, type(elements[index+1]) as type, elements[index+1] as roles
UNWIND roles as role
WITH nodes, start, end, identity, type, apoc.coll.sort(role.roles) as roles
RETURN collect({start: start, end: end, identity: identity, type: type, properties: {roles: roles}}) as relationships, nodes
RESULT:
╒══════════════════════════════════════════════════════════════════════╤══════════════════════════════════════════════════════════════════════╕
│"relationships" │"nodes" │
╞══════════════════════════════════════════════════════════════════════╪══════════════════════════════════════════════════════════════════════╡
│[{"identity":139,"start":106,"end":105,"type":"ACTED_IN","properties":│[{"name":"Halle Berry","born":1966},{"title":"Cloud Atlas","tagline":"│
│{"roles":["Jocasta Ayrs","Luisa Rey","Meronym","Ovid"]}},{"identity":1│Everything is connected","released":2012},{"name":"Tom Hanks","born":1│
│37,"start":105,"end":71,"type":"ACTED_IN","properties":{"roles":["Derm│956}] │
│ot Hoggins","Dr. Henry Goose","Isaac Sachs","Zachry"]}}] │ │
└──────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────┘
====已编辑===
您可以 UNWIND 角色然后对其进行排序。最后,再次收藏为字典列表。
MATCH path=shortestPath((p1:Person)-[*0..15]-(p2:Person))
WHERE toLower(p1.name) = toLower('Halle Berry') AND toLower(p2.name) = toLower('Tom Hanks')
WITH relationships(path) AS relationships, nodes(path) AS nodes
UNWIND relationships as role
WITH apoc.coll.sort(role.roles) as roles, nodes
RETURN collect({roles: roles}) as relationships, nodes
RESULT:
╒══════════════════════════════════════════════════════════════════════╤══════════════════════════════════════════════════════════════════════╕
│"relationships" │"nodes" │
╞══════════════════════════════════════════════════════════════════════╪══════════════════════════════════════════════════════════════════════╡
│[{"roles":["Jocasta Ayrs","Luisa Rey","Meronym","Ovid"]},{"roles":["De│[{"name":"Halle Berry","born":1966},{"title":"Cloud Atlas","tagline":"│
│rmot Hoggins","Dr. Henry Goose","Isaac Sachs","Zachry"]}] │Everything is connected","released":2012},{"name":"Tom Hanks","born":1│
│ │956}] │
└──────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────┘
电影数据集上的非常简单的最短路径查询:
MATCH path=shortestPath((p1:Person)-[*0..15]-(p2:Person)) WHERE toLower(p1.name) = toLower('Halle Berry') AND toLower(p2.name) = toLower('Tom Hanks')
RETURN relationships(path) AS relationships, nodes(path) AS nodes
如何对角色数组进行排序? IE。在这部特别的电影中,他们扮演了很多角色。我可以在应用程序端对其进行排序,但最好在 Neo 端进行排序。说到节点,我一直在用 UNWIND 做这件事,但这似乎对路径属性不起作用。
您可以访问路径的元素并一次获取一个属性。这很乏味,但它完成了工作。
elements[index] 为起始节点; elements[index+1] 是关系,elements[index+2] 是结束节点。 ID(node)是节点id,type(node)是关系类型
MATCH path=shortestPath((p1:Person)-[*0..15]-(p2:Person))
WHERE toLower(p1.name) = toLower('Halle Berry') AND toLower(p2.name) = toLower('Tom Hanks')
WITH apoc.path.elements(path) AS elements, nodes(path) AS nodes
UNWIND range(0, size(elements)-2) AS index
WITH nodes, elements, index WHERE index %2 = 0
WITH nodes, ID(elements[index]) AS start, ID(elements[index+1]) AS identity, ID(elements[index+2]) AS end, type(elements[index+1]) as type, elements[index+1] as roles
UNWIND roles as role
WITH nodes, start, end, identity, type, apoc.coll.sort(role.roles) as roles
RETURN collect({start: start, end: end, identity: identity, type: type, properties: {roles: roles}}) as relationships, nodes
RESULT:
╒══════════════════════════════════════════════════════════════════════╤══════════════════════════════════════════════════════════════════════╕
│"relationships" │"nodes" │
╞══════════════════════════════════════════════════════════════════════╪══════════════════════════════════════════════════════════════════════╡
│[{"identity":139,"start":106,"end":105,"type":"ACTED_IN","properties":│[{"name":"Halle Berry","born":1966},{"title":"Cloud Atlas","tagline":"│
│{"roles":["Jocasta Ayrs","Luisa Rey","Meronym","Ovid"]}},{"identity":1│Everything is connected","released":2012},{"name":"Tom Hanks","born":1│
│37,"start":105,"end":71,"type":"ACTED_IN","properties":{"roles":["Derm│956}] │
│ot Hoggins","Dr. Henry Goose","Isaac Sachs","Zachry"]}}] │ │
└──────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────┘
====已编辑=== 您可以 UNWIND 角色然后对其进行排序。最后,再次收藏为字典列表。
MATCH path=shortestPath((p1:Person)-[*0..15]-(p2:Person))
WHERE toLower(p1.name) = toLower('Halle Berry') AND toLower(p2.name) = toLower('Tom Hanks')
WITH relationships(path) AS relationships, nodes(path) AS nodes
UNWIND relationships as role
WITH apoc.coll.sort(role.roles) as roles, nodes
RETURN collect({roles: roles}) as relationships, nodes
RESULT:
╒══════════════════════════════════════════════════════════════════════╤══════════════════════════════════════════════════════════════════════╕
│"relationships" │"nodes" │
╞══════════════════════════════════════════════════════════════════════╪══════════════════════════════════════════════════════════════════════╡
│[{"roles":["Jocasta Ayrs","Luisa Rey","Meronym","Ovid"]},{"roles":["De│[{"name":"Halle Berry","born":1966},{"title":"Cloud Atlas","tagline":"│
│rmot Hoggins","Dr. Henry Goose","Isaac Sachs","Zachry"]}] │Everything is connected","released":2012},{"name":"Tom Hanks","born":1│
│ │956}] │
└──────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────┘