Neo4j Cypher Pattern理解和收集加入

时间:2017-08-15 19:29:40

标签: neo4j cypher

我有以下Cypher模式理解:

[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)  
WHERE NOT ((ch1)<-[:DEPENDS_ON]-()) AND ch1.type = 'CHILD'
| {characteristicId: toInt(ch1.id),  value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode} ] AS valuedCharacteristics

其中parentDchildD是来自外部查询范围的变量。

现在我需要使用其他节点来改进此模式理解:

(childD)<-[:DEFINED_BY]-(ch2:Characteristic)<-[v2:HAS_VALUE_ON]-(childD)
WHERE NOT ((ch2)<-[:DEPENDS_ON]-()) AND ch2.type = 'OWN'

为了能够从此查询及其值(Characteristics)返回ch2v2)。

请帮助扩展上述查询,以便在同一valuedCharacteristics

下返回此信息

已更新

我已经更新了对以下内容的理解:

[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)<-[:DEFINED_BY]-(ch2:Characteristic)<-[v2:HAS_VALUE_ON]-(childD)  
  WHERE 
    NOT ((ch1)<-[:DEPENDS_ON]-()) AND ch1.type = 'CHILD' AND 
    NOT ((ch2)<-[:DEPENDS_ON]-()) AND ch2.type = 'OWN' | 
  { characteristicId: toInt(ch1.id), value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode, 
    childCharacteristicId: toInt(ch2.id), childValue: v2.value, childAvailable: v2.available, childTotalHistoryValues: v2.totalHistoryValues, childDescription: v2.description, childValueType: ch2.valueType, childVisualMode: ch2.visualMode 
  } 
] AS valuedCharacteristics

但是现在它总是返回空集合。

我可能错了,但我认为原因是以下查询:

[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)<-[:DEFINED_BY]-(ch2:Characteristic)<-[v2:HAS_VALUE_ON]-(childD)  

在某些情况下无法返回结果..例如,当ch2存在且ch1不存在时。

1 个答案:

答案 0 :(得分:0)

这种模式理解应该有效(但除非整个模式匹配,否则它将生成一个空集合):

[(parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)<-[:DEFINED_BY]-(ch2:Characteristic)<-[v2:HAS_VALUE_ON]-(childD)
  WHERE
    NOT ((ch1)<-[:DEPENDS_ON]-()) AND ch1.type = 'CHILD' AND
    NOT ((ch2)<-[:DEPENDS_ON]-()) AND ch2.type = 'OWN' |
  {
    characteristicId: toInt(ch1.id), value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode,
    childCharacteristicId: toInt(ch2.id), childValue: v2.value, childAvailable: v2.available, childTotalHistoryValues: v2.totalHistoryValues, childDescription: v2.description, childValueType: ch2.valueType, childVisualMode: ch2.visualMode
  }
] AS valuedCharacteristics

[增订]

但是,如果您想要始终获取“父”数据,但可以选择获取“子”数据(如果存在),那么您可以切换到使用MATCHOPTIONAL MATCH。例如:

MATCH (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)
WHERE NOT ((ch1)<-[:DEPENDS_ON]-()) AND ch1.type = 'CHILD'
OPTIONAL MATCH (childD)<-[:DEFINED_BY]-(ch2:Characteristic)<-[v2:HAS_VALUE_ON]-(childD)
WHERE NOT ((ch2)<-[:DEPENDS_ON]-()) AND ch2.type = 'OWN'
RETURN COLLECT({
  parent: {characteristicId: toInt(ch1.id), value: v1.value, available: v1.available, totalHistoryValues: v1.totalHistoryValues, description: v1.description, valueType: ch1.valueType, visualMode: ch1.visualMode},
  child: {characteristicId: toInt(ch2.id), value: v2.value, available: v2.available, totalHistoryValues: v2.totalHistoryValues, description: v2.description, valueType: ch2.valueType, visualMode: ch2.visualMode}
}) AS valuedCharacteristics