Enables full-text searching of CouchDB documents using Lucene
Clone or download
Pull request Compare This branch is 824 commits behind rnewson:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



This documentation is slightly ahead of the code; the "language" and "analyzer" options are not yet available.


The indexing API in 0.3 has changed since 0.2 to allow multiple design documents and "views" into Lucene. It will moves the Lucene-specific stuff into an options object.

Issue Tracking

Issue tracking at github.

System Requirements

Sun JDK 5 or higher is necessary. Couchdb-lucene is known to be incompatible with OpenJDK as it includes an earlier, and incompatible, version of the Rhino Javascript library.

Build couchdb-lucene

  1. Install Maven 2.
  2. checkout repository
  3. type 'mvn'
  4. configure couchdb (see below)

Configure CouchDB

os_process_timeout=60000 ; increase the timeout from 5 seconds.

fti=/usr/bin/java -jar /path/to/couchdb-lucene*-jar-with-dependencies.jar -search

indexer=/usr/bin/java -jar /path/to/couchdb-lucene*-jar-with-dependencies.jar -index

_fti = {couch_httpd_external, handle_external_req, <<"fti">>}

Indexing Strategy

Document Indexing

You must supply a index function in order to enable couchdb-lucene as by default, nothing will be indexed.

You may add any number of index views in any number of design documents. All searches will be constrained to documents emitted by the index functions.

