如何将此密码查询转换为java代码?

时间:2013-11-04 15:34:07

标签: java neo4j cypher

我有一个neo4j图形数据库,我使用java嵌入式,如何将此cypher查询转换为java代码(是否可以)?

查询:

START n=node(*)
MATCH p=n-[rels:INCLUDE*]->m 
WHERE ALL (rel IN rels 
  WHERE rel.status='on') 
WITH COLLECT(p) AS paths, MAX(length(p)) AS maxLength 
RETURN FILTER(path IN paths 
  WHERE length(path)= maxLength) AS longestPaths

此查询是关于查找与STATUS =“on”属性相互关联的节点之间的最长路径,并返回路径。

因为我读到使用neo4j数据库,从java api开始比从java应用程序运行cypher查询要快。

所以请帮我写一下这个cypher查询做同样事情的java代码。

提前感谢。

1 个答案:

答案 0 :(得分:3)

如果您指的是遍历框架,那么这是您最好的选择: http://docs.neo4j.org/chunked/stable/tutorial-traversal.html

您可能希望进行自己的性能测试,以确定哪个更快。

请原谅我,因为我的java有点生疏,而且我没有办法真正为你测试这个。但是我觉得这会得到你想要的东西。

List<Path> longestPaths = null;
int longestLength = 0;

for ( Path position : Traversal.description()
        .depthFirst()
        .relationships( Rels.INCLUDE )
        .relationships( Rels.LIKES, Direction.OUTGOING )
        .evaluator( new Evaluator {
            public Evaluation evaluate(Path path) {
                Relationship r = path.lastRelationship();
                if(r != null && "on".equals(r.getProperty("status"))) {
                    return Evaluation.INCLUDE_AND_CONTINUE;
                }

                return EXCLUDE_AND_PRUNE;
            }
        } )
        .traverse( node ) )
{
    //Given that node is your start node this would be the (WHERE ALL rels status = on)

    if(longestPaths == null || longestLength < position.length()) {
        longestPaths = new ArrayList<Path>();
        longestPaths.add(position);
        longestLength = position.length();
    }
}

//At this point you should have a collection of the longest paths