Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 69043 invoked from network); 20 Nov 2009 15:40:38 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Nov 2009 15:40:38 -0000 Received: (qmail 70514 invoked by uid 500); 20 Nov 2009 15:40:37 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 70464 invoked by uid 500); 20 Nov 2009 15:40:37 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 70454 invoked by uid 99); 20 Nov 2009 15:40:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 Nov 2009 15:40:37 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of matt.goodall@gmail.com designates 209.85.219.216 as permitted sender) Received: from [209.85.219.216] (HELO mail-ew0-f216.google.com) (209.85.219.216) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 Nov 2009 15:40:29 +0000 Received: by ewy8 with SMTP id 8so309437ewy.32 for ; Fri, 20 Nov 2009 07:40:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=sIzFSO9B/tcPs4VEnaGPSKFz5ecrfgcE6ixVOWCTIjw=; b=jK5wP4j754dK0FV5wglW+Hz7Px0BY4bM1PM0KsEYi8W0aFqU+Y6zSkkZk6IdKU5SHT LR7dkY/KWXt4F33ZGC4sln6feBapcCHo/mEQU1dXdVLBR7voWPaWehJk13t0CorGcQ7V NXZC2+wzfk//YGOmmRdGIzEyeiGcZW6FkWaQg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=eMOSNVbfCkOZ4GmdhNeIi+dOOGB0HpW4u9E3YgBWh2JE7R8/iMoD3OLeATzicOlazn TalNtQjLlXAbeb9/VZCNabmdJqkAAESELf8LBCJnDhizttpQJH6IfNun1ykXiJE4hseR m8ffax1279DXntrhBR7OE7TM5ZxYoIB4n8P1s= MIME-Version: 1.0 Received: by 10.213.2.80 with SMTP id 16mr368594ebi.9.1258731608484; Fri, 20 Nov 2009 07:40:08 -0800 (PST) In-Reply-To: <20091120152718.GB6038@seblaptop> References: <20091120112631.49640@gmx.com> <20091120124342.GA6038@seblaptop> <214c385b0911200415p18f6fa92yf69026ecfb7bec89@mail.gmail.com> <20091120152718.GB6038@seblaptop> Date: Fri, 20 Nov 2009 15:40:08 +0000 Message-ID: <214c385b0911200740w63cff392r533ec5fffb557f36@mail.gmail.com> Subject: Re: Newbie :Filtering using complex key and array From: Matt Goodall To: user@couchdb.apache.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org 2009/11/20 Sebastien PASTOR : > Thanks for the quick reply Matt, > > It does sound like a trick because it might generate a much bigger list > than i ll have document in the db (times number of delivery areas). But > i guess as long as the map index =C2=A0is generated only once it will jus= t be > a matter of more space used to store the index right? Correct. CouchDB's views essentially trade disk space for predictable query times (assuming the view is up to date, of course). One thing you can do to reduce the size of views, if necessary, is to emit a null value, i.e. emit(key, null), and add the include_docs=3Dtrue query parameter when GET'ing the view. See http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options. However, that may make things a little slower and put a bit more load on CouchDB. If everything's in the view then CouchDB can jump into the view at the startkey and simply stream the view until the endkey. With include_docs=3Dtrue there's another lookup to get the doc per row returned by the view. I've never measured the performance hit of include_docs, I just know I use it quite a lot and haven't worried too much about it so far ;-). > I guess this is how things should be > handled when using couchDB, not obvious when coming from SQL :) Yep, it's quite a different way of thinking about your data. > At least it works exactly as expected.One little thing that bugs me is th= at the reduce function returns : > > {"rows":[ > {"key":[75010,"indian"],"value":1}, > {"key":[75010,"italian"],"value":2}, > {"key":[75010,"japanese"],"value":1} > ]} As you've probably discovered, you can get three types of count from this view by playing with the view's group_level query param: 1. The number of restaurants of each type in a delivery area, e.g. italian restaurants in 75010. 2. The number of restaurants in a delivery area. 3. The total number of restaurants in your database. > > If i want to return only restaurant type and the number of > restaurants > Something like : > {"key":"italian","value":1}, > > Is it something to could/should be done in Lists or in some way in the > reduce function ? For this you'll need an additional view. The map would probably emit(doc.type, 1) and the reduce would calculate the sum. - Matt > > Thanks again ! > > Sebastien > > On Fri, Nov 20, 2009 at 12:15:59PM +0000, Matt Goodall wrote: >> 2009/11/20 Sebastien PASTOR : >> > Sorry for the previous blank mail ... here is the content : >> > >> > Hi there, >> > >> > Pretty new to couchDB. I ve read a lot about couchdb and finally dive >> > into it with a small project :) >> > I am trying to do a simple thing and i am not sure at all if i am goin= g >> > the right way : >> > >> > my docs look like this : >> > >> > { >> > ??"name":"Pizza Torino", >> > ?? "delivery_areas":[75019,75018,75012,75013,75010], >> > ?? ?? "type":"italian" >> > ?? ?? } >> > >> > ?? ?? I managed to get ??all shops by type and get a reduce function t= o >> > ?? ?? do the sum ( not much i know but still quite an accomplishment f= or >> > ?? ?? me :) ) >> > ?? ?? I then tried to get my result filtered by delivery_areas. as in >> > ?? ?? getting only shop >> > ?? ?? that do delivery in postal code 75019. I just could not ??have >> > ?? ?? anything that >> > ?? ?? worked using startkey and endkey ... is it the way to go or is >> > ?? ?? storing >> > ?? ?? delivery_areas within an array not right ? >> > >> > ?? ?? my last map function looks like this : >> > ?? ?? ?? ?? "getShops" : { >> > ?? ?? ?? ?? ?? ?? ?? ?? ??"map" : "function(doc){ >> > ?? ?? ?? ?? ?? ?? ?? ?? emit([doc.delivery_areas,doc.type],doc.name) >> > ?? ?? ?? ?? ?? ?? ?? ?? } >> > ?? ?? } >> > >> > ??Thanks for pointing me to the right direction >> >> Storing a list of delivery areas is good. The "trick" is to emit >> multiple rows per document, e.g. >> >> =C2=A0 =C2=A0 function(doc) { >> =C2=A0 =C2=A0 =C2=A0 for each (area in doc.delivery_areas) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 emit([area, doc.type], doc.name); >> =C2=A0 =C2=A0 =C2=A0 } >> =C2=A0 =C2=A0 } >> >> You can then query with startkey=3D[75018, null] and endkey=3D[75018, {}= ] >> to get all restaurants in the 75018 area. >> >> The null and {} might look a bit weird at first but it's all to do >> with how CouchDB orders rows in a view. See >> http://wiki.apache.org/couchdb/View_collation#Collation_Specification >> for details. >> >> Hope this helps. >> >> - Matt >