首页 > 解决方案 > Ravendb 按值查询顺序

问题描述

我有带有字段的对象scoreid. from我需要从to中获取一些此类对象from + count,这些对象按字段score降序排列。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。我该怎么做?

标签: javaravendb

解决方案


如果您的索引中有此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---numeric-property

id文件编号吗?因为那时它不必被索引,应该只在查询中添加“where”条件即可工作。参见:https ://ravendb.net/docs/article-page/5.1/java/indexes/querying/basics#example-ii---filtering

顺便说一句,查看https://demo.ravendb.net/中的代码示例在 每个示例中,单击“Java”选项卡以查看示例 Java 代码


推荐阅读