在我的Neo4j/SDN項目,我有以下模式:包含子Decision
和Characteristic
實體Neo4j的暗號查詢排序
Decision
實體。
每對兒童Decision
和Characteristic
可以分配一個Value節點。
我已經在3子Decision
節點,例如
childDecision1
childDecision2
childDecision3
和一個Characteristic
:
characterisitc1
我已分配下列值以下對:
childDecision2 + characterisitc1 = Value(Integer 10)
childDecision3 + characterisitc1 = Value(Integer 25)
我正在執行下面的Cypher查詢(帶O RDER BY sortValue88.value ASC
):
MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User)
WHERE id(parentD) = {decisionId}
OPTIONAL MATCH (childD)<-[:SET_FOR]->(sortValue88:Value)-[:SET_ON]->(sortCharacteristic88:Characteristic)
WHERE id(sortCharacteristic88) = 88
WITH ru, u, childD , sortValue88
ORDER BY sortValue88.value ASC SKIP 0 LIMIT 100
RETURN ru, u, childD AS decision, [ (parentD)<-[:DEFINED_BY]-(entity)<-[:COMMENTED_ON]-(comg:CommentGroup)-[:COMMENTED_FOR]->(childD) | {entityId: id(entity), types: labels(entity), totalComments: toInt(comg.totalComments)} ] AS commentGroups, [ (parentD)<-[:DEFINED_BY]-(c1:Criterion)<-[:VOTED_ON]-(vg1:VoteGroup)-[:VOTED_FOR]->(childD) | {criterionId: id(c1), weight: vg1.avgVotesWeight, totalVotes: toInt(vg1.totalVotes)} ] AS weightedCriteria, [ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[:SET_ON]-(v1:Value)-[:SET_FOR]->(childD) | {characteristicId: id(ch1), value: v1.value, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics
至於結果,我有:
childDecision2 (Value = 10)
childDecision3 (Value = 25)
childDecision1 (no value provided)
到目前爲止,一切工作正常。
現在我要去的排序順序改變方向從ASC
到DESC
:
MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User)
WHERE id(parentD) = {decisionId}
OPTIONAL MATCH (childD)<-[:SET_FOR]->(sortValue88:Value)-[:SET_ON]->(sortCharacteristic88:Characteristic)
WHERE id(sortCharacteristic88) = 88
WITH ru, u, childD , sortValue88
ORDER BY sortValue88.value DESC SKIP 0 LIMIT 100
RETURN ru, u, childD AS decision, [ (parentD)<-[:DEFINED_BY]-(entity)<-[:COMMENTED_ON]-(comg:CommentGroup)-[:COMMENTED_FOR]->(childD) | {entityId: id(entity), types: labels(entity), totalComments: toInt(comg.totalComments)} ] AS commentGroups, [ (parentD)<-[:DEFINED_BY]-(c1:Criterion)<-[:VOTED_ON]-(vg1:VoteGroup)-[:VOTED_FOR]->(childD) | {criterionId: id(c1), weight: vg1.avgVotesWeight, totalVotes: toInt(vg1.totalVotes)} ] AS weightedCriteria, [ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[:SET_ON]-(v1:Value)-[:SET_FOR]->(childD) | {characteristicId: id(ch1), value: v1.value, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics
至於結果,我有:
childDecision1 (no value provided)
childDecision3 (Value = 25)
childDecision2 (Value = 10)
現在我不明白爲什麼childDecision1佔據第一位,但我希望childDecision3代替它。
請你幫忙解釋/解決這個問題嗎?
感謝您的回答。有沒有機會重新配置Neo4j以便在進行降序排序時最後返回NULL?我有一個非常沉重的查詢和額外的條件在我看來是不是一個不錯的選擇 – alexanoid
您可以更改'CypherOrderability.compare'函數代碼[https://github.com/neo4j/neo4j/blob/862d62b2f2dc2221e48476ac1f6c93c91d7d6015/community/cypher /cypher-compiler-3.2/src/main/java/org/neo4j/cypher/internal/compiler/v3_2/common/CypherOrderability.java#L105]或是請求新功能的好理由。 –
謝謝!創建了一個github問題https://github.com/neo4j/neo4j/issues/9213 – alexanoid