You'd use multiple indexes:
On Sun, 2010-10-24 at 11:53 +0100, John Logsdon wrote:
> Hi
>
> I have an index that has three 'groups' to to represent an Account Name, a
> Contained entity name and a contained entity type e.g. {"account":"johnl",
> "name":"myplan", "type":"plan"}
>
> I'm after the equivalent of a startkey endkey but for a composite index so I
> could do the following types of queries:
>
> 1) Search across all Accounts for any Entity type starting 'myp" (This
> supports ajax search as the user starts typing in the search box)
>
> e.g. Account = *, Type = *, Name starts with myp
by_name:
emit(name, 1);
startkey=myp
endkey=myp\u9999
>
> 2) Search a list of Accounts for any Entity type starting 'myp"
>
> e.g. Account in johnl, mycompany, myreseller, global, Type = *, Name starts
> with myp
by_account_and_name:
emit([account, name], 1);
query once for every company and then merge the results in your
application:
startkey=["johnl", "myp"]
endkey=["johnl", "myp\u9999"]
(etc.)
>
>
> 3) Search for all plans named "myplan" in all accounts
>
> e.g. Account = *, Type = Plan, Name = myplan
by_type_and_name:
emit([type, name], 1);
startkey=["Plan", "myplan"]
endkey=["Plan", "myplan"]
(or you can probably use key=["Plan", "myplan"])
>
>
> 4) Search a list of Accounts for all plans
>
> e.g. Account = *, Type = Plan, Name = *
>
Use by_type_and_name:
startkey=["Plan", 0]
endkey=["Plan", {}]
>
> 5) Search a List of Accounts for all contained entities
>
> e.g. Account in johnl, mycompany, myreseller, global, Type = *, Name = *
Use by_account_and_name, or perhaps make a new view called by_account
and then just query for accounts:
startkey=["johnl", 0]
endkey=["johnl", {}]
and merge it with the results from the other accounts.
If you need something significantly more complex, like type = x, name =
x, y, or z, account is in unpaid status, and not a corporate account, or
something else that can't really be mapped to a key-value methodology,
then you'll probably need to check out CouchDB-Lucene.
P.S. I emit 1 as the value so that I have an easy way to count the
results. If you have something better (dollars? hits?), then you should
emit that instead. I don't see the point in emitting the ID because you
can use include_docs=true to get the documents, and IIRC, the ID is
passed anyways.