librelist archives

« back to archive

Friendly find distinct

Friendly find distinct

From:
Newman Hunter
Date:
2010-01-30 @ 22:26
Hi all,

I've just gotten started using friendly for a small side project, and
I'm really impressed so far. But I am having trouble implementing a
distinct type query.

Here's an example of the model I'm using:

class Game
  include Friendly::Document

  attribute :user_id, Friendly::UUID
  attribute :friendly_team, String
  attribute :opposing_team, String
  attribute :winning_side, Integer
  attribute :force, Integer

  indexes :user_id
  indexes :force, :user_id

  named_scope :leaders, :order! => :force.desc, :limit! => 25,
:distinct => :user_id

I'm calling Game.leaders.all in the hopes that I'll find the top 25
games by force with duplicate user_ids filtered (so we only consider
the best game for that user). I found the :distinct filter from the
sequel documentation, however when running that query I get
Friendly::MissingIndex exception. I think it should be using the
:force, :user_id index, which is created and specified.

Thanks for taking the time to create this framework and the time to
read my question

-- 
Newman Scott Hunter
scott@driftlogic.net

Re: [friendly] Friendly find distinct

From:
James Golick
Date:
2010-01-31 @ 04:47
Hi Newman,

The idea with Friendly is that you'd pre-compute aggregate queries. The
problem is that if you have a lot of data or are sharding, aggregates aren't
going to be practical or even possible.

At this point, there is probably a way to hack it in because sequel supports
it. But, at a later date, that support may disappear.

I'd recommend either pre-computing the aggregates on save of your model
(maybe in a background job), or use a plain old mysql table for that.

- James

On Sat, Jan 30, 2010 at 2:26 PM, Newman Hunter <scott@driftlogic.net> wrote:

> Hi all,
>
> I've just gotten started using friendly for a small side project, and
> I'm really impressed so far. But I am having trouble implementing a
> distinct type query.
>
> Here's an example of the model I'm using:
>
> class Game
>  include Friendly::Document
>
>  attribute :user_id, Friendly::UUID
>  attribute :friendly_team, String
>  attribute :opposing_team, String
>  attribute :winning_side, Integer
>  attribute :force, Integer
>
>  indexes :user_id
>  indexes :force, :user_id
>
>  named_scope :leaders, :order! => :force.desc, :limit! => 25,
> :distinct => :user_id
>
> I'm calling Game.leaders.all in the hopes that I'll find the top 25
> games by force with duplicate user_ids filtered (so we only consider
> the best game for that user). I found the :distinct filter from the
> sequel documentation, however when running that query I get
> Friendly::MissingIndex exception. I think it should be using the
> :force, :user_id index, which is created and specified.
>
> Thanks for taking the time to create this framework and the time to
> read my question
>
> --
> Newman Scott Hunter
> scott@driftlogic.net
>