I use this for a web service that requests all my documents and returns every document that matches both the existence of the node and the request. In this example, I use node 'detail' to search. If you want to find another node, you need to specify.
This is my first post, so I hope I can help someone :)
***Python Code import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import httplib, json from tornado.options import define,options define("port", default=8000, help="run on the given port", type=int) class MainHandler(tornado.web.RequestHandler): def get(self): db_host = 'YOUR_COUCHDB_SERVER' db_port = 5984 db_name = 'YOUR_COUCHDB_DATABASE' node = self.get_argument('node',None) query = self.get_argument('query',None) cleared = None cleared = 1 if node else self.write('You have not supplied an object node.<br>') cleared = 2 if query else self.write('You have not supplied a query string.<br>') if cleared is 2: uri = ''.join(['/', db_name, '/', '_design/keysearch/_view/' + node + '/?startkey="' + query + '"&endkey="' + query + '\u9999"']) connection = httplib.HTTPConnection(db_host, db_port) headers = {"Accept": "application/json"} connection.request("GET", uri, None, headers) response = connection.getresponse() self.write(json.dumps(json.loads(response.read()), sort_keys=True, indent=4)) class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", MainHandler) ] settings = dict( debug = True ) tornado.web.Application.__init__(self, handlers, **settings) def main(): tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(Application()) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start() if __name__ == '__main__': main() ***CouchDB Design View { "_id": "_design/keysearch", "language": "javascript", "views": { "detail": { "map": "function(doc) { var docs = doc['detail'].match(/[A-Za-z0-9]+/g); if(docs) { for(var each in docs) { emit(docs[each],doc); } } }" } } }
source share