WHERE子句想要收集关系集合,但我只能提供一组关系

时间:2015-07-23 00:03:24

标签: neo4j cypher

我试图在主题节点周围的某个距离查询邻域。扭曲,是我不想跟随具有特定属性的节点的关系。任何查询都需要在数百万个节点和边缘(排除收集路径)的图形上运行。

我想我有一个查询可以做到:

MATCH (topic: attribute)-[r:describedBy|influences*0..2]-(n: attribute) 
WHERE id(topic) IN [239930]
WITH n, r as rels, topic
MATCH (n: attribute)-[r:describedBy|influences]->()
WHERE NOT n.key in ['enrichment', 'classification'] AND r in rels
WITH n, r, collect(r) as rels, topic
MATCH path = shortestpath((topic)-[*..2]-(n))
WHERE extract(rel IN rels(path) | rel) as r WHERE r in rels 
WITH r, extract(n IN nodes(path) | n) as nodes
RETURN count(DISTINCT r), count(DISTINCT nodes)

问题是试图比较关系集合。具体做法是:

Type mismatch: expected Collection<Collection<Relationship>> but was Collection<Relationship> (line 8, column 43 (offset: 369))
"WHERE extract(rel IN rels(path) | rel) in rels "

在多次尝试创建此查询时,我遇到了这个“关系集合”问题的集合。

我怎样才能A:修复collection of collection of relationships问题或B:重写查询,使其返回给定距离的一个或多个节点周围的邻域,而不遵循具有特定属性的节点的关系?

[编辑] 基于@ cybersam的推荐,查询现在是: WHERE NOT n.key in ['enrichment', 'classification'] AND r in rels WITH n, r, collect(r) as rels, topic MATCH path = shortestpath((topic)-[*..2]-(n)) WHERE [r2 IN rels(path) WHERE r2 in rels] WITH r, extract(n IN nodes(path) | n) as nodes RETURN count(DISTINCT r), count(DISTINCT nodes) 问题是这会返回47242个关系和47242个边。看起来它会从路径集合中返回边缘,而不是从上面的子查询返回边缘(其中有大约100103个边缘,或者来自之前的实验)。

1 个答案:

答案 0 :(得分:1)

首先,您在问题查询的这一行中有语法错误:

WHERE extract(rel IN rels(path) | rel) as r WHERE r in rels

您可能实际使用了WITH而不是前导WHERE - 因为这会导致您报告的错误:

WITH extract(rel IN rels(path) | rel) as r WHERE r in rels

现在,假设我在上面是正确的:

  1. extract(rel IN rels(path) | rel)的结果与rels(path)相同,这会使extract()浪费时间。我怀疑你实际上是想提取别的东西。请注意,目前,生成的r是一个关系集合
  2. 由于r是关系的集合,r in rels子句要求rels关系集合的集合。但它实际上是一系列关系,因而是错误。
  3. 解决方案可能就像修复extract()子句一样简单。我不太了解你的问题领域知道如何做到这一点,但希望这足以指出你正确的方向。

    [编辑]

    根据@Gabe的澄清,这可能是相关产品线的正确替代品:

    WHERE all(r IN rels(path) WHERE r in rels)