2017-02-07 39 views
2

我无法返回与特定属性没有关系的节点。为什么Neo4j无功能在我的查询中工作?

下面就来建立图的情况下,查询:

create ({Name:'Foo1'})-[:T]->({Name:'Bar'})<-[:T{Inactive:true}]-({Name:'Foo2'})<-[:T]-({Name:'Foo3'}) 

我们指定期望的结果更多: 我想所有以某种方式连接到“酒吧”的节点。所以从上面的查询创建的图表我希望得到Foo1和Foo3。

我有那些不使用下列查询“无效”财产有关系麻烦节点:

match (bar)<-[rs*]-(foo) where ID(bar)= **BAR_ID** 
optional match (foo)-[r]->() where r in rs 
with foo, collect(distinct r) as relationships 
where none(rel in relationships where rel.Inactive = true) 
return foo 

现在,您可能会注意到查询与第二位的盛会where子句。它是解决不同问题的一部分,所以请不要专注于该部分,除非它是我的问题的一部分。我感兴趣的是查询无法返回任何内容的原因。看来none()函数的谓词无法正常工作。特别是因为与任何()更换无()将只返回foo2的预期,像这样:

match (bar)<-[rs*]-(foo) where ID(bar)= **BAR_ID** 
optional match (foo)-[r]->() where r in rs 
with foo, collect(distinct r) as relationships 
where any(rel in relationships where rel.Inactive = true) 
return foo 

所以,再一次,问题是为什么使用无()功能ISN查询什么都不回来。首先,我认为它可能与某些没有“非活动”属性但docs的关系有关,它们告诉您缺少的属性评估为false(因为它为空)。至少,这是我的解释,当然可能是错的。

我对这个问题的解决方案并不感兴趣,因为我已经编写了另一个返回Foo1和Foo3的查询。 我想知道为什么查询的问题不起作用。

回答

1

这是工作与Null特异性:

WITH [ null, false ] as values 
RETURN none(value in values where value = true) 

// return Null 

with true as val where not (null = true) 
return val 

// returns nothing 

[]

所以,你需要检查null

match (bar)<-[rs*]-(foo) where ID(bar)= **BAR_ID** 
optional match (foo)-[r]->() where r in rs 
with foo, collect(distinct r) as relationships 
with foo, none(rel in relationships where rel.Inactive = true) as logic 
      where logic is null or logic = true 
return foo 

或者,您可以检查是否Inactive是否存在:

match (bar)<-[rs*]-(foo) where ID(bar)= **BAR_ID** 
optional match (foo)-[r]->() where r in rs 
with foo, collect(distinct r) as relationships 
where none(rel in relationships where exists(rel.Inactive) and rel.Inactive = true) 
return foo 
相关问题