如何基于neo4j节点标签返回集合?

时间:2019-06-27 00:19:46

标签: spring-boot neo4j cypher

我不确定如何编写neo4j查询来查找所有节点(包括最短路径中的标签),并在输出中按标签对它们进行分组。

编辑:最短路径包括标签->文件-> SubDirs-> SourceDir之间的链接,我希望所有这些链接。

这种neo4j语法在某处是错误的,但这基本上就是我要返回到我的java spring代码的原因

MATCH (a:Tag),(b:File),(c:SourceDir),
path = shortestPath((a)-[*]->(c))
WHERE a.name=~('(?i).*'+"READ"+'.*')
FOREACH (n IN NODES(path) |
WHERE label(n) = "SubDir" as dir |
WHERE label(n) = "SourceDir" as src |
WHERE label(n) = "File" as file |
WHERE label(n) = "Tag" as tag)
RETURN COLLECT(dir) as dirs, src as source, COLLECT(file) as file, COLLECT(tag) as tags

java语法部分如下所示,它由neo4j查询和结果组成

 @Query("MATCH (a:Tag),(b:File),(c:SourceDir), "
             + "path = shortestPath((a)-[*]->(c)) "
             + "WHERE a.name=~('(?i).*'+{search}+'.*') "
             + "FOREACH (n IN NODES(path) | "
             + "WHERE label(n) = 'SubDir' as dir | "
             + "WHERE label(n) = 'SourceDir' as src | "
             + "WHERE label(n) = 'File' as file | "
             + "WHERE label(n) = 'Tag' as tag) "
             + "RETURN COLLECT(dir) as dirs, src as source, COLLECT(file) as files, COLLECT(tag) as tags)")
  public List<ResultData> findByTag(@Param("search") String search);

@QueryResult
public class ResultData
{
   Set<File> files;
   Set<Directory> dirs;
   Source source;
   Set<Tag> tags;
}

什么是neo4j正确的查询才能给我带来预期的结果?

1 个答案:

答案 0 :(得分:0)

这可能对您有用:

50.35

注意:一个节点可以有多个标签,因此上面的查询将检查每个节点的每个标签。