From your previous couchbase related questions, it seems you are using the java SDK?
Both 1.4 and 2.0 lines of the SDK allow for programmatically creating desing documents and views.
With Java SDK 1.4.x
You have to load your view definitions (map functions, reduce functions, in which design document to put them) somehow, as Strings. See the documentation at http://docs.couchbase.com/couchbase-sdk-java-1.4/#design-documents.
Basically you create a ViewDesign
in a DesignDocument
that you insert in the database via the CouchbaseClient
:
DesignDocument designDoc = new DesignDocument("beers");
designDoc.setView(new ViewDesign("by_name", "function (doc, meta) {" +
" if (doc.type == \"beer\" && doc.name) {" +
" emit(doc.name, null);" +
" }" +
"}"));
client.createDesignDoc(designDoc);
With Java SDK 2.0.x
In the same way, you have to load your view definitions (map functions, reduce functions, in which design document to put them) somehow, as Strings.
Then you deal with DesignDocument
, adding DefaultView
to it, and insert the design document in the bucket via Bucket
's BucketManager
:
List<View> viewsForCurrentDesignDocument = new ArrayList<View>(viewCountForCurrentDesignDoc);
//... for each view definition you loaded
View v = DefaultView.create(viewName, viewMapFunction, viewReduceFunction);
viewsForCurrentDesignDocument.add(v);
//... then create the designDocument proper
DesignDocument designDocument = DesignDocument.create(designDocName, viewsForCurrentDesignDocument);
//optionally you can insert it as a development design doc, retrieve an existing one and update, etc...
targetBucket.bucketManager().insertDesignDocument(designDocument);