Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 29626 invoked from network); 20 Apr 2009 07:51:59 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Apr 2009 07:51:59 -0000 Received: (qmail 74115 invoked by uid 500); 20 Apr 2009 07:51:58 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 74040 invoked by uid 500); 20 Apr 2009 07:51:57 -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 74030 invoked by uid 99); 20 Apr 2009 07:51:57 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Apr 2009 07:51:57 +0000 X-ASF-Spam-Status: No, hits=1.5 required=10.0 tests=SPF_PASS,WEIRD_PORT X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of clairon@gmail.com designates 209.85.218.218 as permitted sender) Received: from [209.85.218.218] (HELO mail-bw0-f218.google.com) (209.85.218.218) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Apr 2009 07:51:51 +0000 Received: by bwz18 with SMTP id 18so1996860bwz.11 for ; Mon, 20 Apr 2009 00:51:29 -0700 (PDT) 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=M3c4vPrX9C89bld+yI0xIj8IiJz2FO/KN8Dehys/vHI=; b=m1SIm7rtSOoEwTQ0dy9W4PzVaOgFgW/rZsQW/Wt0QorRkUm2lX6Rmz+mv7fYGD7MOz WfcTDwk7VoWrYz3hXMvu5ie++4Zdc6letPqnALhhq3aMuou7l0lgqie7cBKJ098XGHUT dp/V9M3s4XO/6V5KeBUJcT+CMl38FNGslJfW4= 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=rI/YZtxlzwMwOZ2/Bsbm3XYhBvkuGNVZ7UxXjOwlDrsl/A2LPpKS6MUCutiawgdAhs dWprgb22RCZpdLdyhMLV+1WkXVv/FUEld2ocyN+mriHzysYybFfYaGtKr5K6CxN7uBq6 BF+ms+Axqffqg9Dpn32+iJxe91qLh04kdszTQ= MIME-Version: 1.0 Received: by 10.103.192.2 with SMTP id u2mr2906269mup.95.1240213889458; Mon, 20 Apr 2009 00:51:29 -0700 (PDT) In-Reply-To: <87864678-231F-4C15-98FB-DB4F60751CD5@gmail.com> References: <4b307fd10904180952jdb6d14ob1d8ed4cb85b1eab@mail.gmail.com> <87864678-231F-4C15-98FB-DB4F60751CD5@gmail.com> Date: Mon, 20 Apr 2009 09:51:29 +0200 Message-ID: <4b307fd10904200051t73c453f9gb374d28b42789090@mail.gmail.com> Subject: Re: group_level and sorting From: Nicolas Clairon To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org First, thank you Jason and Wout for clarifing to me the use of the reduce. It's a behaviour that is not trivial to understand for me. I think the time has come to explain more about my work. I'll try the best I can with my poor written english ability. =3D Creation of documents =3D Say there is 3 users in the application. Each user can write a same document A (by same, I mean same structure). Here's the structure of the DocumentA : DocumentA =3D { _id: ..., _rev: ..., type: "documentA", title; ..., description: ..., tags: ..., // a list of tags date_creation: ... } Now the users create some DocumentA : user1 =3D> { "title": "title1", "description":"a document A", "tags": [], "date_creation": 1 } user2 =3D> { "title": "title1", "description": "another description", "tags": ["tag1"], "date_creation": 2 } user3 =3D> { "title": "title1", "description": "a document A", "tags":["tag1","tag2"], "date_creation": 3 } Note that user1, user2 and user3 create the same document A (same title). They can create more documentA like : { "title": "title2", "description": null, "tags": ["tag3"], "date_creation": 2 } but we don't care about the other. We'are concerned only by the documents A with title1 =3D Reducing to get summary =3D So, we have 3 documents A (with "title1" as title) but I want to display only one to my users. The displayed document will show the most releval informations (the most use description) and all the tags. So I created a reduce function wich summaries all the 3 documents into a single one : reduced_documentA =3D { "title":"title1", "description":"a document A", "tags":["tag1","tag2","tag3"], "date_creation": 1 } Note that the reduced document took the earlier date_creation. =3D Displaying the reduce document =3D The challenge now, is to display the reduced document by tags then by date_creation. That's all. How can I do that ? If I use a map function : emit([doc.tag[t], doc.date_creation], doc.title); i will have duplication. I thought that I can user external process for that. Each time the reduced_document is updated, external process will store the reduced_document as regular document in another db (so that I can query it simplier). What do you think ? It is the way to go ? Thank you for reading me until here. Nicolas On Sun, Apr 19, 2009 at 12:02 PM, Wout Mertens wro= te: > It's not quite clear exactly what you want. I see that you are making you= r > value list unique, so I'm assuming that you want some sort of unique or l= ast > function? > > > I notice a mistake in your code: when creating a reduce function, a > re-reduce should behave in the same way as the regular reduce. The reason= is > that CouchDB doesn't necessarily call re-reduce on your map results. > > Think about it this way: If you have a bunch of values V1 V2 V3 for key K= , > then you can get the combined result either by calling > reduce([K,K,K],[V1,V2,V3],0) or by re-reducing the individual results: > reduce(null,[R1,R2,R3],1). This depends on what your view results look li= ke > internally. > > > That said, what are you interested in? Do you want the last-changed title= or > do you want all titles in order of appearance? > > If the latter, you don't need a reduce at all, you just look at the resul= ts > of your map function in order. > > If the former, you'll have to put your date in the value of the map, and = in > the reduce you'll have to decide which title you want to keep for that se= t > of values. > > Wout. > > PS: I changed the wiki so that it has a common mistakes section in the > ViewSnippets page and I added the reduce mistake. > > On Apr 18, 2009, at 6:52 PM, Nicolas Clairon wrote: > >> Hi all ! >> >> I have a question (wich is a big concerne to me and my project) about >> the group_level option. >> >> I want to display all doc by tag and then sorting them by date. >> >> The map function : >> ----------------------------------- >> function(doc){ >> =A0for(var t in doc.tags){ >> =A0 emit([doc.tags[t], doc.creation_date], doc.title); >> =A0} >> } >> ------------------------------------ >> >> * creation_date is a float since the epoch (ie something like this >> 12423344.003) >> * docs can have the same title >> >> the reduce function: >> ----------------------------------- >> function(key, values, rereduce){ >> =A0 var results =3D []; >> =A0 if(!rereduce){ >> =A0 =A0 =A0 for( var v in values){ >> =A0 =A0 =A0 =A0 =A0 if (results.indexOf( values[v] ) < 0){results.push(v= alues[v]);} >> =A0 =A0 =A0 } >> =A0 } >> =A0 else{ >> =A0 =A0 =A0 for( var i in values){ >> =A0 =A0 =A0 =A0 =A0 for(var e in values[i]){ >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 results.push(values[i][e]); >> =A0 =A0 =A0 =A0 =A0 } >> =A0 =A0 =A0 } >> =A0 } >> =A0 return results; >> } >> ----------------------------------- >> >> $ curl >> http://localhost:5984/db/_design/foo/_view/by_tag_sort_by_date?reduce=3D= false >> >> returns : >> >> >> {"id":"8075ba2ef7418f4d6c9a3e89be83acd8","key":["tag1",1239361935.000004= ],"value":"title2"}, >> >> {"id":"8d9132318a6c34c646e9e2cd43823ffa","key":["tag1",1239794744.000002= ],"value":"title1"}, >> >> {"id":"f49a28ffd2118298c1be7440ec4556fa","key":["tag2",1239794744.000002= ],"value":"title1"}, >> >> this is ok because title1 is newer than title2. But now, I want all >> displayed by tag so I use the group_level : >> >> $ curl >> http://localhost:5984/db/_design/foo/_view/by_tag_sort_by_date?group_lev= el=3D1 >> >> {"key":["tag1"],"value":["title1","title2"]}, >> {"key":["tag2"],"value":["title1"]}, >> >> I have all titles by tag but the docs is not sorted by date anymore... >> >> Does group_level keeps the absolute sorting ? Does the sort break anyway= ? >> >> Thanks, >> >> Nicolas > >