0

我已经编写了一个视图并通过同步网关将其插入到 couchbase 服务器中。当我在 couchbase 控制台中单击“显示结果”时,它会返回一个文档。此视图是生产视图而不是开发视图。当我尝试从 android 应用程序访问此视图时,它返回一个空索引。我还尝试在应用程序中编写视图。这也返回了一个空索引。我似乎找不到问题。任何帮助,将不胜感激。谢谢!

这是通过 Rest API 调用同步网关的管理端口(:4985)通过同步网关插入后的视图:-

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); 
}

这将返回一个 type="ReferenceClass" 的文档。数据库中只有一个具有这种类型的文档。

这是我在 android 应用程序中写的视图:-

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");

在这里,我也尝试将版本号更新为“2”,但问题仍然存在。

这是我查询和构建索引的地方:-

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());
}

这是 query.run() 在 logcat 上的输出:-

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

我使用手机插入了“ReferenceClass”类型的文档,所以它应该在 CBlite 数据库中。不过,我还没有将它插入“_local”数据库。

我还尝试按 ID 获取文档。这对于从手机插入的文档以及从其他手机插入服务器的文档都很好。

我在 map 函数之后插入了以下代码行:-

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

我收到以下结果:-

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

我也试过改变

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

声明

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

1 回答 1

0

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.

于 2015-06-17T07:36:55.613 回答