Outputs the brewery location, accounting for missing fields in the source data. The output creates information either by country, by country and state, or by country, state and city.
function (doc, meta) { if (doc.country, doc.state, doc.city) { emit([doc.country, doc.state, doc.city], 1); } else if (doc.country, doc.state) { emit([doc.country, doc.state], 1); } else if (doc.country) { emit([doc.country], 1); } }
The view also includes the built-in _count
function for the reduce portion of the view. Without using the
reduce, the information outputs the raw location information:
{ "total_rows" : 1413, "rows" : [ { "value" : 1, "id" : "110f0b267e", "key" : [ "Argentina", "", "Mendoza" ] }, { "value" : 1, "id" : "110f035200", "key" : [ "Argentina", "Buenos Aires", "San Martin" ] }, … { "value" : 1, "id" : "110f2701b3", "key" : [ "Australia", "New South Wales", "Sydney" ] }, { "value" : 1, "id" : "110f21eea3", "key" : [ "Australia", "NSW", "Picton" ] }, { "value" : 1, "id" : "110f117f97", "key" : [ "Australia", "Queensland", "Sanctuary Cove" ] } ] }
With the reduce() enabled, grouping can be
used to report the number of breweries by the country, state, or
city. For example, using a grouping level of two, the
information outputs the country and state counts:
{"rows":[ {"key":["Argentina",""],"value":1}, {"key":["Argentina","Buenos Aires"],"value":1}, {"key":["Aruba"],"value":1}, {"key":["Australia"],"value":1}, {"key":["Australia","New South Wales"],"value":4}, {"key":["Australia","NSW"],"value":1}, {"key":["Australia","Queensland"],"value":1}, {"key":["Australia","South Australia"],"value":2}, {"key":["Australia","Victoria"],"value":2}, {"key":["Australia","WA"],"value":1} ] }