如何根据传入关系的数量检索排序的对象列表

How to retrieve a sorted list of objects based on number of incoming relationship

我有一个与其他帐户对象有 "Incoming" 关系的帐户对象。

我需要几个密码查询来检索按每个帐户的关注者数量排序的完整帐户列表。

查询 1:如果帐户一有 200 个关注者,帐户二有 100 个,则帐户一将位于列表顶部。参数 resultSize 将是前 n 个结果的大小。

@Query("...")
List<Account> findSortedAccountByFollowers(int resultSize)

查询 2:除了上述之外,这还需要 2 个参数(maxCount 和 resultSize)。它应该 return 只有那些关注者少于 maxCount 的帐户,例如如果 maxCount 为 200,则 return 关注者少于 200 的帐户按从高到低排序。结果大小由参数 resultSize

定义

这是模型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>;

...

}

[更新]

  1. 实现这个:

    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};
    
  2. 实现这个:

    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};