[英]Neo4j Cypher Pattern Comprehension and collection join
I have a following Cypher Pattern Comprehension: 我有以下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
where parentD
and childD
are the variables from outer query scope. 其中parentD
和childD
是来自外部查询范围的变量。
Right now I need to improve this Pattern Comprehension with additional nodes: 现在,我需要使用其他节点来改进此模式理解:
(childD)<-[:DEFINED_BY]-(ch2:Characteristic)<-[v2:HAS_VALUE_ON]-(childD)
WHERE NOT ((ch2)<-[:DEPENDS_ON]-()) AND ch2.type = 'OWN'
in order to be able also return Characteristics
( ch2
) from this query and their values( v2
). 为了也能够从该查询返回其Characteristics
( ch2
)及其值( v2
)。
Please help to extend the mentioned query in order to return this information under the same valuedCharacteristics
请帮助扩展上述查询,以便以相同的valuedCharacteristics
返回此信息。
UPDATED 更新
I have updated comprehension to the following: 我已将理解更新为以下内容:
[ (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
but right now it always returns empty collection. 但现在它总是返回空集合。
I may be wrong but I think the reason is that the following query: 我可能是错的,但我认为原因是以下查询:
[ (parentD)<-[:DEFINED_BY]-(ch1:Characteristic)<-[v1:HAS_VALUE_ON]-(childD)<-[:DEFINED_BY]-(ch2:Characteristic)<-[v2:HAS_VALUE_ON]-(childD)
can't return the result in some cases.. for example when ch2
is present and ch1
is absent. 在某些情况下无法返回结果。例如,当ch2
存在且ch1
不存在时。
This pattern comprehension should work (but it will generate an empty collection unless the entire pattern is matched): 这种模式理解应该可以工作(但是除非整个模式都匹配,否则它将生成一个空集合):
[(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
[UPDATED] [更新]
However, if you want to always get the "parent" data, but optionally get the "child" data (if it exists), then you could switch to using MATCH
and OPTIONAL MATCH
. 但是,如果您希望始终获取“父”数据,但又可以选择获取“子”数据(如果存在),则可以切换为使用MATCH
和OPTIONAL MATCH
。 For example: 例如:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.