-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated with some better testing, and some sanity around field defaul…
…ts for queryParser
- Loading branch information
Eric Jennings
committed
Aug 17, 2011
1 parent
46b9833
commit 5ee6cd8
Showing
3 changed files
with
113 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |