简体   繁体   English

Neo4j Cypher 查询:通过输入路径查找节点

[英]Neo4j Cypher query: Find node by input path

I have a graph that contains a tree hierarchy of the system.我有一个包含系统树层次结构的图表。 In this graph, one root has indexed label 'MainRoot', all other relationships are type 'hasParent'.在此图中,一个根已索引 label 'MainRoot',所有其他关系的类型均为 'hasParent'。 I would like to construct a query that would have list of nodes names (=inputPath) as an input and return the node at the end of query.我想构建一个查询,将节点名称列表 (=inputPath) 作为输入,并在查询结束时返回节点。

Now i have this working example, which last returned item is the specified node with the name "java", as I want (located in Root/src/main/java):现在我有了这个工作示例,最后返回的项目是我想要的名称为“java”的指定节点(位于 Root/src/main/java 中):

// Input
WITH ["Root", "src", "main", "java"] AS inputPath
// Iterator
UNWIND range(0,size(inputPath)-2) AS i
MATCH (parent)<-[:hasParent]-(child)
WHERE (parent.name = inputPath[i]) AND (child.name = inputPath[i+1])
RETURN child

However, I would like to somehow let the query now, that the first parent element of the query is the node with the indexed label 'MainRoot'.但是,我现在想以某种方式让查询,查询的第一个父元素是索引为 label 'MainRoot' 的节点。

  1. I expect it would be more efficient since it will now, in which node to start the traversal.我希望它会更有效率,因为它现在将在哪个节点开始遍历。
  2. If I have structure eg Root/example/Root/src/main/java, the query would also return this 'java' node, which I obviously don't want to return.如果我有结构,例如 Root/example/Root/src/main/java,查询也会返回这个“java”节点,我显然不想返回它。

Any idea how could I do that?知道我该怎么做吗?

This is the shortest way I could think of.这是我能想到的最短的方法。

WITH ["Root", "src", "main", "java"] AS inputPath     
MATCH path=(mainRoot)<-[:hasParent*]-(child)
WHERE LENGTH(path) = SIZE(inputPath)-1
AND [n IN nodes(path) | n.name] = inputPath
RETURN path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM