Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 18 | Comments: 66

filter by tags archive

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.


Comments

Patrick Huizinga

I don't get it.

How can you do a 'where' on a float? Or is this some RavenDB specific interpretation of Where?

note: my knowledge of RavenDB goes no further than what I picked up from this blog.

Ori
Ori

its a typeo no?

GStearns

It looks like he's making sure MaxScore is not 0 or null, which would cause the division to blow up

Bryan

He's also selecting the score, not the document. I don't know how you're supposed to get the document from the score.

I think he rushed the post a bit. :)

Rafal

Nitpick: this is not querying relative information like the title says. This is transforming relative to non-relative and querying only absolute values.

Sekhat

@Rafal yes, but if the document changes, the index gets updated so the value gets recomputed, so it works the result is the same as if you queried relative information.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. RavenDB 3.0 New Stable Release - one day from now
  2. Production postmortem: The industry at large - about one day from now
  3. The insidious cost of allocations - 3 days from now
  4. Buffer allocation strategies: A possible solution - 6 days from now
  5. Buffer allocation strategies: Explaining the solution - 7 days from now

And 3 more posts are pending...

There are posts all the way to Sep 11, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    01 Sep 2015 - The case of the lying configuration file
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats