Search code examples
lucenecouchdbcloudant

Developing Cloudant-based application locally


I know that CouchDB can be used to locally develop an application and switch to Cloudant in staging / production. This is also covered in this question.

But how do you develop application functionality that requires the lucene search of Cloudant? Is there a way to emulate it locally?


Solution

  • Dmi, I didnot have much experience with the Cloudant. But, as per the CouchDB Lucene we can emulate it locally and according to this link, https://docs.cloudant.com/api/search.html I think CouchDB Lucene works good (which we emulated locally) on the Cloudant.

    Follow this steps:

    1) Follow the official installation instructions from here: https://github.com/rnewson/couchdb-lucene#build-and-run-couchdb-lucene

    2) Once done, you will have to edit your CouchDB configuration file (/etc/couchdb/local.ini), adding the following options:

    timeout = 60000
    [httpd_global_handlers]
    _fti = {couch_httpd_proxy, handle_proxy_req, <<"http://localhost:5985">>}
    [couchdb]
    timeout = 60000

    Here, localhost:5985 is the URL of your CouchDB-Lucene service. Of course, you can modify the port lucene is listening to in the couchdb-lucene.ini configuration file.

    3) Create a new design document in CouchDB, let's call it _design/lucene. Here is how it looks like (as always when creating design documents, be careful to escape your javascript functions):

    {
       "_id": "_design/lucene",
       "_rev": "23-b7e715e927d362bc2de8d7716a29947f",
       "fulltext": {
           "people": {
               "index": "function(doc) {\n\tvar ret = new Document();
               \n\tret.add(doc.gender,{'field':'gender', 'store':'yes'});
               \n\tret.add(doc.age,{'field':'age', 'store':'yes'});
               \n\tret.add(doc.name,{'field':'name', 'store':'yes'});
               \n\tret.add(doc.city,{'field':'city', 'store':'yes'});
               \n\tret.add(doc.lastEmployer,{'field':'lastEmployer', 'store':'yes'});
               \n\treturn ret;\n}"
           }
       }
    }

    4) Now question is, how to send the requests to your newly created Lucene index:

    try like this:

    http://127.0.0.1:5984/_fti/local/dbname/_design/lucene/people?q=gender:F
    AND age:[20 TO 40] AND city:s*&force_json=true&include_docs=true&sort=age

    Here, “people” is the name of your index, as defined in the design document.

    For a complete syntax reference, you can read the official documentation (http://lucene.apache.org/core/2_9_4/queryparsersyntax.html).

    I didnot think any change is their for Lucene queries to run the Cloudant, refer to the above link I posted.