Compount key queries for single and multible keys

33 views
Skip to first unread message

macs.d...@gmail.com

unread,
Jul 16, 2015, 3:50:00 PM7/16/15
to mobile-c...@googlegroups.com
Hi i need to query a view that indexes a compound key either by a single key within the compound key (only the first key )of multiple keys (the first and the second key). To be more specific here is how my map function looks (Android).

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

I am emitting a compound key containing the uuid of a document and the uuid of its parent.  Basically i need to retrieve either a single document by its uuid (32 bit string) or all documents belonging to a specific parent.
i am able to manage the query for a single document by its uuid like so:


View view = database.getView("forms");
QueryOptions options = new QueryOptions();
List<Object> keys = new ArrayList<Object>();
keys
.add(new Object[]{parent_uuid, uuid});
options
.setKeys(keys);
resources
= view.queryWithOptions(opts);

However, how would i query all documents that belong to a certain parent. Would that be possible with the above map function? Or would i need a second map function?
the following approach obviously does not work:

List<Object> keys = new ArrayList<Object>();
keys
.add(new Object[]{parent_uuid, new HashMap<String, Object>()});


Jens Alfke

unread,
Jul 16, 2015, 6:35:25 PM7/16/15
to mobile-c...@googlegroups.com

On Jul 16, 2015, at 12:05 PM, macs.d...@gmail.com wrote:

However, how would i query all documents that belong to a certain parent. Would that be possible with the above map function? Or would i need a second map function?

That’s a key range query. In JSON terms, you’d use a startKey of [parent_uuid] and an endKey of [parent_uuid, { }]. (The “{ }” is basically a placeholder; see the docs I linked to.)

—Jens

atom992

unread,
Jul 17, 2015, 1:30:20 AM7/17/15
to mobile-c...@googlegroups.com
If in the view function I emit([doc.key,doc.type.doc.status],doc.name) and I want to query the data that belong to certain key and  certain type, can I use key range query? 
such as:
startkey=["key1","type1"]
endkey=["key1","type1",{}]

I try to query the data using the startkey and endkey by RCBL EST API but got error:
{
  "error": "not_found",
  "reason": "Router unable to route request to do_GET_DesignDocumentjava.lang.reflect.InvocationTargetException"
}

Jens Alfke

unread,
Jul 17, 2015, 1:52:17 AM7/17/15
to mobile-c...@googlegroups.com
On Jul 16, 2015, at 10:30 PM, atom992 <yangzi...@gmail.com> wrote:

startkey=["key1","type1"]
endkey=["key1","type1",{}]

Yup, that’s exactly right.

I try to query the data using the startkey and endkey by RCBL EST API but got error:

Hm, that looks like a Couchbase Lite bug … I don’t work on the Android version, though.
What was the exact URL of the query?

—Jens
Message has been deleted

macs.d...@gmail.com

unread,
Jul 17, 2015, 6:49:37 AM7/17/15
to mobile-c...@googlegroups.com
       
I can confirm that i got it working with the fillowing query keys

       List<Object> start_key = new ArrayList<Object>();
        start_key.add(parent_uuid);    
        List<Object> end_key = new ArrayList<Object>();
        end_key.add(parent_uuid);
        end_key.add(new HashMap<String, Object>());

atom992

unread,
Jul 17, 2015, 7:51:39 AM7/17/15
to mobile-c...@googlegroups.com
GET /cbldb/_design/poc/_view/getListByKey?stale=ok&connection_timeout=60000&startkey=%5B%22KEY:4f9a9c7f-969e-44a4-89b0-19411c8e6b46%22%2C%22TYPE1%22%%5D%22&endkey=%5B%22KEY:4f9a9c7f-969e-44a4-89b0-19411c8e6b46%22%2C%22TYPE1%22%2C%7B%7D5D HTTP/1.1
 

—Jens

Jens Alfke

unread,
Jul 17, 2015, 11:16:25 AM7/17/15
to mobile-c...@googlegroups.com

On Jul 17, 2015, at 4:51 AM, atom992 <yangzi...@gmail.com> wrote:

GET /cbldb/_design/poc/_view/getListByKey?stale=ok&connection_timeout=60000&startkey=%5B%22KEY:4f9a9c7f-969e-44a4-89b0-19411c8e6b46%22%2C%22TYPE1%22%%5D%22&endkey=%5B%22KEY:4f9a9c7f-969e-44a4-89b0-19411c8e6b46%22%2C%22TYPE1%22%2C%7B%7D5D HTTP/1.1

I tried unescaping the startkey and endkey params, and there are some glitches. startkey unescapes to:

["KEY:4f9a9c7f-969e-44a4-89b0-19411c8e6b46","TYPE1"%]”

Note the bogus “%” — the raw version in the URL has “…%%5D…” in it.

endkey unescapes to:

["KEY:4f9a9c7f-969e-44a4-89b0-19411c8e6b46","TYPE1",{}5D

because of a missing “%” before the final “5D”.

It’s almost like a “%” was cut from the endkey and pasted into the startkey somehow. Weird!

However, Couchbase Lite still seems to be responding the wrong way. It should return a 400, not throw an exception.

—Jens

atom992

unread,
Jul 19, 2015, 11:32:30 PM7/19/15
to mobile-c...@googlegroups.com
Thank you very much. my mistake, I can query the data now.
Reply all
Reply to author
Forward
0 new messages