-->

Query multiple keys in couchbase lite view

2019-08-15 01:15发布

问题:

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?

回答1:

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


回答2:

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