Declare your functions as follows;

  "fulltext": {
    "by_subject": {
      "defaults": { "store":"yes" },
      "index":"function(doc) { var ret=new Document(); ret.add(doc.subject); return ret }"
    "french_documents": {
      "defaults": { "language":"fr" },
      "index":"function(doc) { if (doc.language != "fr") { return null;} var ret=new Document(); etc return ret;  }"

A fulltext object contains multiple index view declarations. An index view consists of;

The default for numerous indexing options can be overridden here. A full list of options follows.
The indexing function itself, documented below.

The Defaults Object

The following indexing options can be defaulted;

name description available options default
field the field name to index under user-defined default
store whether the data is stored. The value will be returned in the search result. yes, no no
index whether (and how) the data is indexed analyzed, analyzed_no_norms, no, not_analyzed, not_analyzed_no_norms analyzed
analyzer how the data is analyzed auto, simple, standard auto
language which language the data is in auto, br, cjk, cn, cz, de, el, en, fr, nl, ru, th en

The Document class

You may construct a new Document instance with;

var doc = new Document();

Data may be added to this document with the add method which takes an optional second object argument that can override any of the above default values.

The data is usually interpreted as a String but couchdb-lucene provides special handling if a Javascript Date object is passed. Specifically, the date is indexed as a numeric value, which allows correct sorting, and stored (if requested) in ISO 8601 format (with a timezone marker).

// Add with all the defaults.

// Add a subject field.
doc.add("this is the subject line.", {"field":"subject"});

// Add but ensure it's stored.
doc.add("value", {"store":"yes"});

// Add but don't analyze.
doc.add("don't analyze me", {"index":"not_analyzed"});

// Extract text from the named attachment and index it (but not store it).
doc.attachment("attachment name", {"field":"attachments"});

Example Transforms

Index Everything

function(doc) {
    var ret = new Document();

    function idx(obj) {
	for (var key in obj) {
	    switch (typeof obj[key]) {
	    case 'object':
	    case 'function':


    if (doc._attachments) {
	for (var i in doc._attachments) {
	    ret.attachment("attachment", i);
    return ret;

Index Nothing

function(doc) {
  return null;

Index Select Fields

function(doc) {
  var result = new Document();
  result.add(doc.subject, {"field":"subject", "store":"yes"});
  result.add(doc.content, {"field":"subject"});
  return result;

Index Attachments

function(doc) {
  var result = new Document();
  for(var a in doc._attachments) {
    result.add_attachment(a, {"field":"attachment"});
  return result;

A More Complex Example

function(doc) {
    var mk = function(name, value, group) {
        var ret = new Document();
        ret.add(value, {"field": group, "store":"yes"});
        ret.add(group, {"field":"group", "store":"yes"});
        return ret;
    var ret = [];
    if(doc.type != "reference") return null;
    for(var g in doc.groups) {
        ret.add(mk("library", doc.groups[g].library, g));
        ret.add(mk("method", doc.groups[g].method, g));
        ret.add(mk("target", doc.groups[g].target, g));
    return ret;

Attachment Indexing

Couchdb-lucene uses Apache Tika to index attachments of the following types, assuming the correct content_type is set in couchdb;

Supported Formats

  • Excel spreadsheets (application/vnd.ms-excel)
  • Word documents (application/msword)
  • Powerpoint presentations (application/vnd.ms-powerpoint)
  • Visio (application/vnd.visio)
  • Outlook (application/vnd.ms-outlook)
  • XML (application/xml)
  • HTML (text/html)
  • Images (image/*)
  • Java class files
  • Java jar archives
  • MP3 (audio/mp3)
  • OpenDocument (application/vnd.oasis.opendocument.*)
  • Plain text (text/plain)
  • PDF (application/pdf)
  • RTF (application/rtf)

Searching with couchdb-lucene

You can perform all types of queries using Lucene's default query syntax. The _body field is searched by default which will include the extracted text from all attachments. The following parameters can be passed for more sophisticated searches;

the query to run (e.g, subject:hello). If not specified, the default field is searched.
The language that the query parameter is in. Available options, and the default if not specified, are identical to the language option specified above.
the comma-separated fields to sort on. Prefix with / for ascending order and \ for descending order (ascending is the default if not specified).
the maximum number of results to return
the number of results to skip
whether to include the source docs
If you set the stale option ok, couchdb-lucene may not perform any refreshing on the index. Searches may be faster as Lucene caches important data (especially for sorting). A query without stale=ok will use the latest data committed to the index.
if false, a normal application/json response with results appears. if true, an pretty-printed HTML blob is returned instead.
(EXPERT) if true, returns a json response with a rewritten query and term frequencies. This allows correct distributed scoring when combining the results from multiple nodes.

All parameters except 'q' are optional.

Special Fields

The source database of the document.
The _id of the document.

Dublin Core

All Dublin Core attributes are indexed and stored if detected in the attachment. Descriptions of the fields come from the Tika javadocs.

An entity responsible for making contributions to the content of the resource.
The extent or scope of the content of the resource.
An entity primarily responsible for making the content of the resource.
A date associated with an event in the life cycle of the resource.
An account of the content of the resource.
Typically, Format may include the media-type or dimensions of the resource.
Recommended best practice is to identify the resource by means of a string or number conforming to a formal identification system.
A language of the intellectual content of the resource.
Date on which the resource was changed.
An entity responsible for making the resource available.
A reference to a related resource.
Information about rights held in and over the resource.
A reference to a resource from which the present resource is derived.
The topic of the content of the resource.
A name given to the resource.
The nature or genre of the content of the resource.


http://localhost:5984/dbname/_fti/design_doc/view_name?debug=true&sort=billing_size&q=body:document AND customer:[A TO C]

Search Results Format

The search result contains a number of fields at the top level, in addition to your search results.

The query that was executed.
An opaque token that reflects the current version of the index. This value is also returned in an ETag header to facilitate HTTP caching.
The number of initial matches that was skipped.
The maximum number of results that can appear.
The total number of matches for this query.
The number of milliseconds spent performing the search.
The number of milliseconds spent retrieving the documents.
The search results array, described below.

The search results array

The search results arrays consists of zero, one or more objects with the following fields;

The unique identifier for this match.
The normalized score (0.0-1.0, inclusive) for this match
All the fields that were stored with this match
The original document from couch, if requested with include_docs=true

Here's an example of a JSON response without sorting;

  "q": "+content:enron",
  "skip": 0,
  "limit": 2,
  "total_rows": 176852,
  "search_duration": 518,
  "fetch_duration": 4,
  "rows":   [
      "id": "hain-m-all_documents-257.",
      "score": 1.601625680923462
      "id": "hain-m-notes_inbox-257.",
      "score": 1.601625680923462

And the same with sorting;

  "q": "+content:enron",
  "skip": 0,
  "limit": 3,
  "total_rows": 176852,
  "search_duration": 660,
  "fetch_duration": 4,
  "sort_order":   [
      "field": "source",
      "reverse": false,
      "type": "string"
      "reverse": false,
      "type": "doc"
  "rows":   [
      "id": "shankman-j-inbox-105.",
      "score": 0.6131107211112976,
      "sort_order":       [
      "id": "shankman-j-inbox-8.",
      "score": 0.7492915391921997,
      "sort_order":       [
      "id": "shankman-j-inbox-30.",
      "score": 0.507369875907898,
      "sort_order":       [

Fetching information about the index

Calling couchdb-lucene without arguments returns a JSON object with information about the whole index.



Working With The Source

To develop "live", type "mvn dependency:unpack-dependencies" and change the external line to something like this;

fti=/usr/bin/java -cp /path/to/couchdb-lucene/target/classes:\
/path/to/couchdb-lucene/target/dependency com.github.rnewson.couchdb.lucene.Main

You will need to restart CouchDB if you change couchdb-lucene source code but this is very fast.


couchdb-lucene respects several system properties;

the url to contact CouchDB with (default is "http://localhost:5984")
specify the path to the lucene indexes (the default is to make a directory called 'lucene' relative to couchdb's current working directory.
specify the directory of the log file (which is called couchdb-lucene.log), defaults to the platform-specific temp directory.

You can override these properties like this;

fti=/usr/bin/java -Dcouchdb.lucene.dir=/tmp \
-cp /home/rnewson/Source/couchdb-lucene/target/classes:\

Basic Authentication

If you put couchdb behind an authenticating proxy you can still configure couchdb-lucene to pull from it by specifying additional system properties. Currently only Basic authentication is supported.

the user to authenticate as.
the password to authenticate with.


The default for couchdb.url is problematic on an IPv6 system. Specify -Dcouchdb.url=http://[::1]:5984 to resolve it.