Neo4j Cypher-使用可变电平路径时的终止条件 [英] Neo4j Cypher - Terminating conditions when using variable level paths

查看:136
本文介绍了Neo4j Cypher-使用可变电平路径时的终止条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接列表,该列表以如下方式循环建模:

I have a linked list that is modelled in a circular fashion like so:

(u:User)
-[:LINK]->(a:NODELINK {linkId: 'aa'})
-[:LINK]->(b:NODELINK {linkId: 'bb'})
-[:LINK]->(c:NODELINK {linkId: 'cc'})
-[:LINK]->(d:NODELINK {linkId: 'dd'})
-[:LINK]->(u)

当我从节点(b:NODELINK {linkId: 'bb'})开始查询它时,我想匹配所有节点,直到到达列表的末尾. (用户节点)

When I query it starting at node (b:NODELINK {linkId: 'bb'}) I would like to match all nodes until I get to the end/start of the list. (The User node)

当我运行以下查询时:

MATCH (u:USER)
WITH u
MATCH (nl:NODELINK)-[:LINK*]->(m)
WHERE nl.linkId = 'bb' AND m <> u
RETURN m

它将返回以下节点

(3:NODELINK {linkId:'cc'})
(4:NODELINK {linkId:'dd'})
(1:NODELINK {linkId:'aa'})
(2:NODELINK {linkId:'bb'})

您可以看到我的查询环绕并且从列表的开头开始返回节点.我想在User节点处终止,只返回列表末尾之前的节点

As you can see my query wraps around and starts returning nodes from the start of the list. I would like to terminate at the User node and only return the nodes before the end of the list

(3:NODELINK {linkId:'cc'})
(4:NODELINK {linkId:'dd'})

我创建了一个图形,其中在此处演示了问题

I have created a graph that demonstrates the issue here

如何查询以便从感兴趣的节点链接开始并在到达用户节点之前返回所有节点?

How do I query so as to start at the node link of interest and returns all nodes before it reaches the User node?

推荐答案

您的WHERE子句限制路径中的第一个和最后一个节点,但不限制中间节点,因此,是的,查询可以匹配包含用户的路径节点.

Your WHERE clause limits the first and last node in the path, but not the intermediate ones, so yes, the query can match paths that include the user node.

与其匹配许多路径并仅返回终端节点,不如尝试匹配从"cc"节点到用户节点的一条路径,然后返回该路径中的所有节点(如果您不希望用户,则返回最后一个节点除外)节点).

Instead of matching many paths and returning only the end nodes, try matching one path from the 'cc' node to the user node, and return all nodes in that path (except the last one if you don't want the user node).

MATCH path=(nl:NODELINK {linkId:'cc'})-[:LINK*]->(u:USER)
RETURN nodes(path)

RETURN nodes(path)[..-1]

如果要返回除最后一个节点以外的所有节点.

if you want to return all the nodes except the last one.

这篇关于Neo4j Cypher-使用可变电平路径时的终止条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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