Prefix matching in an "all docs" query

25 views
Skip to first unread message

Ben Kennedy

unread,
Apr 29, 2015, 6:29:09 PM4/29/15
to mobile-c...@googlegroups.com
Hey folks,

(New to the list, and to Couchbase Lite, about which I am very enthusiastic!)

I am having trouble getting desired results from an "all docs" query when using some of the features described in the 1.0.3 "Query Enhancements" documentation [1]. Specifically, prefixMatchLevel does not seem to work as described.

I wish to retrieve all documents with conflicts whose document ID begins with a particular prefix (specifically of the form "business.NNN." where NNN is a number). I instantiate and configure the query like so:

CBLQuery *query = [database createAllDocumentsQuery];
query.allDocsMode = kCBLOnlyConflicts;

The documentation suggests that I should be able to get the relevant rows by doing this:

query.startKey = query.endKey = [NSString stringWithFormat:@"business.%@.", business.server_psk];
query.inclusiveStart = query.inclusiveEnd = YES;
query.prefixMatchLevel = 1;

However, this returns no results. (It is not clear whether inclusiveStart and inclusiveEnd should or should not be YES in this case, but it seems to make no difference.) I only get a result if I specify a startKey and endKey that exactly match an existing document ID.

Does prefixMatchLevel not work for "all docs" queries? I did not see such a caveat mentioned anywhere.

Instead, I have found it necessary to use this approach (stuffing a letter "Z" onto the end of the number so as to stop up the alphabetic sort):

query.startKey = [NSString stringWithFormat:@"business.%@.", business.server_psk];
query.endKey = [NSString stringWithFormat:@"business.%@Z.", business.server_psk];
query.inclusiveStart = YES;
query.inclusiveEnd = NO;

Needless to say, the "Z" technique is kludgy at best. Surely I am overlooking something. Is there a better approach to achieve my query?

I'm using CBL 1.0.4 from CocoaPods/github.

thanks,

b

[1] https://github.com/couchbase/couchbase-lite-ios/wiki/Query-Enhancements

Jens Alfke

unread,
Apr 29, 2015, 6:41:25 PM4/29/15
to mobile-c...@googlegroups.com

On Apr 29, 2015, at 3:28 PM, Ben Kennedy <ben.k...@kashoo.com> wrote:

Does prefixMatchLevel not work for "all docs" queries? I did not see such a caveat mentioned anywhere.

Crap, it looks like it doesn’t! allDocs is implemented separately from view queries, even though they’re very similar, and it looks like the bozo who implemented prefixMatchLevel [i.e. me] forgot that and only implemented it for view queries. I’ve filed an issue.

The “append a Z” technique will work, although it’s safer to use some other Unicode character that’s higher than anything that might occur in the docID.

—Jens

Ben Kennedy

unread,
Apr 29, 2015, 7:27:19 PM4/29/15
to mobile-c...@googlegroups.com
Awesome; thanks Jens! (particularly for the super-speedy confirmation that I wasn't screwing up something obvious)

cheers,

b
Reply all
Reply to author
Forward
0 new messages