Created
June 23, 2011 12:10
-
-
Save 1999/1042431 to your computer and use it in GitHub Desktop.
map-reduce на выборку последних веток комментов
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
map: function(doc) { | |
if (doc.subtype === 'pm') { | |
return; | |
} | |
if (typeof doc.private !== 'undefined' && doc.private === 1) { | |
return; | |
} | |
var cities = []; | |
if (doc.project_num == 0) { | |
cities = [ | |
'Астрахань', | |
'Великий Новгород', | |
'Владивосток', | |
'Екатеринбург', | |
'Ижевск', | |
'Казань', | |
'Кострома', | |
'Краснодар', | |
'Красноярск', | |
'Москва', | |
'Нижний Новгород', | |
'Новосибирск', | |
'Омск', | |
'Пермь', | |
'Псков', | |
'Ростов-на-Дону', | |
'Санкт-Петербург', | |
'Сочи', | |
'Томск', | |
'Уфа', | |
'Калуга', | |
'Эстония', | |
'Тула' | |
]; | |
} else { | |
cities = [ | |
'Москва', | |
'Ростов-на-Дону', | |
'Санкт-Петербург' | |
]; | |
} | |
if (typeof doc.deleted === 'undefined') { | |
if (doc.city != '_') { | |
emit([doc.project_num, doc.city], doc); | |
} else { | |
for (i=0; i<cities.length; i++) { | |
emit([doc.project_num, cities[i]], doc); | |
} | |
} | |
} | |
}, reduce: function(key, values, rereduce) { | |
if (rereduce) { | |
var data = [], meta = [], record, tmp, index, total = []; | |
for (var i in values) { | |
for (j=0; j<values[i].length; j++) { | |
record = values[i][j]; | |
tmp = record[2] + '_' + record[3]; | |
index = meta.indexOf(tmp); | |
if (index === -1) { | |
meta.push(tmp); | |
data.push(record); | |
} else { | |
data[index][1] = Math.max(data[index][1], record[1]); | |
} | |
} | |
} | |
data.sort(function(a, b) { | |
if (a[1] === b[1]) { | |
return 0; | |
} | |
return (a[1] > b[1]) | |
? -1 | |
: 1; | |
}); | |
return data.slice(0, 7); | |
} else { | |
var output = []; | |
for (var i in values) { | |
output.push([values[i]._id, values[i].ts, values[i].belongs, values[i].city]); | |
} | |
return output; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment