My application stores several types of documents in the same bucket. I know this is not a good practice, but I have a limit on the number of buckets that I can create on my server, and there is currently no way around this. Documents have a prefix of their types, so when I receive a document, I just need to concatenate the prefix and identifier to get the key, and I can perform a key search.
I need to create a report that contains information from several types of documents.
My map is as follows:
function(doc, meta) {
var getStep = function(stepName, exit, mapper) {
if (meta.id.indexOf(stepName) !== -1) {
var hotelId = parseInt(meta.id.replace(stepName + '_', ''));
if (hotelId > 0) {
var result = {
hotelId: hotelId,
exit: exit
};
if (mapper !== undefined) {
mapper(result);
}
return result;
}
}
return null;
};
var photos = getStep('PHOTOS', 7);
if (photos != null) {
emit(photos.hotelId, photos);
}
var pricing = getStep('PICR', 5);
if (pricing != null) {
emit(pricing.hotelId, pricing);
}
var owner = getStep('OWNER', 1);
if (owner != null) {
emit(owner.hotelId, owner);
}
var amenity = getStep('AM', 4);
if (amenity != null) {
emit(amenity.hotelId, amenity);
}
var description = getStep('HDESC', 3, function(result) {
result.description = doc.description;
result.hotelRoomTypeId = doc.hotelRoomTypeId;
result.starRating = doc.starRating;
});
if (description != null) {
emit(description.hotelId, description);
}
var contact = getStep('DC', 3, function(result) {
result.email = doc.emailAddress;
result.contact = doc.mainContactName;
});
if (contact != null) {
emit(contact.hotelId, contact);
}
var location = getStep('LOC', 2, function(result) {
result.city = doc.cityName;
result.zip = doc.postalCode;
result.country = doc.countryName;
result.street = doc.stateName + ', ' + doc.streetName;
});
if (location != null) {
emit(location.hotelId, location);
}
var property = getStep('PRP', 1, function(result) {
result.paymentMethodId = doc.paymentMethodId
});
if (property != null) {
emit(property.hotelId, property);
}
}
It generates this output:
"total_rows":...,"rows":[
{"id":"DC_1","key":1,"value":{"hotelId":1,"exit":3,"email":"test@example.com","contact":"Jeno"}},
{"id":"HDESC_1","key":1,"value":{"hotelId":1,"exit":3,"description":".","hotelRoomTypeId":0,"starRating":5}},
{"id":"LOC_1","key":1,"value":{"hotelId":1,"exit":2,"city":"Barcelona","zip":"1222","country":"Spain","street":"Catalonia, someplacenice"}},
{"id":"PRP_1","key":1,"value":{"hotelId":1,"exit":1}},
{"id":"PRP_2","key":2,"value":{"hotelId":2,"exit":1}},
{"id":"AM_3","key":3,"value":{"hotelId":3,"exit":4}},
{"id":"AM_4","key":4,"value":{"hotelId":4,"exit":4}},
{"id":"PHOTOS_4","key":4,"value":{"hotelId":4,"exit":7}},
{"id":"PRP_4","key":4,"value":{"hotelId":4"exit":1}},
{"id":"AM_4","key":4,"value":{"hotelId":4,"exit":4}},
{"id":"PRP_4","key":4,"value":{"hotelId":4,"exit":1}},
{"id":"PHOTOS_5","key":5,"value":{"hotelId":5,"exit":7}}
...
]
hotelId, , . , , , , , . , , .
function(key, values, rereduce) {
if (rereduce) {
return values;
} else {
var results = {};
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (!results[row.hotelId]) {
results[row.hotelId] = {
phone: '',
exit: 1
};
}
var result = results[row.hotelId];
for (var name in row) {
result[name] = row[name];
}
if (row.exit > row.exit) {
result.exit = row.exit;
}
};
return results;
}
}
RangeError: Maximum call stack size exceeded
function(key, values, rereduce) {
if (rereduce) {
return values;
} else {
var results = [];
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (!results[row.hotelId]) {
results[row.hotelId] = {
phone: '',
exit: 1
};
}
var result = results[row.hotelId];
for (var name in row) {
result[name] = row[name];
}
if (row.exit > row.exit) {
result.exit = row.exit;
}
};
return results;
}
}
reduction too large error
function(key, values, rereduce) {
if (rereduce) {
return values;
} else {
return values;
}
}
RangeError: Maximum call stack size exceeded
:
function(key, values, rereduce) {
if (rereduce) {
return values;
} else {
return values.length;
}
}
:
[ 68, 72, 65, 66, 68, 68, 70, 114 ]
JavaScript 114, . -, max_kv_size_per_doc, 1 , 10 , - . , , - ? -, , , reereduce?