Warm tip: This article is reproduced from serverfault.com, please click

cypher-NEO4J 查找与集合中的所有节点具有(直接/间接)关系的任何节点

(cypher - NEO4J find any nodes that have relationships (direct/indirect) with all nodes in a set)

发布于 2021-12-14 08:36:49

我是neo4j的新手,所以我可能会在这里犯一些基本错误:

这是我的图表的子集:

在此处输入图像描述

我有3种节点:

  • (蓝色)属性
  • (红色)业务
  • (黄色)促销

attributes可以与 和 有关系promotionsbusinesses (:TAGS)

这是我迄今为止最好的猜测密码查询。

MATCH(a:Attribute)--(b:Business)--(p:Promotion)
WHERE a.name IN ["business", "casual", "happy_hour"]
RETURN a, b, p
UNION
MATCH(a:Attribute)--(p:Promotion)--(b:Business)
WHERE a.name IN ["business", "casual", "happy_hour"]
RETURN a, b, p;

这不是我正在寻找的,它返回与一个或多个相关的promotions attributes

我只想返回与给定属性集中的所有属性具有关系的项。我应该怎么做?promotionsattributes

Questioner
Casey Flynn
Viewed
0
Graphileon 2021-12-14 19:50:57

这应该有效。

// create collection of myTagNodes
WITH ["business", "casual", "happy_hour"] AS myTags
MATCH (myTagNode:TAG) WHERE myTagNode.name IN myTags
WITH COLLECT(myTagNode) AS myTagNodes

// only return promotions for which all 'myTagNodes'are in the 
// (indirectly) connected tags, i.e. through buisnesses or directly
MATCH (p:Promotion)
     WHERE ALL(myTagNode IN myTagNodes 
               WHERE myTagNode IN [(p)<-[*1..2]-(pTagNode:TAG) | pTagNode] 
           )
RETURN p

另请参见 https://neo4j.com/docs/developer-manual/current/cypher/syntax/lists/#cypher-list-comprehensionhttps://neo4j.com/docs/developer-manual/current/cypher/syntax/lists/#cypher-pattern-comprehension