Neo4j收集功能错误?

时间:2015-04-09 21:39:37

标签: neo4j

我正在运行以下查询,用于比较两个集合节点set1和set2。 set2中的所有节点都在set1中,我想识别set1中不在set2中的所有节点。但是,查询返回一组节点,其中包含set1中的一些节点。我在v2.1.7上运行此查询。建议?

查询:

MATCH p=(a:ObjectConcept{sctid:233604007})<-[:ISA*]-(b:ObjectConcept)
with  nodes(p) as set1, p
MATCH q=(a:ObjectConcept{sctid:34020007})<-[:ISA*]-(b:ObjectConcept)
with  nodes(q) as set2,set1, p
WHERE ALL(x in set2 WHERE NOT x in set1) 
with  nodes(p) as pneumo
UNWIND pneumo AS pneumolist
RETURN distinct pneumolist.FSN,pneumolist.sctid

替代查询,结果相同: 查询:

MATCH p=(a:ObjectConcept{sctid:233604007})<-[:ISA*]-(b:ObjectConcept)
with  nodes(p) as set1, p
MATCH q=(a:ObjectConcept{sctid:34020007})<-[:ISA*]-(b:ObjectConcept)
with  nodes(q) as set2,set1, p
WHERE NONE(x in set2 WHERE x in set1) 
with  nodes(p) as pneumo
UNWIND pneumo AS pneumolist
RETURN distinct pneumolist.FSN,pneumolist.sctid

2 个答案:

答案 0 :(得分:0)

您的匹配项不会像您预期的那样只返回一行但是很多行, 并且您在那些许多行组合的叉积之间进行了比较。您可能希望首先使用unwind + collect(distinct)

的组合为两个子树中的每一个创建一个集合

下面的代码不会那么快,因为cypher内部还没有Set概念。

试试这个

MATCH p=(a:ObjectConcept{sctid:233604007})<-[:ISA*]-(b:ObjectConcept)
unwind nodes(p) as n
with collect(distinct n) as set1
MATCH q=(a:ObjectConcept{sctid:34020007})<-[:ISA*]-(b:ObjectConcept)
unwind nodes(q) as m
with collect(distinct m) as set2
WHERE NONE(x in set2 WHERE x in set1)
UNWIND set1 AS pneumolist
RETURN distinct pneumolist.FSN,pneumolist.sctid

答案 1 :(得分:0)

以下查询成功,并解决了Michael关于跨产品的讨论(上图)。

MATCH p=(a:ObjectConcept{sctid:233604007})<-[:ISA*]-(b:ObjectConcept)
with  distinct nodes(p) as set1
UNWIND set1 as x1
with collect(DISTINCT x1) as set11
MATCH q=(a:ObjectConcept{sctid:34020007})<-[:ISA*]-(b:ObjectConcept)
with  distinct nodes(q) as set2,set11
UNWIND set2 as x2
with collect(distinct x2) as set22,set11
with REDUCE(pneumo=[],x in set11|case when x in set22 then pneumo else pneumo 
+ [x] END) AS pneumo
return pneumo