Skip to content

Commit

Permalink
Updated with some better testing, and some sanity around field defaul…
Browse files Browse the repository at this point in the history
…ts for queryParser
  • Loading branch information
Eric Jennings committed Aug 17, 2011
1 parent 46b9833 commit 5ee6cd8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 23 deletions.
34 changes: 13 additions & 21 deletions clucene.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
var clucene = require("./build/default/clucene");

exports.CLucene = clucene;
clucene.STORE_YES = 1;
clucene.STORE_NO = 2;
clucene.STORE_COMPRESS = 4;
clucene.INDEX_NO = 16;
clucene.INDEX_TOKENIZED = 32;
clucene.INDEX_UNTOKENIZED = 64;
clucene.INDEX_NONORMS = 128;
clucene.TERMVECTOR_NO = 256;
clucene.TERMVECTOR_YES = 512;
clucene.TERMVECTOR_WITH_POSITIONS = 512 | 1024;
clucene.TERMVECTOR_WITH_OFFSETS = 512 | 2048;
clucene.TERMVECTOR_WITH_POSITIONS_OFFSETS = (512 | 1024) | (512 | 2048);

exports.Store = {
STORE_YES: 1,
STORE_NO: 2,
STORE_COMPRESS: 4
};

exports.Index = {
INDEX_NO: 16,
INDEX_TOKENIZED: 32,
INDEX_UNTOKENIZED: 64,
INDEX_NONORMS: 128,
};

exports.TermVector = {
TERMVECTOR_NO: 256,
TERMVECTOR_YES: 512,
TERMVECTOR_WITH_POSITIONS: 512 | 1024,
TERMVECTOR_WITH_OFFSETS: 512 | 2048,
TERMVECTOR_WITH_POSITIONS_OFFSETS: (512 | 1024) | (512 | 2048)
};
exports.CLucene = clucene;
4 changes: 2 additions & 2 deletions src/clucene_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class Lucene : public ObjectWrap {

TryCatch tryCatch;

baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
baton->callback->Call(Context::GetCurrent()->Global(), 3, argv);

if (tryCatch.HasCaught()) {
FatalException(tryCatch);
Expand Down Expand Up @@ -466,7 +466,7 @@ class Lucene : public ObjectWrap {

try {
TCHAR* searchString = STRDUP_AtoT(*(*baton->search));
Query* q = QueryParser::parse(searchString, _T(""), &analyzer);
Query* q = QueryParser::parse(searchString, _T("_id"), &analyzer);
Hits* hits = s.search(q);

HandleScope scope;
Expand Down
98 changes: 98 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
var path = require('path');
var fs = require('fs');
var wrench = require('wrench');

var cl = require('../clucene').CLucene;
var clucene = new cl.Lucene();

var indexPath = './test.index';

exports['add new document'] = function (test) {
if (path.existsSync(indexPath)) {
wrench.rmdirSyncRecursive(indexPath);
}

var doc = new cl.Document();
var docId = '1';

doc.addField('name', 'Eric Jennings', cl.STORE_YES|cl.INDEX_TOKENIZED);
doc.addField('timestamp', '1293765885000', cl.STORE_YES|cl.INDEX_UNTOKENIZED);

clucene.addDocument(docId, doc, indexPath, function(err, indexTime, docsReplaced) {
test.equal(err, null);
test.ok(is('Number', indexTime));
test.equal(docsReplaced, 0);
test.done();
});
};

exports['query newly-added document'] = function (test) {
clucene.search(indexPath, '1', function(err, results) {
test.equal(err, null);
test.ok(is('Array', results));
test.equal(results[0]._id, 1);
test.equal(results[0].name, 'Eric Jennings');
test.equal(results[0].timestamp, '1293765885000');
test.done();
});
};

exports['update existing document'] = function (test) {
var doc = new cl.Document();
var docId = '1';

doc.addField('name', 'Thomas Anderson', cl.STORE_YES|cl.INDEX_TOKENIZED);
doc.addField('timestamp', '129555555555555', cl.STORE_YES|cl.INDEX_UNTOKENIZED);

clucene.addDocument(docId, doc, indexPath, function(err, indexTime, docsReplaced) {
test.equal(err, null);
test.ok(is('Number', indexTime));
test.equal(docsReplaced, 1);
test.done();
});
};

exports['query updated document'] = function (test) {
clucene.search(indexPath, '1', function(err, results) {
test.equal(err, null);
test.ok(is('Array', results));
test.equal(results[0]._id, 1);
test.equal(results[0].name, 'Thomas Anderson');
test.equal(results[0].timestamp, '129555555555555');
test.done();
});
};

exports['query by full field name'] = function (test) {
clucene.search(indexPath, 'name:"Thomas"', function(err, results) {
test.equal(err, null);
test.ok(is('Array', results));
test.equal(results[0]._id, 1);
test.equal(results[0].name, 'Thomas Anderson');
test.equal(results[0].timestamp, '129555555555555');
test.done();
});
};

exports['query by wildcard'] = function (test) {
clucene.search(indexPath, 'name:Thom*', function(err, results) {
test.equal(err, null);
test.ok(is('Array', results));
test.equal(results[0]._id, 1);
test.equal(results[0].name, 'Thomas Anderson');
test.equal(results[0].timestamp, '129555555555555');
test.done();
});
};

exports['cleanup'] = function (test) {
if (path.existsSync(indexPath)) {
wrench.rmdirSyncRecursive(indexPath);
}
test.done();
}

function is(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
return obj !== undefined && obj !== null && clas === type;
}

0 comments on commit 5ee6cd8

Please sign in to comment.