Regarding Views in Couchbase!


#1

Hi,
I am creating the design document and view from application side (java side) and after that querying that view from the application side. We are getting the documents sometimes and sometimes not (returning empty response)

The following is my view code:

public void generateTextFile() {
CouchbaseClient couchClient = ConnectionManager.instance().getClient();

    String batchRunDate = null;
        //Yesterday's date

// if(batchDate == null || batchDate.isEmpty()) {
DateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
dateFormat.setTimeZone(TimeZone.getTimeZone(“America/Chicago”));
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
batchRunDate = dateFormat.format(cal.getTime());
logger.info("Yesterday’s Date : " + batchRunDate);

    DesignDocument designDoc = new DesignDocument("dev_batchProcess");

    String batchViewName = "by_date";

// String mapFunction=“function(doc,metadata){\n if(doc.CtxtbFN && doc.CtxtbFN!=’’){\n emit(doc.CurrentDate);\n}\n}”;
String mapFunction = “function(doc,meta){\n if(doc.batchDate && doc.batchDate!=’’ && doc.signedDate && doc.signedDate!=’’){\n emit(doc.batchDate);\n}\n}”;

    ViewDesign viewDesign = new ViewDesign(batchViewName, mapFunction);
    designDoc.getViews().add(viewDesign);
    couchClient.createDesignDoc(designDoc);

      Query query = new Query();
      query.setIncludeDocs(true);
      query.setKey(batchRunDate);
      query.setStale(Stale.FALSE);

    View batchView = couchClient.getView("dev_batchProcess","by_date");
    ViewResponse batchResp = couchClient.query(batchView,query);
    logger.info("Batch Response " + batchResp);
    Map<String, Object> batchDocs = batchResp.getMap();
    logger.info("Eapp Batch docs in Map : " + batchDocs);
    ArrayList<Object> batchDocsList = new ArrayList<Object>(batchDocs.values());
    logger.info("Eapp Batch docs in List : " + batchDocsList);

}