You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Andrew Mee <an...@hydratech.com.au> on 2009/08/24 01:27:05 UTC

Unexpected results using start/end keys on a reduced view.

Hello everybody,
     I have just signed up on this list so please forgive me if this 
topic has been covered.

I am currently using the latest trunk version of CouchDB to experiment 
with it's features and I was experimenting with doing reports and using 
some views to run some data.
My map and reduce functions are:
Map:
function(doc) {
  if(doc.pc && doc.cc){
     emit([doc.cc,doc.pc,doc.task,doc.timestamp],doc.duration);
  }
}

Reduce:
function(keys, values) {
  return sum(values)
}

This produces a result like:

{"rows":[
{"key":["ACT","TXP","",1250825678],"value":5},
{"key":["ACT","TXP","",1250825960],"value":5},
{"key":["ACT","TXP","",1250827265],"value":5},
{"key":["HYD","SPR","blah",1250825378],"value":5},
{"key":["HYD","SPR","blah",1250825660],"value":5},
{"key":["HYD","SPR","blah",1250826965],"value":5},
{"key":["HYD","SPR","Phone",1250791287],"value":1},
{"key":["HYD","SPR","Phone",1250791355],"value":3}
]}

This is to be expected However I get some strange things happening if I 
filter this:
/taskreport?startkey=["HYD","","",1250825660]&endkey=["HYD","\u9999","\u9999",1250826965]&group=true
{"rows":[
{"key":["HYD","SPR","blah",1250825378],"value":5},
{"key":["HYD","SPR","blah",1250825660],"value":5},
{"key":["HYD","SPR","blah",1250826965],"value":5},
{"key":["HYD","SPR","Phone",1250791287],"value":1},
{"key":["HYD","SPR","Phone",1250791355],"value":3}
]}

I would have thought that since my startkey value is greater than the 
first row this should only return the last 4 rows
and in fact if I send specific data for the 2nd and 3rd keys this works:
/taskreport?startkey=["HYD","SPR","blah",1250825660]&endkey=["HYD","SPR","blah",1250826965]&group=true
{"rows":[
{"key":["HYD","SPR","blah",1250825660],"value":5},
{"key":["HYD","SPR","blah",1250826965],"value":5}
]}

I then try and group limit this result so I can group by 
["HYD","SPR","blah"], hence the reason for putting the timestamp field 
at the end of the keys.
/taskreport?startkey=["HYD","SPR","blah",1250825660]&endkey=["HYD","SPR","blah",1250826965]&group=true&group_level=3
{"rows":[
{"key":["HYD","SPR","blah"],"value":10}
]}
Which also works as expected.

So my question is with using start/end keys should it filter on each 
individual part of the key? Should not
startkey=["HYD","","",1250825660]&endkey=["HYD","\u9999","\u9999",1250826965]&group=true 
return
{"rows":[
{"key":["HYD","SPR","blah",1250825660],"value":5},
{"key":["HYD","SPR","blah",1250826965],"value":5}
]}
as well? How could I get this working? BTW If I try a group_level on this
/taskreport?startkey=["HYD",null,null,1250825660]&endkey=["HYD",{},{},1250826965]&group=true&group_level=3
I get :
{"rows":[
{"key":["HYD","SPR","blah"],"value":15},
{"key":["HYD","SPR","Phone"],"value":4}
]}


Thanks, in advance, for any help :)

Regards
Andrew Mee











Re: Unexpected results using start/end keys on a reduced view.

Posted by Paul Davis <pa...@gmail.com>.
On Sun, Aug 23, 2009 at 9:41 PM, Jim Tarvid<ta...@ls.net> wrote:
> Sort of like egrep? It would be nice if the server could do pattern
> matching.
>
> Jim
>

It'd be nice, but it'd either be hugely inefficient or we'd be
reinventing SQL. :)

Paul Davis

Fwd: Unexpected results using start/end keys on a reduced view.

Posted by Jim Tarvid <ta...@ls.net>.
Sort of like egrep? It would be nice if the server could do pattern
matching.

Jim

Re: Unexpected results using start/end keys on a reduced view.

Posted by Andrew Mee <an...@hydratech.com.au>.
Paul,
  thanks for answering so quickly! - that makes sense now.
I guess I will have to have at least 4 views now..
One just on timestamp, another for first level ("HYD") and timestamp, 
another for 1st and 2nd level ("HYD" and "SPR") and timestamp and lastly 
one with 1st, 2nd and 3rd level with timestamp.

While this is a workaround, is there any chance in the future of making 
the filtered on each key an option? e.g. multikey_filter=true

It would mean in my case I could see all the first levels groups that 
have a 3rd level task of "foobar" without have having to write a special 
view for this particular searching/calculation combination I may use.

Just a thought :)

Thanks
Andrew

On 24/08/09 09:49, Paul Davis wrote:
> Andrew,
>
>    
>> I would have thought that since my startkey value is greater than the first
>> row this should only return the last 4 rows
>>      
> ["HYD","","",1250825660]<  ["HYD","SPR","blah",1250825378]
>
> Because ""<  "SPR". The first position that doesn't compare equal is
> going to determine the resulting order.
>
>    
>> So my question is with using start/end keys should it filter on each
>> individual part of the key?
>>      
> Keys are sorted, not filtered. You'll just need to make more views to
> filter as appropriate.
>
> HTH,
> Paul Davis
>    


Re: Unexpected results using start/end keys on a reduced view.

Posted by Paul Davis <pa...@gmail.com>.
Andrew,

> I would have thought that since my startkey value is greater than the first
> row this should only return the last 4 rows

["HYD","","",1250825660] < ["HYD","SPR","blah",1250825378]

Because "" < "SPR". The first position that doesn't compare equal is
going to determine the resulting order.

> So my question is with using start/end keys should it filter on each
> individual part of the key?

Keys are sorted, not filtered. You'll just need to make more views to
filter as appropriate.

HTH,
Paul Davis