繁体   English   中英

Neo4j Cypher查询排序顺序

[英]Neo4j Cypher query sort order

在我的Neo4j / SDN项目中,我具有以下模式:

Decision实体包含子DecisionCharacteristic的实体。

每对子DecisionCharacteristic都可以分配一个值节点。

例如,我创建了3个子Decision节点

childDecision1
childDecision2
childDecision3

和一个Characteristic

特征1

我已将以下值分配给以下对:

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代替那里。

您能帮忙解释/解决此问题吗?

因为: When sorting the result set, null will always come at the end of the result set for ascending sorting, and first when doing descending sort.

因此,您需要知道排序的最小可能值。 例如,如果所有值都不小于零

WITH [1, 0, 2, NULL, 4] AS CS
UNWIND RANGE(0, size(CS)-1) as i
RETURN i, 
       CASE WHEN CS[i] IS NULL THEN -1 ELSE CS[i] END AS sortValue
ORDER BY sortValue DESC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM