首页 > 解决方案 > 如果前一个没有任何结果,Cypher Neo4J 中的 UNWIND 语句不会被执行?

问题描述

当我在c_fromand上匹配时,下面的这个请求通常有效c_to

MATCH (u:User {uid: $userId}) 
WITH u UNWIND $statements as statement  
WITH u, statement 
UNWIND statement.conceptsRelations as conceptsRelation 
MATCH (c_from:Concept{name: conceptsRelation.from}) 
MATCH (c_to:Concept{name: conceptsRelation.to}) 
CREATE (c_from)-[:TO {context:conceptsRelation.context,statement:conceptsRelation.statement,user:u.uid,timestamp:conceptsRelation.timestamp, uid:apoc.create.uuid(), gapscan:conceptsRelation.gapscan, weight: conceptsRelation.weight}]->(c_to)  
WITH u, statement 
UNWIND statement.mentionsRelations as mentionsRelation 
MATCH (m_from:Concept{name: mentionsRelation.from}) 
MATCH (m_to:Concept{name: mentionsRelation.to}) return m_from, m_to

但是,只要我没有任何匹配项,查询的最后一部分(最后一个UNWIND)就不会被执行。我通过删除第二个检查它UNWIND,然后第三个再次工作。如:

MATCH (u:User {uid: $userId}) 
WITH u UNWIND $statements as statement  
WITH u, statement 
UNWIND statement.mentionsRelations as mentionsRelation 
MATCH (m_from:Concept{name: mentionsRelation.from}) 
MATCH (m_to:Concept{name: mentionsRelation.to}) return m_from, m_to

以防万一,我的参数是:

{ "userId": "15229100-b20e-11e3-80d3-6150cb20a1b9", "contextNames": [ { "uid": "af8debb0-1f71-11e9-a572-691cc47b060f", "name": "dsfasdf" } ], "statements": [ { "text": "@submit desire", "concepts": [ "desire" ], "mentions": [ "@submit" ], "timestamp": 15482915128250000, "name": "#desire @@submit ", "uid": "2bd1f170-1f73-11e9-a508-0d8a16ad5cf6", "uniqueconcepts": [ "desire" ], "conceptsRelations": [], "mentionsRelations": [ { "from": "desire", "to": "@submit", "context": "af8debb0-1f71-11e9-a572-691cc47b060f", "statement": "2bd1f170-1f73-11e9-a508-0d8a16ad5cf6", "user": "15229100-b20e-11e3-80d3-6150cb20a1b9", "timestamp": 15482915128250000, "uid": "apoc.create.uuid()", "gapscan": "1", "weight": 3 } ], "uniquementions": [ "@submit" ] } ], "timestamp": 15482915128250000 }

这似乎是一种奇怪的不合逻辑的行为。知道为什么会出现吗?谢谢!

标签: neo4jcypher

解决方案


UNWIND 的用途是将列表转换为行 如果您有一个空列表,您将获得零行。

如果它是空的,您可以将一个元素添加到列表中:

UNWIND case when size(list) = 0 then [null] else list end

或者

UNWIND list + null

然后跳过第二部分中的特殊值。


推荐阅读