You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by "Simeon F. Willbanks" <si...@simeons.net> on 2011/02/19 17:17:34 UTC

Query view with multiple keys with OR logic

Hello,

I'm trying to fetch a set of documents with OR logic. For example,
fetch all documents with type="post" OR tag="tag1". Here are a few
example documents with rev omitted for brevity:

{
    "_id": 1,
    "type": "post",
    "tags": [
        "tag1",
        "tag2"
    ]
}

{
    "_id": 2,
    "type": "photo",
    "tags": [
        "tag1",
        "tag3"
    ]
}

{
    "_id": 3,
    "type": "photo",
    "tags": [
        "tag4",
        "tag5"
    ]
}

I'd like to fetch documents with the ids 1 and 2.

It seems this method will be my starting point:
"A JSON structure of {"keys": ["key1", "key2", ...]} can be posted to
any user defined view or _all_docs to retrieve just the view rows
matching that set of keys. Rows are returned in the order of the keys
specified. Combining this feature with include_docs=true results in
the so-called multi-document-fetch feature."
http://wiki.apache.org/couchdb/HTTP_view_API

Am I correct so far? If yes, how would I define my map function? Would
I call emit multiple times and possibly use a group=true parameter?

Thanks,
Simeon

Re: Query view with multiple keys with OR logic

Posted by Steve Midgley <sc...@misuse.org>.
Hi,

I'm new on this list - hopefully this post is relevant. I'd like to
ask a bit more about this question of querying multiple views. I come
from a postgres background, and if I were to solve this problem in
postgres, I'd just index each of the two fields holding the data and
let the SQL engine "intersect" the two fields' indices when I select
against them. Something I'd take for granted on that platform (of
course the trade-off being lack of flexibility in the indices).

