Hi,
So the documentation on map/reduce says this:
Don't try to "parameterize" the map function by referring to an external variable whose value you change when querying. It won't work. People sometimes try this because they want to find various subsets of the data, like all the items of a particular color. Instead, emit all the values of that property, and use a key range in the query to pick out the rows with the specific value you want.
I have in my app, a set of user configurable NSPredicates (on iOS and OS X) which I use to query my SQLite database. Currently I'm using these predicates to generate SQL statements which query the data returning a filtered set of results and also to calculate aggregate totals, averages, mins, and maxes on the results.
So in converting my NSPredicates into something the could be used in Couchbase Lite, I decided to loop through all the NSComparisonPredicates of the NSPredicate and basically decide which documents should be emitted or not.
The values I'm using for the keys in the emit function can be the same and/or different from the values being queried. That is, I may be sorting my records on Field 1, Field 4, and Field 7, but I may be querying the data on Field 3, Field 4, Field 5, and so on. The user can specify as many fields as they like to query on.
An example NSPredicate for a Movies form might be: (Movie Title == "The Terminator") AND (Rating >= 3) AND (Actor contains "Schwarzenegger") AND (Release Date IS EMPTY). The results might be grouped by Genre, then sorted by Movie Title, then sorted by Director Name.
So I thought the only way that I would be able to do this would be to create a view and emit only these rows that qualify for all the comparisons within the NSPredicate. But after reading the above quote from the manual, I'm not so sure if what I've done is a good idea or not.
A user could potentially have a dozen forms each with a dozen NSPredicates, so there would be a view for all the forms and a view for each NSPredicate.
So I'm wondering if maybe I have to abandon the idea of creating one view for each NSPredicate (which in itself can be a compound NSPredicate as described above) and just emit all the records for the specific form and then convert my NSPredicates into something that can be used by the CBLQueryBuilder. Or should I be using the postFilter property of CBLQuery to filter the results?
I need my queries to be fast so that's why I was thinking of creating a view for each NSPredicate. But obviously indexing should be fast too, so I don't want to do too much. There could be tens of thousands of documents in the database. Ultimately I'd like to be able to support over 100,000 documents in the database.
Thanks!
Brendan