If you haven't already, download and install Couchbase Server
2.0. While you're at it, make sure to install the
beer-sample sample bucket on the fly. If you
already have the server installed and the
beer-sample bucket is not in place, head over
to Settings->Sample Buckets and install it.
Give it a few seconds until the notificaton box disappears. You
may need to shrink the size of some of your existing buckets to
make room for the beer-sample database.
The beer-sample bucket comes with a small set
of views already predefined, but to make our application
function correctly we need some more. This is also a very good
chance to explore the view management possibilities inside the
Web UI.
Since we want to list beers and breweries by name, we need to
define one view for each. Head over to the Web UI and click on
the Views menu. Select
beer-sample from the dropdown list to switch
to the correct bucket. Now click on Development
Views and then Create Development
View to define your first view. You need to give it
the name of both the design document and the actual view. Insert
the following names:
Design Document Name: _design/dev_beer
View Name: by_name
The next step is to define the map and
(optional) reduce functions. In our examples,
we won't use have a reduce function, but you
can play around if you would like to. Insert the following
JavaScript map function and click
Save.
function (doc, meta) { if(doc.type && doc.type == "beer") { emit(doc.name, null); } }
Every map function takes the full document
(doc) and (optionally) its associated
metadata (meta) as the arguments. You are
then free to inspect this data and emit a
result when you want to have it in your view. Views are always
sorted by key, so by emitting information, we are in effect
creating an index. In our case we emit the name of the beer
(doc.name) when the document both has a type
field and the type is beer. We don't need to
emit a value - that's because we are using
null here. It's always advisable to keep the
view entry as small as possible. Resist the urge to include the
full document through emit(meta.id, doc),
because it will increase the size of your views. If you need to
access the full document (or large parts), then retrieve the
document via the returned id in the view result. Note: at this
time PHP does not have a way to include docs, though some other
Couchbase SDKs do.
Now we need to do (nearly) the same for our breweries. Since you already know how to do this, here is all the information you need to create it:
Design Document Name: _design/dev_brewery
View Name: by_name
Map Function:
function (doc, meta) { if(doc.type && doc.type == "brewery") { emit(doc.name, null); } }
The final step that you need to do is to push the design
documents in production. While the design documents are in
development, the index is only applied to a
subset of the data. Since we want to have the index on the whole
dataset, click the Publish button on both
design documents (and accept any info popup that warns you from
overriding the old one). See the view section of the Couchbase
Server manual for more information on how you may use this
development and production workflow to your advantage when
developing a large application.