2016-05-01 43 views
0

我正在尝试编写一个查询,以查找基于共同朋友和兴趣的Neo4j数据库中的潜在朋友。Neo4j - 如果发现几个结果,则使用替代匹配完成查询

我不想张贴整个查询(作业的一部分),但是这是最重要的组成部分

MATCH (me:User {firstname: "Name"}), (me)-[:FRIEND]->(friend:User)<-[:FRIEND]-(potential:User), (me)-[:MEMBER]->(i:Interest) 
WHERE NOT (potential)-[:FRIEND]->(me) 
WITH COLLECT(DISTINCT potential) AS potentialFriends, 
    COLLECT(DISTINCT friend) AS friends, 
    COLLECT(i) as interests 

UNWIND potentialFriends AS potential 

/* 
@HANDLING_FINDINGS 
Here I count common friends, interests and try to find relationships between 
potential friends too -- hence the collect/unwind 
*/ 

RETURN potential, 
     commonFriends, 
     commonInterests, 
     (commonFriends+commonInterests) as totalPotential 
ORDER BY totalPotential DESC 
LIMIT 10 

在部分@HANDLING_FINDINGS我用的是发现潜在的朋友找到每个之间的关系并计算他们的潜力(即共享朋友和共同利益的总和),然后按潜力排序。

问题是,可能有没有朋友的用户,我也想推荐一些朋友。

我的问题 - 我可以以某种方式插入几个随机用户到“潜在”的调查结果,如果他们的计数低于10,以便每个人都得到一个建议?

我已经试过这样的事情

... 
UNWIND potentialFriends AS potential 
CASE 
WHEN (count(potential) < 10) 
... 

但是,一旦出现了一个错误,因为它击中的情况下开始。我认为这种情况只能用于像return这样的命令的一部分? (也许只是返回)

编辑与第二个相关的问题: 我已经想匹配的所有用户,然后基于共同的朋友/ interestes排名他们,但不会在整个DB搜索精耕细作?

回答

1

A CASE表达式可用于任何需要值的地方,但不能用作完整的子句。

对于你的主要问题,你可以把一个WITH条款喜欢你现有的WITHUNWIND子句之间的以下内容:

WITH friends, interests, 
    CASE WHEN SIZE(potentialFriends) < 10 THEN {randomFriends} ELSE potentialFriends END AS potentialFriends 

如果potentialFriends集合的大小小于10时,CASE表达将{randomFriends}parameter的值分配给potentialFriends

至于你的第二个问题,是的,这将是昂贵的。

相关问题