简体   繁体   English

Cypher for neo4j

[英]Cypher for neo4j

I was trying to create a union of two optional matches (as shown below) but instead of a union I'm getting an intersection of the two. 我试图创建一个两个可选匹配的联合(如下所示),但不是一个联合我得到两个的交集。 How should I change this query to get the required union? 我该如何更改此查询以获取所需的联合?

optional match (a:PA)-[r2:network*2]-(b:PA) where a.last_name='smith'      
And  Not (a:PA)-[:network]-(b:PA)  
optional match (a:PA)-[r3:network*3]-(b:PA) where a.last_name='smith'
And  Not (a:PA)-[:network]-(b:PA) 
return b.first_name, count(r2), count(r3)

This graph database is supposed to mimic a social network.Through the first optional match I have tried to locate second degree connections in the user's(smith's) network and the count of times they appear as a 2nd degree connection in his network. 这个图形数据库应该模仿社交网络。通过第一个可选匹配,我试图找到用户(史密斯)网络中的第二度连接以及它们在网络中作为第二度连接出现的次数。 The second match does the same for the 3rd degree connections. 对于3度连接,第二场比赛也是如此。

But the query is returning the intersection of the 2 optional matches instead of their union ie I'm getting a count of only those names which can be mapped as both a 2 degree as well as a 3 degree connection with respect to the user(smith). 但是查询返回2个可选匹配的交集,而不是它们的联合,即我只得到那些可以映射为用户的2度和3度连接的名称的计数(史密斯) )。

Instead I would like to get the name of all the 2nd degree and 3rd degree connections along with the counts. 相反,我希望得到所有二度和三度连接的名称以及计数。 How shall I modify this query to get the required results? 如何修改此查询以获得所需的结果?

I think in your case the union is not required: 我认为在你的情况下,工会不是必需的:

match path = (a:PA {last_name: 'smith'})-[r2:network*2..3]-(b:PA) 
where not (a:PA)-[:network]-(b:PA)
with b.first_name as name,
     CASE WHEN length(path) = 2 THEN 1 ELSE 0 END as deg2
     CASE WHEN length(path) = 3 THEN 1 ELSE 0 END as deg3
RETURN name, sum(deg2), sum(deg3)

I think what you might be after is something like the following query. 我想你可能会追求的是类似下面的查询。 It does the following: 它执行以下操作:

  1. match smith 匹配史密斯
  2. optionally match smith's 2nd degree PA 可选择匹配史密斯的第二度PA
  3. collect the 2nd degree connections 收集二度连接
  4. optionally match smith's 3rd degree PA 可选择匹配史密斯的3度PA
  5. collect the 3nd degree connections 收集第3度连接
  6. return the collections of 2nd and 3rd degree PA along with the size of each. 返回第二和第三度PA的集合以及每个的大小。

Here is the proposed query 这是建议的查询

match (a:PA)
where a.last_name='smith'      
with a
optional match (a)-[r2:network*2]-(b:PA) 
where Not (a)-[:network]-(b)  
with a, collect(b.first_name) as 2_deg
optional match (a)-[r3:network*3]-(b:PA) 
where Not (a)-[:network]-(b) 
return 2_deg, collect(b.first_name) as 3_deg, size(2_deg) as num_2_deg, size(3_deg) as num_3_deg

If you did want to actually union the two different queries you need a return statement for each half, matching columns and UNION ALL . 如果你确实希望实际联合两个不同的查询,则需要为每一半提供一个return语句,匹配列和UNION ALL Here is a sample but I don't think it is what you are seeking. 这是一个样本,但我认为这不是你想要的。

optional match (a:PA)-[r2:network*2]-(b:PA) 
where a.last_name='smith'      
And  Not (a:PA)-[:network]-(b:PA)  
return b.first_name, count(r2) as num
UNION ALL
optional match (a:PA)-[r3:network*3]-(b:PA) 
where a.last_name='smith'
And  Not (a:PA)-[:network]-(b:PA) 
return b.first_name, count(r3) as num

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM