Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: Java 1.1
Community Wiki and Resources
Download Client Library
JavaDoc
Couchbase Developer Guide 2.0
Couchbase Server Manual 2.0
Java Client Library
SDK Forum
Wiki: Java Client Library
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
2.2 Preparations
Chapter Sections
Chapters

2.2.2. Preparing the Views

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:

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:

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: