根据查询neo4j

时间:2019-05-05 17:39:48

标签: neo4j cypher

我想根据一个节点和一组节点之间最短路径的结果建立新的关系。我目前正在使用此查询来执行此类任务,但要在两个节点之间进行(很好):

MATCH
  (p1:Category {catName: "Main_topic_classifications"}), 
  (p2:Category {catName: "Monarchs_of_the_Bulgars"}),
  path = allShortestPaths((p2)-[:SUBCAT_OF*]->(p1))
UNWIND RELATIONSHIPS(path) AS rel
WITH STARTNODE(rel) AS s, ENDNODE(rel) AS e
MERGE (s)-[:NEW_SUBCAT]->(e)

如何归纳此查询以返回p2(“ Monarchs_of_the_Bulgars”)和节点列表p1 = [“ Health”,“ History”,“ Humanity”]之间的所有最短路径列表,而不是返回像上一个查询?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这应该有效:

MATCH (p1:Category)
WHERE p1.category IN ["Health", "History", "Humanity"]
MATCH
  (p2:Category {catName: "Monarchs_of_the_Bulgars"}),
  path = allShortestPaths((p2)-[:SUBCAT_OF*]->(p1))
UNWIND RELATIONSHIPS(path) AS rel
WITH STARTNODE(rel) AS s, ENDNODE(rel) AS e
MERGE (s)-[:NEW_SUBCAT]->(e)

[已更新]

如果p1也必须是Top_Main_Classification的直接子目录,则可以执行以下操作:

MATCH (p1:Category)-[:SUBCAT_OF]->(main:Category {catName: "Main_topic_classifications"})
WHERE p1.category IN ["Health", "History", "Humanity"]
MERGE (p1)-[:NEW_SUBCAT]->(main)
MATCH
  (p2:Category {catName: "Monarchs_of_the_Bulgars"}),
  path = allShortestPaths((p2)-[:SUBCAT_OF*]->(p1))
UNWIND RELATIONSHIPS(path) AS rel
WITH STARTNODE(rel) AS s, ENDNODE(rel) AS e
MERGE (s)-[:NEW_SUBCAT]->(e)