How to create index search in CouchDB?

16,277

Solution 1

The solution I was searching for for was based on this library.

  1. I had to install CouchDB 1.6.1 to have available database on http://localhost:5984,
  2. the next step was to install couchdb-lucene, which was running on http://localhost:5985 with successfull response. It is maven based app.

{"couchdb-lucene":"Welcome","version":"1.1.0-SNAPSHOT"}

To make it run I had to build it in the root directory with mvn and then navigate to target and run command ./bin/run in the unzipped couchdb-lucene:

root@mario-VirtualBox:/home/mario/CouchDB_mario/couchdb-lucene/target/couchdb-lucene-1.1.0-SNAPSHOT# ./bin/run
  1. The next constraint was to connect these two servers together. And all I had to do was to map them via proxy in the /etc/couchdb/local.ini

All what you need to have there is the following piece of code:

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

Thanks to which, I was able to finally query CouchDB using Apache Lucene indexing.

  1. Before querying I had to insert my custom JSON Design Document, not new design through the UI, neither new view, but new JSON Document. Essentially hacking CouchDB a little bit with faked design so that could support Lucene search. I've used CURL request with the following format

curl -X PUT http://localhost:5984/user14169_slovnik_medical/_design/medical -d @user14169_slovnik_medical.json

Where the JSON Design Document looked like this:

 {
   "_id": "_design/medical",
   "fulltext": {
       "by_meaning": {
           "index": "function(doc) { var ret=new Document(); ret.add(doc.vyznam); return ret }"
       },
       "by_shortcut": {
           "index": "function(doc) { var ret=new Document(); ret.add(doc.zkratka); return ret }"
       }
   }
}
  1. As an example. Having this search index defined and let's say this type of data in the JSON Documents:
  {
     "_id": "63e5c848fa2211c3b063d6feccd3d942",
     "_rev": "1-899a6924ed08097b1a37e497d91726fd",
     "DATAWORKS_DOCUMENT_TYPE": "user14169_slovnik_medical",
     "vyznam": "End to side",
     "zkratka": "e-t-s"
   }

Then you are easiliy able to achieve queries like this:

http://localhost:5984/_fti/local/user14169_slovnik_medical/_design/medical/by_meaning?q=lob~

Which returns the expected data: enter image description here

The local prefix is because I am running the database on localhost on 1 node and by default couchdb-lucene is connecting to the localhost.

The coolest thing is that you are able to use client API org.lightcouch jar library in Java and do some easy calls like this:

CouchDbClient dbClient = new CouchDbClient("user14169_slovnik_medical", true, "http", "127.0.0.1", 5984, null, null);

String uriFullText = dbClient.getBaseUri() + "_fti/local/user14169_slovnik_medical/_design/medical/by_shortcut?q=lob*";

JsonObject result = dbClient.findAny(JsonObject.class, uriFullText);

System.out.println(result.toString());

Solution 2

To create a search index, you need to create a "view". This is well explained here: http://guide.couchdb.org/editions/1/en/views.html

You can do it right from Futon but you should practice first with a small dataset.

Solution 3

You can't. Search is a Cloudant feature and is not in any current release of CouchDB.

Share:
16,277
user2670818
Author by

user2670818

Updated on June 04, 2022

Comments

  • user2670818
    user2670818 almost 2 years

    Assuming configuring couchDB locally, how and where to create the search index similarly to Cloudant on Bluemix?

    enter image description here

  • rhyshort
    rhyshort about 8 years
    Best to ask the author.
  • user2670818
    user2670818 almost 8 years
  • rhyshort
    rhyshort almost 8 years
    It may work with CouchDB 2.0 but that's not released yet.
  • user2670818
    user2670818 almost 8 years
    Is this solution equal to Apache lucene search queries? with *, fuzzy search etc?