You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Andrey Cherkashin <an...@gmail.com> on 2011/02/23 02:06:38 UTC
Help With map function
Hello,
I have a problem, I can't understand how to write map function that solves my problem:
I have a lot of "documents", each document has type (e.g. item, user), category and brand. So i have map functions that gives me a list of every document that has type item (that's easy), but how i can get list of all documents that has brand == xxx or category == yyy or even brand==xxx&category=zzz (xxx,yyy,zzz different every time).
Re: Help With map function
Posted by Andrey Cherkashin <an...@gmail.com>.
Javi,
Thanks for answer. As far as i know i can use complex keys (according to couch wiki) my problem was that I didn't understand how to do it. Now my problem is that i can't find way to do "curl http://127.0.0.1:5984/hanh-phuc-sac-dev/_design/staff/_view/all?key=\"cat1\" from my ruby app. I'm planing to use couchrest_model and if i understand coretly when i add "view_by :category" to my class I can use Item.find_by_category('cat1').
On Feb 22, 2011, at 5:52 PM, Javier Julio wrote:
> Andrey,
>
> Great question as I had been struggling with the same. In the docs its kind of buried there but you can post multiple keys to a view. I believe this is what you are looking for. Correct me if I'm wrong. So I'd create a view where the index has item, brand and category and then you can just include a key set for each grouping you want in the post body.
>
> If you look under the table with all the query params in this section of the View docs: http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options you'll see a that it mentions you can post multiple keys to a view.
>
> I'm still learning myself so I'm not sure if you can post complex keys (only simple strings are used). I would assume so since its not stated otherwise but I believe this is what will solve your problem.
>
> Ciao!
> Javi
>
> On Feb 22, 2011, at 8:06 PM, Andrey Cherkashin wrote:
>
>> Hello,
>>
>> I have a problem, I can't understand how to write map function that solves my problem:
>>
>> I have a lot of "documents", each document has type (e.g. item, user), category and brand. So i have map functions that gives me a list of every document that has type item (that's easy), but how i can get list of all documents that has brand == xxx or category == yyy or even brand==xxx&category=zzz (xxx,yyy,zzz different every time).
>
Re: Help With map function
Posted by Andrey Cherkashin <an...@gmail.com>.
Well total 3 views: 1) All Items and key is brand
2) All Items and key is category
3) All Items and key is category,brand
Right?
On Feb 22, 2011, at 6:19 PM, Paul J. Davis wrote:
>
>
> On Feb 22, 2011, at 9:06 PM, Javier Julio <jj...@gmail.com> wrote:
>
>> Paul, thanks for confirming. Can you elaborate on "the non-wildcard selection criteria as a leading prefix"? What does that mean?
>>
>> So to handle an AND/OR statement would best be or perhaps only way to do with multiple views? So since Andrey wanted to support "brand and category" or "brand or category" he would need 3 views right?
>>
>
> The root is that CouchDB views don't do OR queries. For AND the terms need to be ordered so you can apply left to right.
>
> In a funny aside, one of the amusing from couchdb-lucene is "for when you want foo OR bar".
>
>> One for item, brand and category, second for item and brand, and the third for item and category?
>>
>> On Feb 22, 2011, at 9:01 PM, Paul J. Davis wrote:
>>
>>> Yep, the multi-key post doesn't require string keys.
>>>
>>> Also, when you want wild card behavior you need a view that has the non-wildcard selection criteria as a leading prefix. It's easiest to think about in terms of array slicing.
>>>
>>> For the OP I would suggest multiple views with the required array orders for the expected queries.
>>>
>>> On Feb 22, 2011, at 8:52 PM, Javier Julio <jj...@gmail.com> wrote:
>>>
>>>> Andrey,
>>>>
>>>> Great question as I had been struggling with the same. In the docs its kind of buried there but you can post multiple keys to a view. I believe this is what you are looking for. Correct me if I'm wrong. So I'd create a view where the index has item, brand and category and then you can just include a key set for each grouping you want in the post body.
>>>>
>>>> If you look under the table with all the query params in this section of the View docs: http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options you'll see a that it mentions you can post multiple keys to a view.
>>>>
>>>> I'm still learning myself so I'm not sure if you can post complex keys (only simple strings are used). I would assume so since its not stated otherwise but I believe this is what will solve your problem.
>>>>
>>>> Ciao!
>>>> Javi
>>>>
>>>> On Feb 22, 2011, at 8:06 PM, Andrey Cherkashin wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I have a problem, I can't understand how to write map function that solves my problem:
>>>>>
>>>>> I have a lot of "documents", each document has type (e.g. item, user), category and brand. So i have map functions that gives me a list of every document that has type item (that's easy), but how i can get list of all documents that has brand == xxx or category == yyy or even brand==xxx&category=zzz (xxx,yyy,zzz different every time).
>>>>
>>
Re: Help With map function
Posted by "Paul J. Davis" <pa...@gmail.com>.
On Feb 22, 2011, at 9:06 PM, Javier Julio <jj...@gmail.com> wrote:
> Paul, thanks for confirming. Can you elaborate on "the non-wildcard selection criteria as a leading prefix"? What does that mean?
>
> So to handle an AND/OR statement would best be or perhaps only way to do with multiple views? So since Andrey wanted to support "brand and category" or "brand or category" he would need 3 views right?
>
The root is that CouchDB views don't do OR queries. For AND the terms need to be ordered so you can apply left to right.
In a funny aside, one of the amusing from couchdb-lucene is "for when you want foo OR bar".
> One for item, brand and category, second for item and brand, and the third for item and category?
>
> On Feb 22, 2011, at 9:01 PM, Paul J. Davis wrote:
>
>> Yep, the multi-key post doesn't require string keys.
>>
>> Also, when you want wild card behavior you need a view that has the non-wildcard selection criteria as a leading prefix. It's easiest to think about in terms of array slicing.
>>
>> For the OP I would suggest multiple views with the required array orders for the expected queries.
>>
>> On Feb 22, 2011, at 8:52 PM, Javier Julio <jj...@gmail.com> wrote:
>>
>>> Andrey,
>>>
>>> Great question as I had been struggling with the same. In the docs its kind of buried there but you can post multiple keys to a view. I believe this is what you are looking for. Correct me if I'm wrong. So I'd create a view where the index has item, brand and category and then you can just include a key set for each grouping you want in the post body.
>>>
>>> If you look under the table with all the query params in this section of the View docs: http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options you'll see a that it mentions you can post multiple keys to a view.
>>>
>>> I'm still learning myself so I'm not sure if you can post complex keys (only simple strings are used). I would assume so since its not stated otherwise but I believe this is what will solve your problem.
>>>
>>> Ciao!
>>> Javi
>>>
>>> On Feb 22, 2011, at 8:06 PM, Andrey Cherkashin wrote:
>>>
>>>> Hello,
>>>>
>>>> I have a problem, I can't understand how to write map function that solves my problem:
>>>>
>>>> I have a lot of "documents", each document has type (e.g. item, user), category and brand. So i have map functions that gives me a list of every document that has type item (that's easy), but how i can get list of all documents that has brand == xxx or category == yyy or even brand==xxx&category=zzz (xxx,yyy,zzz different every time).
>>>
>
Re: Help With map function
Posted by Javier Julio <jj...@gmail.com>.
Paul, thanks for confirming. Can you elaborate on "the non-wildcard selection criteria as a leading prefix"? What does that mean?
So to handle an AND/OR statement would best be or perhaps only way to do with multiple views? So since Andrey wanted to support "brand and category" or "brand or category" he would need 3 views right?
One for item, brand and category, second for item and brand, and the third for item and category?
On Feb 22, 2011, at 9:01 PM, Paul J. Davis wrote:
> Yep, the multi-key post doesn't require string keys.
>
> Also, when you want wild card behavior you need a view that has the non-wildcard selection criteria as a leading prefix. It's easiest to think about in terms of array slicing.
>
> For the OP I would suggest multiple views with the required array orders for the expected queries.
>
> On Feb 22, 2011, at 8:52 PM, Javier Julio <jj...@gmail.com> wrote:
>
>> Andrey,
>>
>> Great question as I had been struggling with the same. In the docs its kind of buried there but you can post multiple keys to a view. I believe this is what you are looking for. Correct me if I'm wrong. So I'd create a view where the index has item, brand and category and then you can just include a key set for each grouping you want in the post body.
>>
>> If you look under the table with all the query params in this section of the View docs: http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options you'll see a that it mentions you can post multiple keys to a view.
>>
>> I'm still learning myself so I'm not sure if you can post complex keys (only simple strings are used). I would assume so since its not stated otherwise but I believe this is what will solve your problem.
>>
>> Ciao!
>> Javi
>>
>> On Feb 22, 2011, at 8:06 PM, Andrey Cherkashin wrote:
>>
>>> Hello,
>>>
>>> I have a problem, I can't understand how to write map function that solves my problem:
>>>
>>> I have a lot of "documents", each document has type (e.g. item, user), category and brand. So i have map functions that gives me a list of every document that has type item (that's easy), but how i can get list of all documents that has brand == xxx or category == yyy or even brand==xxx&category=zzz (xxx,yyy,zzz different every time).
>>
Re: Help With map function
Posted by "Paul J. Davis" <pa...@gmail.com>.
Yep, the multi-key post doesn't require string keys.
Also, when you want wild card behavior you need a view that has the non-wildcard selection criteria as a leading prefix. It's easiest to think about in terms of array slicing.
For the OP I would suggest multiple views with the required array orders for the expected queries.
On Feb 22, 2011, at 8:52 PM, Javier Julio <jj...@gmail.com> wrote:
> Andrey,
>
> Great question as I had been struggling with the same. In the docs its kind of buried there but you can post multiple keys to a view. I believe this is what you are looking for. Correct me if I'm wrong. So I'd create a view where the index has item, brand and category and then you can just include a key set for each grouping you want in the post body.
>
> If you look under the table with all the query params in this section of the View docs: http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options you'll see a that it mentions you can post multiple keys to a view.
>
> I'm still learning myself so I'm not sure if you can post complex keys (only simple strings are used). I would assume so since its not stated otherwise but I believe this is what will solve your problem.
>
> Ciao!
> Javi
>
> On Feb 22, 2011, at 8:06 PM, Andrey Cherkashin wrote:
>
>> Hello,
>>
>> I have a problem, I can't understand how to write map function that solves my problem:
>>
>> I have a lot of "documents", each document has type (e.g. item, user), category and brand. So i have map functions that gives me a list of every document that has type item (that's easy), but how i can get list of all documents that has brand == xxx or category == yyy or even brand==xxx&category=zzz (xxx,yyy,zzz different every time).
>
Re: Help With map function
Posted by Javier Julio <jj...@gmail.com>.
Andrey,
Great question as I had been struggling with the same. In the docs its kind of buried there but you can post multiple keys to a view. I believe this is what you are looking for. Correct me if I'm wrong. So I'd create a view where the index has item, brand and category and then you can just include a key set for each grouping you want in the post body.
If you look under the table with all the query params in this section of the View docs: http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options you'll see a that it mentions you can post multiple keys to a view.
I'm still learning myself so I'm not sure if you can post complex keys (only simple strings are used). I would assume so since its not stated otherwise but I believe this is what will solve your problem.
Ciao!
Javi
On Feb 22, 2011, at 8:06 PM, Andrey Cherkashin wrote:
> Hello,
>
> I have a problem, I can't understand how to write map function that solves my problem:
>
> I have a lot of "documents", each document has type (e.g. item, user), category and brand. So i have map functions that gives me a list of every document that has type item (that's easy), but how i can get list of all documents that has brand == xxx or category == yyy or even brand==xxx&category=zzz (xxx,yyy,zzz different every time).