NEO4J 查找与集合中的所有节点有关系 (direct/indirect) 的任何节点
NEO4J find any nodes that have relationships (direct/indirect) with all nodes in a set
我是 neo4j 的新手,所以我可能在这里犯了一些基本错误:
这是我的图表的一个子集:
我有3种节点:
- (蓝色)属性
- (红色)业务
- (黄色)促销
attributes
可以与 promotions
和 businesses
(: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;
这不是我想要的,它 returns promotions
与 一个或多个 attributes
我只想 return promotions
与给定属性集中的 all attributes
有关系。我应该怎么做?
这应该有效。
// 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-comprehension 和
https://neo4j.com/docs/developer-manual/current/cypher/syntax/lists/#cypher-pattern-comprehension
我是 neo4j 的新手,所以我可能在这里犯了一些基本错误:
这是我的图表的一个子集:
我有3种节点:
- (蓝色)属性
- (红色)业务
- (黄色)促销
attributes
可以与 promotions
和 businesses
(: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;
这不是我想要的,它 returns promotions
与 一个或多个 attributes
我只想 return promotions
与给定属性集中的 all attributes
有关系。我应该怎么做?
这应该有效。
// 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-comprehension 和 https://neo4j.com/docs/developer-manual/current/cypher/syntax/lists/#cypher-pattern-comprehension