我有一个与其他帐户对象具有“传入”关系的帐户对象。
我需要一些cypher查询来检索按每个帐户的关注者数量排序的完整帐户列表。
查询1:如果,帐户一有200个关注者,帐户二有100个,则帐户一将位于列表的顶部。参数resultSize将是前n个结果的大小。
@Query("...")
List<Account> findSortedAccountByFollowers(int resultSize)
查询2:除上述外,还需要2个参数(maxCount和resultSize)。它应仅返回那些关注者少于maxCount的帐户,例如如果maxCount为200,则返回那些跟随者减去200的帐户,按高到低排序。结果大小由参数resultSize
定义这是Model Class
@NodeEntity
public class Account implements Serializable{
@GraphId
private Long id;
...
@Fetch
@RelatedTo(type="follows",
direction= Direction.OUTGOING,
elementClass = Account.class)
private Set<Account> following = new HashSet<Account>;
@Fetch
@RelatedTo(type="follows",
direction= Direction.INCOMING,
elementClass = Account.class)
private Set<Account> followers = new HashSet<Account>;
...
}
答案 0 :(得分:0)
[增订]
要实现这一点:
List<Account> findSortedAccountByFollowers(int resultSize)
此查询应该有效:
MATCH (a:Account)<-[:follows]-(b:Account)
WITH a, COLLECT(b) AS bs
ORDER BY SIZE(bs) DESC
RETURN a
LIMIT {0};
要实现这一点:
List<Account> findCappedSortedAccountByFollowers(int maxCount, int resultSize)
此查询应该有效:
MATCH (a:Account)<-[:follows]-(b:Account)
WITH a, COLLECT(b) AS bs
WHERE SIZE(bs) <= {0}
RETURN a
ORDER BY SIZE(bs) DESC
LIMIT {1};