Denis Butuzov
Denis Butuzov

Reputation: 43

Ravendb query order by value

I have objects with fields score and id. I need to get some amount of this objecs from from to from + count, which ordering by field score in descending order. Inside each equals range I need to get object with specific value of id on top of this range if it exist.

Example:

 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

Specific value id = 7, from = 0, count = 2

Expected result:

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

I write simple query:

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

But this does not take into account the conditions with specific value id. How can i do it?

Upvotes: 4

Views: 160

Answers (1)

Danielle
Danielle

Reputation: 3839

If you have this id field indexed in your index, then you can add a whereGreaterThan condition in your query, to filter for those id's that are greater than '7'

So query would be something like:

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

See documentation link:
https://ravendb.net/docs/article-page/5.1/java/indexes/querying/filtering#where---numeric-property

Is id the document id ? because then it doesn't have to be indexed, should work w/ just adding the 'where' condition to the query. See: https://ravendb.net/docs/article-page/5.1/java/indexes/querying/basics#example-ii---filtering

Btw, check out the code samples in https://demo.ravendb.net/
In each sample, click the 'Java' tab to see sample Java code.

Upvotes: 4

Related Questions