So it seems like it'd be a really handy feature to be able to
intersect two views together (i.e. take the two view b-trees merge
them and find all id's that have duplicates). Once you have the
intersection, you could process this view/b-tree with a list function
or something?

Is there no way to do this on the server side with couch? Seems like
arbitrary merge/intersection of views would permit just about anything
that SQL servers can do with similar performance (and a whole lot more
flexibility)?

Thanks for any input,

Steve



On Sat, Feb 19, 2011 at 8:52 PM, Gabriel Farrell <gs...@gmail.com> wrote:
> On Sat, Feb 19, 2011 at 2:54 PM, Simeon F. Willbanks <si...@simeons.net> wrote:
>> Correct. In retrospect, I am looking for an idiom or preferred way to
>> achieve a dynamic view. My example states I'd like to query by
>> type="post" OR tag="tag1", but I'd like the key values to be dynamic.
>> My next request might be type="photo" OR tag="tag2". This would return
>> IDs 1, 2 and 3.
>>
>> I understand temporary views are an option, but they aren't efficient.
>> Would it be best to have two permanent views which can be queried by
>> key and merge the results in the client? I could have one permanent
>> view by_type and another by_tag. This technique looks to be described
>> in the blog post below, but its from 2009.
>
> Yes, I would run two queries and find the union of the returned IDs in
> the client. There are some clever ways to do things otherwise but most
> result in a combinatorial explosion.
>
>> http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html
>>
>> Thanks,
>> Simeon
>>
>> On Sat, Feb 19, 2011 at 9:49 AM, Robert Newson <ro...@gmail.com> wrote:
>>> While this;
>>>
>>> function(doc) {
>>>  if (doc.type=="post") {
>>>    emit(null, null);
>>>  }
>>>  for (var i=0; i<doc.tags.length; i++) {
>>>    if (doc.tags[i] == "tag1") {
>>>      emit(null, null);
>>>    }
>>>  }
>>> }
>>>
>>> achieves your stated goal, I don't think it's what you're really
>>> asking for, right?
>>>
>>> B.
>>>
>>> On 19 February 2011 16:17, Simeon F. Willbanks <si...@simeons.net> wrote:
>>>> Hello,
>>>>
>>>> I'm trying to fetch a set of documents with OR logic. For example,
>>>> fetch all documents with type="post" OR tag="tag1". Here are a few
>>>> example documents with rev omitted for brevity:
>>>>
>>>> {
>>>>    "_id": 1,
>>>>    "type": "post",
>>>>    "tags": [
>>>>        "tag1",
>>>>        "tag2"
>>>>    ]
>>>> }
>>>>
>>>> {
>>>>    "_id": 2,
>>>>    "type": "photo",
>>>>    "tags": [
>>>>        "tag1",
>>>>        "tag3"
>>>>    ]
>>>> }
>>>>
>>>> {
>>>>    "_id": 3,
>>>>    "type": "photo",
>>>>    "tags": [
>>>>        "tag4",
>>>>        "tag5"
>>>>    ]
>>>> }
>>>>
>>>> I'd like to fetch documents with the ids 1 and 2.
>>>>
>>>> It seems this method will be my starting point:
>>>> "A JSON structure of {"keys": ["key1", "key2", ...]} can be posted to
>>>> any user defined view or _all_docs to retrieve just the view rows
>>>> matching that set of keys. Rows are returned in the order of the keys
>>>> specified. Combining this feature with include_docs=true results in
>>>> the so-called multi-document-fetch feature."
>>>> http://wiki.apache.org/couchdb/HTTP_view_API
>>>>
>>>> Am I correct so far? If yes, how would I define my map function? Would
>>>> I call emit multiple times and possibly use a group=true parameter?
>>>>
>>>> Thanks,
>>>> Simeon
>>>>
>>>
>>
>

Re: Query view with multiple keys with OR logic

Posted by Gabriel Farrell <gs...@gmail.com>.
On Sat, Feb 19, 2011 at 2:54 PM, Simeon F. Willbanks <si...@simeons.net> wrote:
> Correct. In retrospect, I am looking for an idiom or preferred way to
> achieve a dynamic view. My example states I'd like to query by
> type="post" OR tag="tag1", but I'd like the key values to be dynamic.
> My next request might be type="photo" OR tag="tag2". This would return
> IDs 1, 2 and 3.
>
> I understand temporary views are an option, but they aren't efficient.
> Would it be best to have two permanent views which can be queried by
> key and merge the results in the client? I could have one permanent
> view by_type and another by_tag. This technique looks to be described
> in the blog post below, but its from 2009.

Yes, I would run two queries and find the union of the returned IDs in
the client. There are some clever ways to do things otherwise but most
result in a combinatorial explosion.

> http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html
>
> Thanks,
> Simeon
>
> On Sat, Feb 19, 2011 at 9:49 AM, Robert Newson <ro...@gmail.com> wrote:
>> While this;
>>
>> function(doc) {
>>  if (doc.type=="post") {
>>    emit(null, null);
>>  }
>>  for (var i=0; i<doc.tags.length; i++) {
>>    if (doc.tags[i] == "tag1") {
>>      emit(null, null);
>>    }
>>  }
>> }
>>
>> achieves your stated goal, I don't think it's what you're really
>> asking for, right?
>>
>> B.
>>
>> On 19 February 2011 16:17, Simeon F. Willbanks <si...@simeons.net> wrote:
>>> Hello,
>>>
>>> I'm trying to fetch a set of documents with OR logic. For example,
>>> fetch all documents with type="post" OR tag="tag1". Here are a few
>>> example documents with rev omitted for brevity:
>>>
>>> {
>>>    "_id": 1,
>>>    "type": "post",
>>>    "tags": [
>>>        "tag1",
>>>        "tag2"
>>>    ]
>>> }
>>>
>>> {
>>>    "_id": 2,
>>>    "type": "photo",
>>>    "tags": [
>>>        "tag1",
>>>        "tag3"
>>>    ]
>>> }
>>>
>>> {
>>>    "_id": 3,
>>>    "type": "photo",
>>>    "tags": [
>>>        "tag4",
>>>        "tag5"
>>>    ]
>>> }
>>>
>>> I'd like to fetch documents with the ids 1 and 2.
>>>
>>> It seems this method will be my starting point:
>>> "A JSON structure of {"keys": ["key1", "key2", ...]} can be posted to
>>> any user defined view or _all_docs to retrieve just the view rows
>>> matching that set of keys. Rows are returned in the order of the keys
>>> specified. Combining this feature with include_docs=true results in
>>> the so-called multi-document-fetch feature."
>>> http://wiki.apache.org/couchdb/HTTP_view_API
>>>
>>> Am I correct so far? If yes, how would I define my map function? Would
>>> I call emit multiple times and possibly use a group=true parameter?
>>>
>>> Thanks,
>>> Simeon
>>>
>>
>

Re: Query view with multiple keys with OR logic

Posted by "Simeon F. Willbanks" <si...@simeons.net>.
Correct. In retrospect, I am looking for an idiom or preferred way to
achieve a dynamic view. My example states I'd like to query by
type="post" OR tag="tag1", but I'd like the key values to be dynamic.
My next request might be type="photo" OR tag="tag2". This would return
IDs 1, 2 and 3.

I understand temporary views are an option, but they aren't efficient.
Would it be best to have two permanent views which can be queried by
key and merge the results in the client? I could have one permanent
view by_type and another by_tag. This technique looks to be described
in the blog post below, but its from 2009.

http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html

Thanks,
Simeon

On Sat, Feb 19, 2011 at 9:49 AM, Robert Newson <ro...@gmail.com> wrote:
> While this;
>
> function(doc) {
>  if (doc.type=="post") {
>    emit(null, null);
>  }
>  for (var i=0; i<doc.tags.length; i++) {
>    if (doc.tags[i] == "tag1") {
>      emit(null, null);
>    }
>  }
> }
>
> achieves your stated goal, I don't think it's what you're really
> asking for, right?
>
> B.
>
> On 19 February 2011 16:17, Simeon F. Willbanks <si...@simeons.net> wrote:
>> Hello,
>>
>> I'm trying to fetch a set of documents with OR logic. For example,
>> fetch all documents with type="post" OR tag="tag1". Here are a few
>> example documents with rev omitted for brevity:
>>
>> {
>>    "_id": 1,
>>    "type": "post",
>>    "tags": [
>>        "tag1",
>>        "tag2"
>>    ]
>> }
>>
>> {
>>    "_id": 2,
>>    "type": "photo",
>>    "tags": [
>>        "tag1",
>>        "tag3"
>>    ]
>> }
>>
>> {
>>    "_id": 3,
>>    "type": "photo",
>>    "tags": [
>>        "tag4",
>>        "tag5"
>>    ]
>> }
>>
>> I'd like to fetch documents with the ids 1 and 2.
>>
>> It seems this method will be my starting point:
>> "A JSON structure of {"keys": ["key1", "key2", ...]} can be posted to
>> any user defined view or _all_docs to retrieve just the view rows
>> matching that set of keys. Rows are returned in the order of the keys
>> specified. Combining this feature with include_docs=true results in
>> the so-called multi-document-fetch feature."
>> http://wiki.apache.org/couchdb/HTTP_view_API
>>
>> Am I correct so far? If yes, how would I define my map function? Would
>> I call emit multiple times and possibly use a group=true parameter?
>>
>> Thanks,
>> Simeon
>>
>

Re: Query view with multiple keys with OR logic

Posted by Robert Newson <ro...@gmail.com>.
While this;

function(doc) {
  if (doc.type=="post") {
    emit(null, null);
  }
  for (var i=0; i<doc.tags.length; i++) {
    if (doc.tags[i] == "tag1") {
      emit(null, null);
    }
  }
}

achieves your stated goal, I don't think it's what you're really
asking for, right?

B.

On 19 February 2011 16:17, Simeon F. Willbanks <si...@simeons.net> wrote:
> Hello,
>
> I'm trying to fetch a set of documents with OR logic. For example,
> fetch all documents with type="post" OR tag="tag1". Here are a few
> example documents with rev omitted for brevity:
>
> {
>    "_id": 1,
>    "type": "post",
>    "tags": [
>        "tag1",
>        "tag2"
>    ]
> }
>
> {
>    "_id": 2,
>    "type": "photo",
>    "tags": [
>        "tag1",
>        "tag3"
>    ]
> }
>
> {
>    "_id": 3,
>    "type": "photo",
>    "tags": [
>        "tag4",
>        "tag5"
>    ]
> }
>
> I'd like to fetch documents with the ids 1 and 2.
>
> It seems this method will be my starting point:
> "A JSON structure of {"keys": ["key1", "key2", ...]} can be posted to
> any user defined view or _all_docs to retrieve just the view rows
> matching that set of keys. Rows are returned in the order of the keys
> specified. Combining this feature with include_docs=true results in
> the so-called multi-document-fetch feature."
> http://wiki.apache.org/couchdb/HTTP_view_API
>
> Am I correct so far? If yes, how would I define my map function? Would
> I call emit multiple times and possibly use a group=true parameter?
>
> Thanks,
> Simeon
>