密码:通过严格交替的一组关系在任意深度匹配节点 [英] Cypher: Matching nodes at arbitrary depth via a strictly alternating set of relations

查看:68
本文介绍了密码:通过严格交替的一组关系在任意深度匹配节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Cypher还是很陌生,但一直在努力寻找方法.我想概括一下以下内容,以便可以在任意深度进行匹配.

I'm quite new to Cypher but struggling to find how to do this. I want to generalise the following so that it will match at an arbitrary depth.

MATCH (:start)-[:a]-()-[:b]-(:end) //Depth1
MATCH (:start)-[:a]-()-[:b]-()-[:a]-()-[:b]-(:end) //Depth2
MATCH (:start)-[:a]-()-[:b]-()-[:a]-()-[:b]-()-[:a]-()-[:b]-(:end) //Depth3
MATCH (:start)-[:a]-()-[:b]-()-[:a]-()-[:b]-()-[:a]-()-[:b]-()-[:a]-()-[:b]-(:end) //Depth4

换句话说,路径必须严格交替通过任何数量的 a-node-b a-node-a 等不起作用.所以根据

In other words, the path needs to pass through any number of a-node-b in strict alternation; a-node-a etc. will not work. So I can't do this, as per Neo4J: find a sub-graph of arbitrary depth with nodes connected by a given set of relations?

MATCH (:start)-[:a|b*]-(:end)

因为它将匹配以下内容:

because that would match things like this:

MATCH (:start)-[:a]-()-[:a]-(:end)
MATCH (:start)-[:b]-()-[:b]-(:end)

有人知道如何解决这个问题吗?如果重要的话,我正在将Cypher与Neo4j 2.x配合使用.

Does anyone know how to solve this? I'm using Cypher with Neo4j 2.x if it matters.

谢谢!

推荐答案

严格的答案是否",这在Cypher中是不可能的.延伸的答案是:可以确定这样的路径是否存在,并可以返回路径两端的节点,但这很慢,而且您将无法沿路径保留一些信息.

The strict answer is no, this is not possible in Cypher. The stretched answer is: it's possible to determine whether such a path exists, and to return the nodes on either end of the path, but it's slow and you won't get to keep some of the information along the path.

MATCH (first) - [:a] -> () - [:b] -> (third)
MERGE (first) - [temp:c] -> (third)
WITH COLLECT(temp) AS temps
MATCH p = (:start) - [:c*] -> (:end)
WITH temps, COLLECT(p) AS ps
FOREACH(temp IN temps)|DELETE temp)
UNWIND ps AS p
RETURN DISTINCT p

这确实需要您具有:c或其他可以在此处用作临时类型的关系.

This does require you to have a :c or other relationship type that you can use as a temp here.

这篇关于密码:通过严格交替的一组关系在任意深度匹配节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