incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Bonser <mister...@gmail.com>
Subject Re: selecting a random subset of a view
Date Mon, 28 Jun 2010 22:55:25 GMT
A fairly simple solution that will give you pseudo-random-ish selections:

1. GET /music_db/, take note of the update_seq returned.
2. For each random item you want:
    2.1. Pick a random number between 0 and update_seq
    2.2. GET /music_db/_changes?since={number}&limit=1&include_docs=true

A couple of problems with this include the fact that it's not a view
you're getting results from and it will be weighted a bit in favor of
items that have been updated. Still, performance-wise, it's 4 requests
to get your 3 items, and you won't have to handle a large amount of
data being returned by any one request.

On Mon, Jun 28, 2010 at 8:29 AM,  <mickael.bailly@free.fr> wrote:
> Hello couchers,
>
> how would you do to select a random subset of a view result (a simple view with map only).
>
> Example (I don't write the full view response array for clarity)
>
> When called normally, my view returns :
>
> {
> ...
> rows: [
> {id: aa1},
> {id: aa2},
> {id: aa3},
> {id: aa4},
> {id: aa5},
> {id: aa6},
> {id: aa7},
> {id: aa8},
> {id: aa9}
> ]
> }
>
> And I want only three of those rows, randomly chosen. So I launch the magic "get three
random rows" feature, and it gives me :
>
> {
> ...
> rows: [
> {id: aa5},
> {id: aa3},
> {id: aa6}
> ]
> }
>
> The second time I launch the same magic "get three random rows" I got:
> {
> ...
> rows: [
> {id: aa7},
> {id: aa1},
> {id: aa5}
> ]
> }
>
> Thanks for your advices
>
> Mickael
>



-- 
Paul Bonser
http://probablyprogramming.com

Mime
View raw message