hi Mickael,
i've had the same problem and used the lists-feature from couch.
my list-function looks like this:
function(head,req) {
function shuffle(ary) {
function randOrd(){ return Math.round(Math.random()) - 0.5; }
ary.sort( randOrd );
}
body={};
eval('body='+req.body);
out = head;
out.rows = [];
if(out.total_rows > out.offset) {
while (row = getRow()) {
out.rows.push(row);
}
}
shuffle(out.rows);
if (body && body.rlimit) {
out.rows = out.rows.slice(-body.rlimit);
}
return toJSON(out) + '\n';
}
maybe you could use a range with startkey and endkey, if the number of
rows is to big. I didn't test the performance with bigger datasets.
Am 28.06.2010 15:29, schrieb mickael.bailly@free.fr:
> 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
|