Beer sample bucket

Beer sample bucket

The beer sample data shows a combination of document structure and sample views.

The beer sample data demonstrates a combination of the document structure used to describe different items, including references between objects, and a number of sample views that show the view structure and layout.

The primary document type is the ‘beer’ document:

{ "name": "Piranha Pale Ale", "abv": 5.7, "ibu": 0, "srm": 0, "upc": 0, "type": "beer", "brewery_id": "110f04166d", "updated": "2010-07-22 20:00:20", "description": "", "style": "American-Style Pale Ale", "category": "North American Ale" } 

Beer documents contain core information about different beers, including the name, alcohol by volume ( abv ) and categorization data.

Individual beer documents are related to brewery documents using the brewery_id field, which holds the information about a specific brewery for the beer:

{ "name": "Commonwealth Brewing #1", "city": "Boston", "state": "Massachusetts", "code": "", "country": "United States", "phone": "", "website": "", "type": "brewery", "updated": "2010-07-22 20:00:20", "description": "", "address": [ ], "geo": { "accuracy": "APPROXIMATE", "lat": 42.3584, "lng": -71.0598 } } 

The brewery record includes basic contact and address information for the brewery, and contains a spatial record consisting of the latitude and longitude of the brewery location.

To demonstrate the view functionality in Couchbase Server, three views are defined.

brewery_beers view

The brewery_beers view outputs a composite list of breweries and beers they brew by using the view output format to create a ‘fake’ join. This outputs the brewery ID for brewery document types, and the brewery ID and beer ID for beer document types:

function(doc, meta) { switch(doc.type) { case "brewery": emit([meta.id]); break; case "beer": if (doc.brewery_id) { emit([doc.brewery_id, meta.id]); } break; } } 

The raw JSON output from the view:

{ "total_rows" : 7315, "rows" : [ { "value" : null, "id" : "110f0013c9", "key" : [ "110f0013c9" ] }, { "value" : null, "id" : "110fdd305e", "key" : [ "110f0013c9", "110fdd305e" ] }, { "value" : null, "id" : "110fdd3d0b", "key" : [ "110f0013c9", "110fdd3d0b" ] }, … { "value" : null, "id" : "110fdd56ff", "key" : [ "110f0013c9", "110fdd56ff" ] }, { "value" : null, "id" : "110fe0aaa7", "key" : [ "110f0013c9", "110fe0aaa7" ] }, { "value" : null, "id" : "110f001bbe", "key" : [ "110f001bbe" ] } ] } 

The output could be combined with the corresponding brewery and beer data to provide a list of the beers at each brewery.

by_location view

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} ] }