You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Maurício Szabo <ma...@gmail.com> on 2009/10/21 13:16:42 UTC

Finding with two fields

Hello, list.

I have a problem on an aplication I'm trying to migrate to CouchDB: I have a
series of documents like:

{
  type: "Period",
  start: "2009-10-12",
  end: "2009-12-12"
}

Problem is, I need to check on my application if there is a valid period. I
need to search if:

(start < today) and (today < end)

How can I do this in CouchDB? I've tried creating views, but I'm stuck (I'm
emitting [doc.start, doc.end] as an index, but I still cannot find a way to
search this).

Thanks in advance,
Mauricio.

Re: Finding with two fields

Posted by Nathan Stott <nr...@gmail.com>.
Brian,

good points

On Thu, Oct 22, 2009 at 6:54 AM, Brian Candler <B....@pobox.com> wrote:

> On Wed, Oct 21, 2009 at 08:21:50AM -0500, Nathan Stott wrote:
> > You have two options.  1) use couchdb-lucene
> > 2) emit multiple rows for each document.  something like this:
> > function(doc) {
> >     var iterDate = doc.startDate;
> >     var endDate = doc.endDate;
> >     while (iterDate < endDate) {
> >         emit(iterDate);
> >         iterDate.setDay(iterDate.getDay() + 1) // make this as
> fine-grained
> > as you like
> >     }
> > }
>
> Option (3): have views for startDate and endDate. Query the first view for
> all docs with startDate < today, query the second view for all docs with
> today < endDate, then intersect on the client.
>
> Option (4): emit([startDate, endDate]), query for startDate < today, then
> use a _list function to filter out matches with an endDate out of range.
>
> These two options can get expensive with very large numbers of documents.
> Option (2) above may be better, but can also get expensive with very wide
> ranges between startDate and endDate.
>
> A compromise is to take option (2) with large-grained buckets to get a set
> of possible candidates, and then post-filter in the client or in _list.
>

Re: Finding with two fields

Posted by Brian Candler <B....@pobox.com>.
On Wed, Oct 21, 2009 at 08:21:50AM -0500, Nathan Stott wrote:
> You have two options.  1) use couchdb-lucene
> 2) emit multiple rows for each document.  something like this:
> function(doc) {
>     var iterDate = doc.startDate;
>     var endDate = doc.endDate;
>     while (iterDate < endDate) {
>         emit(iterDate);
>         iterDate.setDay(iterDate.getDay() + 1) // make this as fine-grained
> as you like
>     }
> }

Option (3): have views for startDate and endDate. Query the first view for
all docs with startDate < today, query the second view for all docs with
today < endDate, then intersect on the client.

Option (4): emit([startDate, endDate]), query for startDate < today, then
use a _list function to filter out matches with an endDate out of range.

These two options can get expensive with very large numbers of documents.
Option (2) above may be better, but can also get expensive with very wide
ranges between startDate and endDate.

A compromise is to take option (2) with large-grained buckets to get a set
of possible candidates, and then post-filter in the client or in _list.

Re: Finding with two fields

Posted by Nathan Stott <nr...@gmail.com>.
You have two options.  1) use couchdb-lucene
2) emit multiple rows for each document.  something like this:
function(doc) {
    var iterDate = doc.startDate;
    var endDate = doc.endDate;
    while (iterDate < endDate) {
        emit(iterDate);
        iterDate.setDay(iterDate.getDay() + 1) // make this as fine-grained
as you like
    }
}

On Wed, Oct 21, 2009 at 6:31 AM, Maurício Szabo <ma...@gmail.com>wrote:

> Ok, but how? How is it compared? I've already tried with a lot of
> combinations, and none seens to work.
>
> On Wed, Oct 21, 2009 at 9:27 AM, Cairo Noleto <ca...@gmail.com>
> wrote:
>
> > You can use startkey and endkey to find this values
> >
> > On Wed, Oct 21, 2009 at 08:16, Maurício Szabo <mauricio.szabo@gmail.com
> > >wrote:
> >
> > > Hello, list.
> > >
> > > I have a problem on an aplication I'm trying to migrate to CouchDB: I
> > have
> > > a
> > > series of documents like:
> > >
> > > {
> > >  type: "Period",
> > >  start: "2009-10-12",
> > >  end: "2009-12-12"
> > > }
> > >
> > > Problem is, I need to check on my application if there is a valid
> period.
> > I
> > > need to search if:
> > >
> > > (start < today) and (today < end)
> > >
> > > How can I do this in CouchDB? I've tried creating views, but I'm stuck
> > (I'm
> > > emitting [doc.start, doc.end] as an index, but I still cannot find a
> way
> > to
> > > search this).
> > >
> > > Thanks in advance,
> > > Mauricio.
> > >
> >
> >
> >
> > --
> > Cairo Noleto 
> > Visits http://www.caironoleto.com/
> >
>

Re: Finding with two fields

Posted by Maurício Szabo <ma...@gmail.com>.
Ok, but how? How is it compared? I've already tried with a lot of
combinations, and none seens to work.

On Wed, Oct 21, 2009 at 9:27 AM, Cairo Noleto <ca...@gmail.com> wrote:

> You can use startkey and endkey to find this values
>
> On Wed, Oct 21, 2009 at 08:16, Maurício Szabo <mauricio.szabo@gmail.com
> >wrote:
>
> > Hello, list.
> >
> > I have a problem on an aplication I'm trying to migrate to CouchDB: I
> have
> > a
> > series of documents like:
> >
> > {
> >  type: "Period",
> >  start: "2009-10-12",
> >  end: "2009-12-12"
> > }
> >
> > Problem is, I need to check on my application if there is a valid period.
> I
> > need to search if:
> >
> > (start < today) and (today < end)
> >
> > How can I do this in CouchDB? I've tried creating views, but I'm stuck
> (I'm
> > emitting [doc.start, doc.end] as an index, but I still cannot find a way
> to
> > search this).
> >
> > Thanks in advance,
> > Mauricio.
> >
>
>
>
> --
> Cairo Noleto 
> Visits http://www.caironoleto.com/
>

Re: Finding with two fields

Posted by Cairo Noleto <ca...@gmail.com>.
You can use startkey and endkey to find this values

On Wed, Oct 21, 2009 at 08:16, Maurício Szabo <ma...@gmail.com>wrote:

> Hello, list.
>
> I have a problem on an aplication I'm trying to migrate to CouchDB: I have
> a
> series of documents like:
>
> {
>  type: "Period",
>  start: "2009-10-12",
>  end: "2009-12-12"
> }
>
> Problem is, I need to check on my application if there is a valid period. I
> need to search if:
>
> (start < today) and (today < end)
>
> How can I do this in CouchDB? I've tried creating views, but I'm stuck (I'm
> emitting [doc.start, doc.end] as an index, but I still cannot find a way to
> search this).
>
> Thanks in advance,
> Mauricio.
>



-- 
Cairo Noleto 
Visits http://www.caironoleto.com/