Querying relative information with RavenDB

time to read 2 min | 295 words

Originally posted at 12/3/2010

Let us say that we have the following document:

{
  "Name": "Ayende",
  "LastScore": 239.2,
  "MaxScore": 392.6
}

And we want to find all documents whose last score is within a certain range from their max score. Note that for different queries, the range that I can use may be different.

RavenDB doesn’t offer the option of doing computation in the where clause. Mostly, that is because such computations are going to perform badly unless special care is taken to avoid that. Instead, we are going to create a computed field in the index.

First, we define:

from u in docs.Users
where u.MaxScore
select new { Score = u.LastScore / u.MaxScore } 

This computed field now allows us to query on it very easily. Moreover, when we query, we are still querying over pre-computed data, which is going to blindingly fast.