遍历描述等同于密码查询

时间:2014-09-10 09:13:13

标签: java neo4j cypher graph-traversal

我有一个密码查询,我正在尝试编写等效的遍历描述,但我被订单所困扰。

start n=node:EduNodes(title='bachelor of science')     
match p=n-[:JOB_CHANGE|EDUCATION|CAREER_ENTRY|EDUCATION_CONTINUED*1..4]->m     
with p, relationships(p) as rels, n, m     
return p,n,m,     
reduce(weight=0, x in rels: weight + x.weight) as totalWeight     
order by totalWeight asc    

此查询非常消耗内存且速度慢,遍历描述很快:

TraversalDescription td = Traversal.traversal().evaluator(Evaluators.excludeStartPosition())
        .uniqueness(Uniqueness.NONE).depthFirst()
        .evaluator(Evaluators.includingDepths(1, 4))
        .relationships(RelationshipTypes.JOB_CHANGE, Direction.OUTGOING)
        .relationships(RelationshipTypes.EDUCATION, Direction.OUTGOING)
        .relationships(RelationshipTypes.CAREER_ENTRY, Direction.OUTGOING)
        .relationships(RelationshipTypes.EDUCATION_CONTINUED, Direction.OUTGOING);

我可以在遍历描述中还有cypher查询的两个最后几行(reduce order)吗?之后我可以在循环中做到这一点..

Traverser traverser = td.traverse(start);
List<PathWrapper> orderedTraversalResult = new ArrayList<>();
for (Path path : traverser) {
    orderedTraversalResult.add(new PathWrapper(path));
}
Collections.sort(orderedTraversalResult);

1 个答案:

答案 0 :(得分:0)

您可以提供自己的PathEvaluator能够处理分支状态的{{1}}。最好的资源是Neo4j本身的单元测试,请查看https://github.com/neo4j/neo4j/blob/master/community/kernel/src/test/java/org/neo4j/kernel/impl/traversal/TestBranchState.java