CARVIEW |
rnewson / couchdb-lucene
- Source
- Commits
- Network (15)
- Issues (8)
- Downloads (7)
- Graphs
-
Branch:
master
click here to add a description
click here to add a homepage
Pledgie Donations
Once activated, we'll place the following badge in your repository's detail box:
Enables full-text searching of CouchDB documents using Lucene — Read more
name | age | message | |
---|---|---|---|
![]() |
.gitignore | Thu Jul 23 12:12:43 -0700 2009 | ignore artifacts [rnewson] |
![]() |
LICENSE | Fri Mar 13 17:19:09 -0700 2009 | fix license file [rnewson] |
![]() |
README.md | Fri Feb 19 14:39:54 -0800 2010 | fix documentation s/integer/int [rnewson] |
![]() |
THANKS.md | Fri Dec 11 07:22:46 -0800 2009 | add thanks to Adam Lofts. [rnewson] |
![]() |
TODO | Fri Feb 05 05:39:08 -0800 2010 | add note to configure plugin for OSX bundle. [rnewson] |
![]() |
couchdb-external-hook.py | Sun Jan 31 12:50:21 -0800 2010 | more fixes for new _cleanup method. [rnewson] |
![]() |
pom.xml | Sun Feb 28 09:06:58 -0800 2010 | upgrade to Lucene 3.0.1 [rnewson] |
![]() |
src/ | Mon Mar 01 10:31:08 -0800 2010 | remove MINUTES for 1.5 compat. [rnewson] |
WARNING
0.5 is not yet released and may contain bugs and regressions. However, it is shaping up nicely so I recommend you try it first. 0.4 remains available if you hit insurmountable problems; but please take time to file a ticket at github if you do.
Version Compatibility
CouchDB | couchdb-lucene |
---|---|
0.9.1, 0.10 | 0.4 |
0.11 (not yet released) | 0.4-maint (0.4 with patch for trunk compatibility) |
0.10+ | 0.5 (not yet released) |
Issue Tracking
Issue tracking at github.
Minimum System Requirements
Java 1.5 (or above) is required; the Sun version is recommended as it's regularly tested against.
Build couchdb-lucene
- Install Maven 2.
- checkout repository
- type 'mvn'
- configure couchdb (see below)
You will now have a zip file in the target/ directory. This contains all the couchdb-lucene code, dependencies, startup scripts and configuration files to run couchdb-lucene.
Configure CouchDB
[couchdb] os_process_timeout=60000 ; increase the timeout from 5 seconds. [external] fti=/path/to/python /path/to/couchdb-lucene/tools/couchdb-external-hook.py [httpd_db_handlers] _fti = {couch_httpd_external, handle_external_req, <<"fti">>}
Hook options
Option | Meaning | Default Value |
---|---|---|
--remote-host | The hostname of the couchdb-lucene server | localhost |
--remote-port | The port of the couchdb-lucene server | 5985 |
--local-key | The key for the local couchdb instance as known to the couchdb-lucene server | local |
Configure couchdb-lucene
couchdb-lucene runs in a single, standalone JVM. As such, you can choose to locate your couchdb-lucene server on a different machine to couchdb if you wish, or keep it on the same machine, it's your call.
Start couchdb-lucene
To start couchdb-lucene, run;
bin/run
To stop couchdb-lucene, simply kill the Java process.
Indexing Strategy
Document Indexing
You must supply a index function in order to enable couchdb-lucene as, by default, nothing will be indexed. To suppress a document from the index, return null. It's more typical to return a single Document object which contains everything you'd like to query and retrieve. You may also return an array of Document objects if you wish.
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.
Here's an complete example of a design document with couchdb-lucene features:
{ "_id":"_design/a_design_document_with_any_name_you_like", "fulltext": { "by_subject": { "index":"function(doc) { var ret=new Document(); ret.add(doc.subject); return ret }" }, "by_content": { "index":"function(doc) { var ret=new Document(); ret.add(doc.content); return ret }" } } }
Here are some example URL's for the given design document;
https://localhost:5984/database/_fti/lucene/by_subject?q=hello https://localhost:5984/database/_fti/lucene/by_content?q=hello
A fulltext object contains multiple index view declarations. An index view consists of;
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 |
type | the type of the field | date, double, float, int, long, string | string |
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 |
The Analyzer Option
Lucene has numerous ways of converting free-form text into tokens, these classes are called Analyzer's. By default, the StandardAnalyzer is used which lower-cases all text, drops common English words ("the", "and", and so on), among other things. This processing might not always suit you, so you can choose from several others by setting the "analyzer" field to one of the following values;
- brazilian
- chinese
- cjk
- czech
- dutch
- english
- french
- german
- keyword
- perfield
- porter
- russian
- simple
- standard
- thai
The "perfield" option lets you use a different analyzer for different fields and is configured as follows;
"analyzer":"perfield:{field_name:\"analyzer_name\"}"
Unless overridden, any field name not specified will be handled by the standard analyzer. To change the default, use the special default field name;
"analyzer":"perfield:{default:\"keyword\"}"
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.
// Add with all the defaults. doc.add("value"); // Add a numeric field. doc.add(35, {"type":"int"}); // Add a date field. doc.add(new Date("2009-01-01"), {"type":"date"}); // Add a date field (object must be a Date object // 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': idx(obj[key]); break; case 'function': break; default: ret.add(obj[key]); break; } } }; idx(doc); 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"}); result.add(new Date(), {"field":"indexed_at"}); return result; }
Index Attachments
function(doc) { var result = new Document(); for(var a in doc._attachments) { result.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; }; if(doc.type != "reference") return null; var ret = new Array(); for(var g in doc.groups) { ret.push(mk("library", doc.groups[g].library, g)); ret.push(mk("method", doc.groups[g].method, g)); ret.push(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)
- HTML (text/html)
- Images (image/*)
- Java class files
- Java jar archives
- MP3 (audio/mp3)
- OpenDocument (application/vnd.oasis.opendocument.*)
- Outlook (application/vnd.ms-outlook)
- PDF (application/pdf)
- Plain text (text/plain)
- Powerpoint presentations (application/vnd.ms-powerpoint)
- RTF (application/rtf)
- Visio (application/vnd.visio)
- Word documents (application/msword)
- XML (application/xml)
Searching with couchdb-lucene
You can perform all types of queries using Lucene's default query syntax.
Numeric range queries
In addition to normal text-based range searches (using the "field:[lower TO upper]" syntax), couchdb-lucene all supports numeric range searches for the following types: int, long, float, double and date. The type is specified after the field name, as follows;
type | example |
int | field<int>:[0 TO 100] |
long | field<long>:[0 TO 100] |
float | field<float>:[0.0 TO 100.0] |
double | field<double>:[0.0 TO 100.0] |
date | field<date>:[2001-01-01 TO 2010-01-01] or field<date>:[2000-01-01T00:00:00-0500 TO 2010-01-01T00:00:00-0500] |
An example numeric range query for spatial searching.
?q=pizza AND lat<double>:[51.4707 TO 51.5224] AND long<double>:[-0.6622 TO -0.5775]
The following parameters can be passed for more sophisticated searches;
All parameters except 'q' are optional.
Special Fields
Dublin Core
All Dublin Core attributes are indexed and stored if detected in the attachment. Descriptions of the fields come from the Tika javadocs.
Examples
https://localhost:5984/dbname/_fti/design_doc/view_name?q=field_name:value https://localhost:5984/dbname/_fti/design_doc/view_name?q=field_name:value&sort;=other_field https://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 search results array
The search results arrays consists of zero, one or more objects with the following fields;
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": [ "enron", 6 ] }, { "id": "shankman-j-inbox-8.", "score": 0.7492915391921997, "sort_order": [ "enron", 7 ] }, { "id": "shankman-j-inbox-30.", "score": 0.507369875907898, "sort_order": [ "enron", 8 ] } ] }
Content-Type of response
The Content-Type of the response is negotiated via the Accept request header like CouchDB itself. If the Accept header includes "application/json" then that is also the Content-Type of the response. If not, "text/plain;charset=utf-8" is used.
Fetching information about the index
Calling couchdb-lucene without arguments returns a JSON object with information about the index.
https://127.0.0.1:5984/<db>/_fti/<ddoc>/<index
returns;
{"current":true,"disk_size":110674,"doc_count":397,"doc_del_count":0, "fields":["default","number"],"last_modified":"1263066382000", "optimized":true,"ref_count":2}
Index Maintenance
For optimal query speed you can optimize your indexes. This causes the index to be rewritten into a single segment.
curl -X POST https://localhost:5984/<db>/_fti/<ddoc>/<index>/_optimize
If you just want to expunge pending deletes, then call;
curl -X POST https://localhost:5984/<db>/_fti/<ddoc>/<index>/_expunge
If you recreate databases or frequently change your fulltext functions, you will probably have old indexes lying around on disk. To remove all of them, call;
curl -X POST https://localhost:5984/<db>/_fti/_cleanup