2014-09-10 45 views
1

我有一個密碼查詢,我試圖編寫等價的遍歷描述,但是我被訂單卡住了。相當於密碼查詢的遍歷描述

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); 

可我也有暗號查詢的最後兩行的遍歷描述(減少訂單)?我後來自己做一個循環..

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

回答

0

您可以提供自己的PathEvaluator是能夠處理分支狀態。最好的資源是Neo4j本身的單元測試,請查看https://github.com/neo4j/neo4j/blob/master/community/kernel/src/test/java/org/neo4j/kernel/impl/traversal/TestBranchState.java

+0

嗯..?我必須能夠將'totalWeight'加入'Path'。從例子中,我不能這樣做。但是,感謝鏈接! – myborobudur 2014-09-12 11:06:55

+0

如果這不起作用,您可以聲明一個「Map 權重」並將其填充到自定義的pathevaluator中。在遍歷結果時,可以通過'weights.get(path)'獲取給定路徑的權重。 – 2014-09-12 11:11:55