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 their 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 the reduce functions at all but
you can play around and see what happens. Insert the following
map function (that's JavaScript) 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 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 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 we we are using
null here. It's always advisable to keep the
index 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 view indexes. If you need to
access the full document (or large parts), then use the
setIncludeDocs(true) directive which will do
a get() call against the document ID in the
background. The resulting retrieval of the document may be
slightly out of sync with your view, but it will be fast and
efficient.
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 on the
local node. 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).
For more information about using views for indexing and querying from Couchbase Server, here are some useful resources:
General Information: Couchbase Server Manual: Views and Indexes.
Sample Patterns: to see examples and patterns you can use for views, see Couchbase Views, Sample Patterns.
Timestamp Pattern: many developers frequently ask about extracting information based on date or time. To find out more, see Couchbase Views, Sample Patterns.