[英]How to exclude nodes connected to a specific node from Neo4j Cypher search results
I need to perform a Neo4J Cypher query where I exclude the nodes from the result that are connected to a specific node. 我需要执行Neo4J Cypher查询,从结果中排除连接到特定节点的节点。
I'm using that query below, but it's very slow (several seconds). 我在下面使用该查询,但是它非常慢(几秒钟)。
What I do below is to first get the node (of the Context type), which the other nodes should not be connected to. 我下面要做的是首先获取(上下文类型的)节点,其他节点不应该连接到该节点。
Then I make the next query, finding the nodes, created by the same user but in another context " help
" and exclude those of them that are also connected to the " private
" context. 然后,我进行下一个查询,查找由同一用户在另一个上下文“ help
”中创建的节点,并排除那些也连接到“ private
”上下文的节点。
MATCH (u:User{uid:'6dbe5450-852d-11e4-9c48-b552fc8c2b90'}),
(ctxa:Context{name:"private"}), ctxa-[:BY]->u
WITH ctxa,u MATCH (s:Statement), (ctx:Context{name:"help"}), ctx-[:BY]->u,
s-[:IN]->ctx, s-[:BY]->u
WHERE NOT s-[:IN]->ctxa
RETURN DISTINCT s.uid ORDER BY s.timestamp DESC;
Anyone has a better idea? 有人有更好的主意吗?
UPDATE: I also tried this: 更新:我也尝试过:
MATCH (u:User{uid:'b9745f70-a13f-11e3-98c5-476729c16049'}), (s:Statement),
(ctx:Context{name:"private"}), ctx-[:BY]->u, s-[:IN]->ctx, s-[:BY]->u
WITH s AS sta, u MATCH (s:Statement), (ctx:Context{name:"help"}),
ctx-[:BY]->u, s-[:IN]->ctx, s-[rel:BY]->u WHERE s <> sta RETURN DISTINCT s;
Found that that using DISTINCT
in the query really improved the speed: 发现在查询中使用DISTINCT
确实提高了速度:
MATCH (u:User{uid:'b9745f70-a13f-11e3-98c5-476729c16049'}),
(ctxa:Context{name:"private"}), ctxa-[:BY]->u
WITH DISTINCT ctxa,u
MATCH (s:Statement), s-[rel:BY]->u, (ctx:Context{name:"bodypractices"}),
ctx-[:BY]->u, s-[:IN]->ctx
WHERE NOT s-[:IN]->ctxa
RETURN DISTINCT s;
Basically the problem was that I was not using DISTINCT
with the first WITH
clause and because of the structure of my database (or maybe for another reason) I was getting a lot of rows for ctxa and then each was checked in the WHERE
statement, slowing everything down. 基本上,问题在于我没有在第一个WITH
子句中使用DISTINCT
,并且由于数据库的结构(或者可能出于其他原因),我为ctxa获取了很多行,然后在WHERE
语句中对每个行进行了检查,从而减慢了速度一切都下来了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.