couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gordon Leland Hempton <ghemp...@gmail.com>
Subject Re: selecting a random subset of a view
Date Mon, 28 Jun 2010 16:47:59 GMT
It sounds like couchdb doesn't have a solid solution to this. If this is
central to your application and performance is an issue, maybe you should
explore having a secondary index structure (similar to couchdb-lucene) which
would support fast random querying.

On Mon, Jun 28, 2010 at 9:37 AM, Jan Prieser <j.prieser@hotornot.de> wrote:

> 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
>>
>


-- 
Gordon L. Hempton
http://componentlab.com
http://hempton.com

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message