Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3789258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:07:43+00:00 2026-05-19T12:07:43+00:00

I have a mapreduce issue with couchdb (both functions shown below): when I run

  • 0

I have a mapreduce issue with couchdb (both functions shown below): when I run it with grouplevel = 2 (exact) I get accurate output:

{"rows":[
 {"key":["2011-01-11","staff-1"],"value":{"total":895.72,"count":2,"services":6,"services_ignored":6,"services_liked":0,"services_disliked":0,"services_disliked_avg":0,"Revise":{"total":275.72,"count":1},"Review":{"total":620,"count":1}}},
 {"key":["2011-01-11","staff-2"],"value":{"total":8461.689999999999,"count":2,"services":41,"services_ignored":37,"services_liked":4,"services_disliked":0,"services_disliked_avg":0,"Revise":{"total":4432.4,"count":1},"Review":{"total":4029.29,"count":1}}},
 {"key":["2011-01-11","staff-3"],"value":{"total":2100.72,"count":1,"services":10,"services_ignored":4,"services_liked":3,"services_disliked":3,"services_disliked_avg":2.3333333333333335,"Revise":{"total":2100.72,"count":1}}},

However, changing to grouplevel=1 so the values for all the different staff keys should be all grouped by date no longer gives accurate output (notice the total is currect but all others are wrong):

{"rows":[
  {"key":["2011-01-11"],"value":{"total":11458.130000000001,"count":2,"services":0,"services_ignored":0,"services_liked":0,"services_disliked":0,"services_disliked_avg":0,"None":{"total":11458.130000000001,"count":2}}},

My only theory is this has something to do with rereduce, which I have not yet learned. Should I explore that option or am I missing something else here?

This is the Map function:

function(doc) {
if(doc.doc_type == 'Feedback') {
    emit([doc.date.split('T')[0], doc.staff_id], doc);
}
}

And this is the Reduce:

function(keys, vals) {
// sum all key points by status: total, count, services (liked, rejected, ignored)
var ret = {
    'total':0,
    'count':0, 
    'services': 0,
    'services_ignored': 0,
    'services_liked': 0,
    'services_disliked': 0,
    'services_disliked_avg': 0,
};

var total_disliked_score = 0;

// handle status
function handle_status(doc) {
    if(!doc.status || doc.status == '' || doc.status == undefined) {
        status = 'None';
    } else if (doc.status == 'Declined') {
        status = 'Rejected';
    } else {
        status = doc.status;
    }
    if(!ret[status]) ret[status] = {'total':0, 'count':0};
    ret[status]['total'] += doc.total;  
    ret[status]['count'] += 1;
};

// handle likes / dislikes
function handle_services(services) {
    ret.services += services.length;
    for(var a in services) {
        if (services[a].user_likes == 10) {
            ret.services_liked += 1;
        } else if (services[a].user_likes >= 1) {
            ret.services_disliked += 1;
            total_disliked_score += services[a].user_likes;
            if (total_disliked_score >= ret.services_disliked) {
                ret.services_disliked_avg = total_disliked_score / ret.services_disliked;
            }
        } else {
            ret.services_ignored += 1;
        }
    }
}

// loop thru docs 
for(var i in vals) {
    // increment the total $
    ret.total += vals[i].total;
    ret.count += 1;

    // update totals and sums for the status of this route
    handle_status(vals[i]);

    // do the likes / dislikes stats
    if(vals[i].groups) {
        for(var ii in vals[i].groups) {
            if(vals[i].groups[ii].services) {
                handle_services(vals[i].groups[ii].services); 
            }
        }
    }

    // handle deleted services
    if(vals[i].hidden_services) {
        if (vals[i].hidden_services) {
            handle_services(vals[i].hidden_services);
        }
    }
}

return ret;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:07:43+00:00Added an answer on May 19, 2026 at 12:07 pm

    This is a classic mistake. Keep in mind that CouchDB reduction happens in several steps, and some of these steps will receive as input the result of other reduction steps. However, your code seems to assume that vals[i] will be an object of the form { "groups": _ , "hidden_services": _ , _ } representing a single document. This code will fail when a rereduce happens, because then vals[i] will be of the form { "count" : _ , "services" : _ , _ } representing the result of a previous reduction step.

    So, for instance, by counting with ret.count += 1, you’re counting the number of intermediary reduction results and not the number of documents.

    One solution is to write a two versions of your reduce code, one to handle the original reduce and another to handle the rereduce steps. You can determine whether a given call is an initial or rereduce call by looking at the third argument (false if initial, true if rereduce).

    Another solution is to have the map function emit a pre-processed value of the same form { "count" : _ , "services" : _ , _ } that is returned by the reduce function, and have the reduce function merely add up the members of those values together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
Since Cassandra doesn't have MapReduce built in yet (I think it's coming in 0.7),
I have been learning the mapreduce algorithm and how it can potentially scale to
Do you know of any python mapreduce ready clustering libraries? I have found some
I have a bunch of large HTML files and I want to run a
Lately, i have reading a lot about MapReduce/Hadoop and think this is where industry
In CouchDB you always have to use map reduce to query results. In MongoDB
I have a script that appends some rows to a table. One of the
I have been trying to understand the MapReduce concept and apply it to my
I have a pipeline that I currently run on a large university computer cluster.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.