Query multiple keys in couchbase lite view

2019-08-15 00:44发布

Hi I am new to couchbase/couchbase-lite and i try to query a view with multiple keys without success. Her is how the map function looks:

public void map(Map<String, Object> doc, Emitter emitter) {
    if (doc.get("type").equals("my_type") {
        List<Object> keys = new ArrayList<Object>();
        keys.add(doc.get("key_1"));
        keys.add(doc.get("key_2"));
        emitter.emit(keys, null); 
    }   
}

My problem is that i need to query the view either only with key_1 or with a combination of key_1 and key_2 like so

List<Object> keys = new ArrayList<Object>();
keys.add(key_1);
if (key_2 != null) keys.add(key_2);
query.setKeys(keys);
results = query.run()

However the results are always empty. Do i overlook anything?

2条回答
姐就是有狂的资本
2楼-- · 2019-08-15 01:12

Two emits doesn't work. If you give a ArrayList in setKeys() method, each key in the List match the each key in emit. If you want to match two keys, add keys ArrayList into another List. Then pass it to setKeys() method. Like this,

List<Object> keys = new ArrayList<Object>();
List<Object> allKeys = new ArrayList<Object>();
keys.add(key_1);
if (key_2 != null) keys.add(key_2);
allKeys.add(keys);
query.setKeys(allKeys);
results = query.run();
查看更多
Melony?
3楼-- · 2019-08-15 01:24

This is the solution, We need to add keys into another List<Object> https://github.com/couchbase/couchbase-lite-android/issues/740

查看更多
登录 后发表回答