ravendb查询按值排序

epfja78i  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(219)

我有带字段的对象 score 以及 id . 我需要从他们那里弄点东西 fromfrom + count ,按字段排序 score 按降序排列。在每个equals范围内,我需要得到具有特定值的对象 id 在这个范围的顶部。
例子:

1. score = 10, id = 5
 2. score = 20, id = 6
 3. score = 20, id = 7
 4. score = 20, id = 8
 5. score = 30, id = 9

特定值 id = 7, from = 0, count = 2
预期结果:

1. score = 30, id = 9
 2. score = 20, id = 7  <--- my specific value on top of equal range, where score = 20

我写了一个简单的查询:

final List<SomeClass> players = session.query(SomeClass.class, SomeIndexClass.class)
            .orderByDescending("score", OrderingType.LONG)
            .skip(from)
            .take(count)
            .toList();

但这并没有考虑具有特定价值的条件 id . 我该怎么做?

ua4mk5z4

ua4mk5z41#

如果你有这个 id 在索引中索引字段,然后可以添加 whereGreaterThan 条件,以筛选大于“7”的id
所以查询应该是这样的:

final List<SomeClass> players = session.query(SomeClass.class, SomeIndexClass.class)
            .whereGreaterThan("id", 7)
            .orderByDescending("score", OrderingType.LONG)
            .skip(from)
            .take(count)
            .toList();

请参阅文档链接:
https://ravendb.net/docs/article-page/5.1/java/indexes/querying/filtering#where---数值特性
id 文档id?因为这样它就不必被索引,只要在查询中添加“where”条件就可以了。请参见:https://ravendb.net/docs/article-page/5.1/java/indexes/querying/basics#example-ii---过滤
顺便说一句,请查看中的代码示例https://demo.ravendb.net/
在每个示例中,单击“java”选项卡以查看示例java代码。

相关问题