【问题标题】:WHERE clause wants collection of collections of relationships, but I can only provide a collection of relationshipsWHERE 子句想要关系集合的集合,但我只能提供关系集合
【发布时间】:2015-07-23 00:03:23
【问题描述】:

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

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

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)。

【问题讨论】:

    标签: neo4j cypher


    【解决方案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关系集合的集合。但它实际上是关系的集合,因此会出现错误。

    解决方案可能就像修复 extract() 子句一样简单。我对您的问题领域了解得不够多,不知道该怎么做,但希望这足以为您指明正确的方向。

    [编辑]

    根据@Gabe 的澄清,这可能是相关行的正确替换:

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

    【讨论】:

    • 双行应该是WHERE extract(rel IN rels(path) | rel) in rels,或者,简单来说,WHERE rels(path) IN rels。这仍然会产生“类型不匹配:预期的 Collection> 但是是 Collection”错误。不幸的是,`WHERE extract(rels(path)) IN rels" 产生错误:"Unknown function 'extract'"
    • 请参阅Edit 我的回答。
    猜你喜欢
    • 2017-05-11
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多