You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Craig McInnes <cr...@gmail.com> on 2009/02/19 14:49:36 UTC

couchdb-python, calling a permanent view

Hi,

I'm having difficulty getting results from a permanent view using
couchdb-python.
I'm not sure this is the correct place to ask. Apologies if it is not.

I've successfully called temporary views and stepped through results (just
to verify them), but my understanding is that temporary views do not build a
permanent b-tree index on the data (which is what I'd really like to be
accessing).

I created a permanent view a few different ways:

   - via futon:
   In Map Function I've added the barebones map fn:
   function(doc){emit(doc._id, null);}
   and hit SaveAS, named the view, the view is now accessible via the drop
   down.

   - via couchdb-python:
   <python snippet>
   view = ViewDefinition('mytest', 'myall', '''function(doc) {emit(doc._id,
   null);}''')
   view.get_doc(db)
   view.sync(db)
   </python snippet>
   and this also is accessible via the drop down in futon.

   - via couchdb-python:
   <python snippet>
   class MyDoc(Document):
       type = TextField()
       tags = ListField(IntegerField)
       added = DateTimeField(default=datetime.datetime.now())
       myDocByTag = View('mydocbytag', '''function(doc) {emit(doc._id,
   null);}''')
   </python snippet>

   I then create instances of this class and store them using: <class
   instance>.store(db)
   The class instances are correctly stored as documents in couchdb.
   This view is *not* stored in couchdb (i.e. I can't see it in the drop
   down in futon).
   Trying to invoke this view via:
   res = MyDoc.myDocByTag(db, count=3)
   print str(res)
   print dir(res)
   for r in res:
     print str(r)

   returns:
   <ViewResults <PermanentView '_view/mydocbytag/byTag'> {'count': 3}>

   ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
   '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__module__',
   '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
   '__str__', '__weakref__', '_fetch', '_get_offset', '_get_rows',
   '_get_total_rows', '_offset', '_rows', '_total_rows', 'offset', 'options',
   'rows', 'total_rows', 'view']

   Traceback (most recent call last):
     File "ctest.py", line 277, in <module>
       for r in res:
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 727, in __iter__
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 744, in _get_rows
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 737, in _fetch
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 627, in _exec
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 832, in get
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 884, in _request


Discounting the last view, I can't seem to access the first two via
couchdb-python.

I've tried:
view = db.view('mytest/myall')
print str(view)
>>> <ViewResults <PermanentView '_view/mytest/myall'> {}>
So it's definitely finding the view correctly
(otherwise I'd see a:
couchdb.client.ResourceNotFound: (u'not_found', u'missing')
error).

for r in view:
  print r.id
# this prints nothing, there are no rows in view


I've tried messing around with the PermanentView class, or invoking the
permanent view via the MyDoc class, but no luck.

I'm sure I'm missing something simple, but I need some help.
Apologies for the long winded email.

Cheers,
-Craig

Re: couchdb-python, calling a permanent view

Posted by Domingo Aguilera <do...@gmail.com>.
Have you tried to use limit instead of count ?

On Thu, Feb 19, 2009 at 7:49 AM, Craig McInnes <cr...@gmail.com>wrote:

> Hi,
>
> I'm having difficulty getting results from a permanent view using
> couchdb-python.
> I'm not sure this is the correct place to ask. Apologies if it is not.
>
> I've successfully called temporary views and stepped through results (just
> to verify them), but my understanding is that temporary views do not build
> a
> permanent b-tree index on the data (which is what I'd really like to be
> accessing).
>
> I created a permanent view a few different ways:
>
>   - via futon:
>   In Map Function I've added the barebones map fn:
>   function(doc){emit(doc._id, null);}
>   and hit SaveAS, named the view, the view is now accessible via the drop
>   down.
>
>   - via couchdb-python:
>   <python snippet>
>   view = ViewDefinition('mytest', 'myall', '''function(doc) {emit(doc._id,
>   null);}''')
>   view.get_doc(db)
>   view.sync(db)
>   </python snippet>
>   and this also is accessible via the drop down in futon.
>
>   - via couchdb-python:
>   <python snippet>
>   class MyDoc(Document):
>       type = TextField()
>       tags = ListField(IntegerField)
>       added = DateTimeField(default=datetime.datetime.now())
>       myDocByTag = View('mydocbytag', '''function(doc) {emit(doc._id,
>   null);}''')
>   </python snippet>
>
>   I then create instances of this class and store them using: <class
>   instance>.store(db)
>   The class instances are correctly stored as documents in couchdb.
>   This view is *not* stored in couchdb (i.e. I can't see it in the drop
>   down in futon).
>   Trying to invoke this view via:
>   res = MyDoc.myDocByTag(db, count=3)
>   print str(res)
>   print dir(res)
>   for r in res:
>     print str(r)
>
>   returns:
>   <ViewResults <PermanentView '_view/mydocbytag/byTag'> {'count': 3}>
>
>   ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
>   '__getitem__', '__hash__', '__init__', '__iter__', '__len__',
> '__module__',
>   '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
>   '__str__', '__weakref__', '_fetch', '_get_offset', '_get_rows',
>   '_get_total_rows', '_offset', '_rows', '_total_rows', 'offset',
> 'options',
>   'rows', 'total_rows', 'view']
>
>   Traceback (most recent call last):
>     File "ctest.py", line 277, in <module>
>       for r in res:
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 727, in __iter__
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 744, in _get_rows
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 737, in _fetch
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 627, in _exec
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 832, in get
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 884, in _request
>
>
> Discounting the last view, I can't seem to access the first two via
> couchdb-python.
>
> I've tried:
> view = db.view('mytest/myall')
> print str(view)
> >>> <ViewResults <PermanentView '_view/mytest/myall'> {}>
> So it's definitely finding the view correctly
> (otherwise I'd see a:
> couchdb.client.ResourceNotFound: (u'not_found', u'missing')
> error).
>
> for r in view:
>  print r.id
> # this prints nothing, there are no rows in view
>
>
> I've tried messing around with the PermanentView class, or invoking the
> permanent view via the MyDoc class, but no luck.
>
> I'm sure I'm missing something simple, but I need some help.
> Apologies for the long winded email.
>
> Cheers,
> -Craig
>

couchdb-python, calling a permanent view

Posted by Craig McInnes <cr...@gmail.com>.
Hi,

{Resending - I hadn't confirmed subscription to the list before sending the
first time. Assume this means first send treated as spam.}

I'm having difficulty getting results from a permanent view using
couchdb-python.
I'm not sure this is the correct place to ask. Apologies if it is not.

I've successfully called temporary views and stepped through results (just
to verify them), but my understanding is that temporary views do not build a
permanent b-tree index on the data (which is what I'd really like to be
accessing).

I created a permanent view a few different ways:

   - via futon:
   In Map Function I've added the barebones map fn:
   function(doc){emit(doc._id, null);}
   and hit SaveAS, named the view, the view is now accessible via the drop
   down.

   - via couchdb-python:
   <python snippet>
   view = ViewDefinition('mytest', 'myall', '''function(doc) {emit(doc._id,
   null);}''')
   view.get_doc(db)
   view.sync(db)
   </python snippet>
   and this also is accessible via the drop down in futon.

   - via couchdb-python:
   <python snippet>
   class MyDoc(Document):
       type = TextField()
       tags = ListField(IntegerField)
       added = DateTimeField(default=datetime.datetime.now())
       myDocByTag = View('mydocbytag', '''function(doc) {emit(doc._id,
   null);}''')
   </python snippet>

   I then create instances of this class and store them using: <class
   instance>.store(db)
   The class instances are correctly stored as documents in couchdb.
   This view is *not* stored in couchdb (i.e. I can't see it in the drop
   down in futon).
   Trying to invoke this view via:
   res = MyDoc.myDocByTag(db, count=3)
   print str(res)
   print dir(res)
   for r in res:
     print str(r)

   returns:
   <ViewResults <PermanentView '_view/mydocbytag/byTag'> {'count': 3}>

   ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
   '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__module__',
   '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
   '__str__', '__weakref__', '_fetch', '_get_offset', '_get_rows',
   '_get_total_rows', '_offset', '_rows', '_total_rows', 'offset', 'options',
   'rows', 'total_rows', 'view']

   Traceback (most recent call last):
     File "ctest.py", line 277, in <module>
       for r in res:
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 727, in __iter__
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 744, in _get_rows
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 737, in _fetch
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 627, in _exec
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 832, in get
     File
   "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 884, in _request


