couchbase client (android) view returns empty index

319 views Asked by At

I have written a view and inserted it into the couchbase server through the sync gateway. When I click on "Show Results" in the couchbase console, it returns one document. This view is a production view and not a development view. When I tried accessing this view from the android application, it returned an empty index. I also tried writing a view in the application. This too returned an empty index. I can't seem to find the issue. Any help would be appreciated. Thank you!

This is the view after it got inserted through sync gateway by Rest API calls to the admin port(:4985) of the sync gateway:-

function(doc,meta) {
    var sync = doc._sync;
    if (sync === undefined || meta.id.substring(0,6) == "_sync:")
      return;
    if ((sync.flags & 1) || sync.deleted)
      return;
    delete doc.sync;
    meta.rev = sync.rev;
    (function (doc, meta) {  if (doc.type != null && doc.type== "ReferenceClass")  { emit(doc.type, doc); }}) (doc, meta); 
}

This returns one document with type="ReferenceClass". There is only one document in the database with this type.

This is the view I wrote in the android application:-

com.couchbase.lite.View viewItems = database.getView(String.format("%s/%s", "design_document", "view2"));
viewItems.setMap(new Mapper() {
    @Override
    public void map(Map<String, Object> document, Emitter emitter) {
        if (document.get("type") != null && document.get("type") == "ReferenceClass")
            emitter.emit(document.get("type"), document);
    }
}, "1.0");

Here, I have also tried updating the version number to "2", yet the problem persists.

This is where I'm querying and building the index:-

Query query = database.getView("design_document/view2").createQuery();
QueryEnumerator result = query.run();
System.out.println("*************** query count="+result.getCount());
for(Iterator<QueryRow> it=result; it.hasNext();){
    QueryRow row=it.next();
    System.out.println("*************** row="+row.toString());
}

This is the output on the logcat for query.run():-

Re-indexing view: design_document/view2
 I/CBLite﹕ main Begin transaction (level 1)
 V/View﹕ lastSequence (3) == dbMaxSequence (3), nothing to do
 I/CBLite﹕ main Committing transaction (level 1)
 V/View﹕ Query design_document/view2: SELECT key, value, docid, revs.sequence FROM maps, revs, docs WHERE maps.view_id=? AND revs.sequence = maps.sequence AND docs.doc_id = revs.doc_id ORDER BY key LIMIT ? OFFSET ? | args: [2, 2147483647, 0]
 D/CBLite﹕ Query view design_document/view2 completed in 6 milliseconds
 I/System.out﹕ *************** query count=0

I inserted a document of type "ReferenceClass" using the phone, so it should be on the CBlite database. I haven't inserted it into the "_local" database though.

I also tried getting the document by Id. This worked fine for documents inserted from the phone, as well as for documents inserted into the server from other phones.

I inserted the following line of code after the map function:-

System.out.println("************** view index=" + viewItems.dump().toString());

to which i received the following result:-

I/System.out﹕ ************** view index=[]

I also tried changing the

if (document.get("type") != null && document.get("type") == "ReferenceClass")

statement to

if (document.get("type") != null && document.get("type").equals("ReferenceClass"))
1

There are 1 answers

0
TheGreenApple On

It seems to have been an issue with the property I specified as "type". I changed the key name from "type" to "type_doc" and now the query runs fine. So now my map function looks like :-

com.couchbase.lite.View viewItems = database.getView(String.format("%s/%s", "design_document", "view2"));
viewItems.setMap(new Mapper() {
            @Override
            public void map(Map<String, Object> document, Emitter emitter) {
                if (document.get("type_doc") != null && document.get("type_doc") == "ReferenceClass")
                    emitter.emit(document.get("type_doc"), document);
            }
        }, "1.0");

I guess it was looking at "type" as being JSON or otherwise. I'm not sure. Could someone please explain the details? There was no "type" key in the document other than the "type" I inserted, but I guess a wrong "type" value was being read from somewhere else.