Spring Data Couchbase - Views



We need to create a Couchbase design document and Views in our bucket. Our document class name will be a design document name but in lowerCamelCase format( Here its customer). To support the findAll repository method we need to create a view named all. To create design documents and views, Go to the Couchbase server dashboard, and click on Views, followed by ADD VIEW.

  • Enter the details as mentioned in the below image.

View All
  • If we want to create a view for any custom method such as findBySalary or findByEmail then it needs to be created in the same way, as follows.

View By Email
  • Similarly, we can create views for all other custom methods of our repository. Finally, it will look like this −

Views

If we want, we can create or modify the views using Map functions, to do this click on edit, and enter map function. Lets say we want to create views for findByName, then our equivalent map function will be −

function (doc, meta) {
   if(doc._class == "com.tutorialspoint.couchbase.document.Customer"
   && doc.name) {
      emit(doc.name, null);
   }
}

For field salary and method findBySalary it will be −

function (doc, meta) {
   if(doc._class == "com.tutorialspoint.couchbase.document.Customer"
   && doc.salary) {
      emit(doc.salary, null);
   }
}

Views based custom methods inside repository must be annotated with the annotation @View as given below −

@View
List<Customer> findByName(String name);

Creation of view are optional if we are using Couchbase server 4.0 or later, otherwise it is mandatory.

Advertisements