Discounting the last view, I can't seem to access the first two via
couchdb-python.

I've tried:
view = db.view('mytest/myall')
print str(view)
>>> <ViewResults <PermanentView '_view/mytest/myall'> {}>
So it's definitely finding the view correctly
(otherwise I'd see a:
couchdb.client.ResourceNotFound: (u'not_found', u'missing')
error).

for r in view:
  print r.id
# this prints nothing, there are no rows in view


I've tried messing around with the PermanentView class, or invoking the
permanent view via the MyDoc class, but no luck.

I'm sure I'm missing something simple, but I need some help.
Apologies for the long winded email.

Cheers,
-Craig

Re: couchdb-python, calling a permanent view

Posted by Craig McInnes <cr...@gmail.com>.
Thanks for the help.

@Domingo: Have you tried to use limit instead of count ?
Just tried it:
>>> res = MyDoc.byTag(db, limit=3)
>>> res
<ViewResults <PermanentView '_view/mydocbytag/byTag'> {'limit': 3}>
>>> for r in res:
...     print r
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
line 727, in __iter__
  File
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
line 744, in _get_rows
  File
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
line 737, in _fetch
  File
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
line 627, in _exec
  File
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
line 832, in get
  File
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
line 890, in _request
couchdb.client.ServerError: (500, (u'query_parse_error', u'Bad URL query
key:limit'))

@Boyd:
Nope, I was already saving the view using view.sync.
I'm not sure you're correct about couchdb-python not correctly handing
emit(null), I haven't looked into though, but in couch_tests.py it creates
and calls an all_docs view with
function(doc) { emit(doc.integer, null) }
which works correctly.


I believe I have calling a permanent view working correctly, I used the same
access methods as in couch_tests.py.


# create the view
db[viewDocName] = {'views': {'myview_all': {'map': 'function(doc) {
emit(doc._id, doc.name)}'}}}

# use view
res = db.view('_view/test/myview_all')
for r in res:
...     print r
...
<Row id=u'1fa88a5559ad7ef0e2d60bdb8026406e',
key=u'1fa88a5559ad7ef0e2d60bdb8026406e', value=u'Keiko1'>
<Row id=u'2afaca0142c384c3b2e9144a0e10e8dd',
key=u'2afaca0142c384c3b2e9144a0e10e8dd', value=u'OBrien2'>
...etc.

I can close this interpreter instance, open a new one, connect to the db and
call
res = db.view('_view/test/myview_all')
without having to re-create the view.
This also works with views created via:
view = ViewDefinition('tests', 'all', '''function(doc) {emit(doc._id,
doc.name);}''')
view.get_doc(db)
view.sync(db)


What was I doing wrong?
I started out using db.query() rather than db.view().
db.query() is for temporary views only.

Then I was trying to call the view using and incorrect prefix:
res = db.view('_design/test/myview_all')
as that's what the view location looked like in Futon web panel.

I think the correct way to call is:
res = db.view('_view/test/myview_all')
or the db.view fn adds the '_view' prefix if you only specify:
res = db.view('test/myview_all')

Silly Craig. :/


On Sun, Feb 22, 2009 at 4:37 PM, Boyd Ebsworthy <be...@gmail.com>wrote:

> Hi,
>
> I think you need to tell couchdb-python to save the view.
>
> from couchdb.design import ViewDefinition
> ViewDefinition.sync_many(db, [
>    MyDoc.myDocByTag, MyDoc.MyOtherView
> ])
>
> Also,couchdb-python seems to fail if emit return a null value.
>
> # This is ok
> by_name = View('user', '''\
>              function(user) {
>                  emit(user.name, user);
>              }''')
>
> # This is ok (although not all user info are loaded of course..)
> by_name = View('user', '''\
>              function(user) {
>                  emit(user.name, {});
>              }''')
>
> # Couchdb-python doesn't check for null value and an assigment fails..
> by_name = View('user', '''\
>              function(user) {
>                  emit(user.name, null);
>              }''')
>
> Hope that helps.
>
> -----Original Message-----
> From: Craig McInnes [mailto:craig.mcinnes@gmail.com]
> Sent: Thursday, February 19, 2009 17:50
> To: user@couchdb.apache.org
> Subject: couchdb-python, calling a permanent view
>
> Hi,
>
> I'm having difficulty getting results from a permanent view using
> couchdb-python.
> I'm not sure this is the correct place to ask. Apologies if it is not.
>
> I've successfully called temporary views and stepped through results (just
> to verify them), but my understanding is that temporary views do not build
> a
> permanent b-tree index on the data (which is what I'd really like to be
> accessing).
>
> I created a permanent view a few different ways:
>
>   - via futon:
>   In Map Function I've added the barebones map fn:
>   function(doc){emit(doc._id, null);}
>   and hit SaveAS, named the view, the view is now accessible via the drop
>   down.
>
>   - via couchdb-python:
>   <python snippet>
>   view = ViewDefinition('mytest', 'myall', '''function(doc) {emit(doc._id,
>   null);}''')
>   view.get_doc(db)
>   view.sync(db)
>   </python snippet>
>   and this also is accessible via the drop down in futon.
>
>   - via couchdb-python:
>   <python snippet>
>   class MyDoc(Document):
>       type = TextField()
>       tags = ListField(IntegerField)
>       added = DateTimeField(default=datetime.datetime.now())
>       myDocByTag = View('mydocbytag', '''function(doc) {emit(doc._id,
>   null);}''')
>   </python snippet>
>
>   I then create instances of this class and store them using: <class
>   instance>.store(db)
>   The class instances are correctly stored as documents in couchdb.
>   This view is *not* stored in couchdb (i.e. I can't see it in the drop
>   down in futon).
>   Trying to invoke this view via:
>   res = MyDoc.myDocByTag(db, count=3)
>   print str(res)
>   print dir(res)
>   for r in res:
>     print str(r)
>
>   returns:
>   <ViewResults <PermanentView '_view/mydocbytag/byTag'> {'count': 3}>
>
>   ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
>   '__getitem__', '__hash__', '__init__', '__iter__', '__len__',
> '__module__',
>   '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
>   '__str__', '__weakref__', '_fetch', '_get_offset', '_get_rows',
>   '_get_total_rows', '_offset', '_rows', '_total_rows', 'offset',
> 'options',
>   'rows', 'total_rows', 'view']
>
>   Traceback (most recent call last):
>     File "ctest.py", line 277, in <module>
>       for r in res:
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 727, in __iter__
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 744, in _get_rows
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 737, in _fetch
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 627, in _exec
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 832, in get
>     File
>
> "/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
>   line 884, in _request
>
>
> Discounting the last view, I can't seem to access the first two via
> couchdb-python.
>
> I've tried:
> view = db.view('mytest/myall')
> print str(view)
> >>> <ViewResults <PermanentView '_view/mytest/myall'> {}>
> So it's definitely finding the view correctly
> (otherwise I'd see a:
> couchdb.client.ResourceNotFound: (u'not_found', u'missing')
> error).
>
> for r in view:
>  print r.id
> # this prints nothing, there are no rows in view
>
>
> I've tried messing around with the PermanentView class, or invoking the
> permanent view via the MyDoc class, but no luck.
>
> I'm sure I'm missing something simple, but I need some help.
> Apologies for the long winded email.
>
> Cheers,
> -Craig
>
>

RE: couchdb-python, calling a permanent view

Posted by Boyd Ebsworthy <be...@gmail.com>.
Hi,

I think you need to tell couchdb-python to save the view.

from couchdb.design import ViewDefinition
ViewDefinition.sync_many(db, [
    MyDoc.myDocByTag, MyDoc.MyOtherView
])

Also,couchdb-python seems to fail if emit return a null value.

# This is ok
by_name = View('user', '''\
              function(user) {
                  emit(user.name, user);
              }''')

# This is ok (although not all user info are loaded of course..)
by_name = View('user', '''\
              function(user) {
                  emit(user.name, {});
              }''')

# Couchdb-python doesn't check for null value and an assigment fails..
by_name = View('user', '''\
              function(user) {
                  emit(user.name, null);
              }''')

Hope that helps.

-----Original Message-----
From: Craig McInnes [mailto:craig.mcinnes@gmail.com] 
Sent: Thursday, February 19, 2009 17:50
To: user@couchdb.apache.org
Subject: couchdb-python, calling a permanent view

Hi,

I'm having difficulty getting results from a permanent view using
couchdb-python.
I'm not sure this is the correct place to ask. Apologies if it is not.

I've successfully called temporary views and stepped through results (just
to verify them), but my understanding is that temporary views do not build a
permanent b-tree index on the data (which is what I'd really like to be
accessing).

I created a permanent view a few different ways:

   - via futon:
   In Map Function I've added the barebones map fn:
   function(doc){emit(doc._id, null);}
   and hit SaveAS, named the view, the view is now accessible via the drop
   down.

   - via couchdb-python:
   <python snippet>
   view = ViewDefinition('mytest', 'myall', '''function(doc) {emit(doc._id,
   null);}''')
   view.get_doc(db)
   view.sync(db)
   </python snippet>
   and this also is accessible via the drop down in futon.

   - via couchdb-python:
   <python snippet>
   class MyDoc(Document):
       type = TextField()
       tags = ListField(IntegerField)
       added = DateTimeField(default=datetime.datetime.now())
       myDocByTag = View('mydocbytag', '''function(doc) {emit(doc._id,
   null);}''')
   </python snippet>

   I then create instances of this class and store them using: <class
   instance>.store(db)
   The class instances are correctly stored as documents in couchdb.
   This view is *not* stored in couchdb (i.e. I can't see it in the drop
   down in futon).
   Trying to invoke this view via:
   res = MyDoc.myDocByTag(db, count=3)
   print str(res)
   print dir(res)
   for r in res:
     print str(r)

   returns:
   <ViewResults <PermanentView '_view/mydocbytag/byTag'> {'count': 3}>

   ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
   '__getitem__', '__hash__', '__init__', '__iter__', '__len__',
'__module__',
   '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
   '__str__', '__weakref__', '_fetch', '_get_offset', '_get_rows',
   '_get_total_rows', '_offset', '_rows', '_total_rows', 'offset',
'options',
   'rows', 'total_rows', 'view']

   Traceback (most recent call last):
     File "ctest.py", line 277, in <module>
       for r in res:
     File
 
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 727, in __iter__
     File
 
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 744, in _get_rows
     File
 
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 737, in _fetch
     File
 
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 627, in _exec
     File
 
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 832, in get
     File
 
"/usr/lib/python2.5/site-packages/CouchDB-0.5-py2.5.egg/couchdb/client.py",
   line 884, in _request


Discounting the last view, I can't seem to access the first two via
couchdb-python.

I've tried:
view = db.view('mytest/myall')
print str(view)
>>> <ViewResults <PermanentView '_view/mytest/myall'> {}>
So it's definitely finding the view correctly
(otherwise I'd see a:
couchdb.client.ResourceNotFound: (u'not_found', u'missing')
error).

for r in view:
  print r.id
# this prints nothing, there are no rows in view


I've tried messing around with the PermanentView class, or invoking the
permanent view via the MyDoc class, but no luck.

I'm sure I'm missing something simple, but I need some help.
Apologies for the long winded email.

Cheers,
-Craig