Indexes work with dot notation

pull/2/head
Louis Chatriot 12 years ago
parent 16cf75b70d
commit 1d246b6a94
  1. 5
      lib/indexes.js
  2. 46
      test/indexes.test.js

@ -74,5 +74,10 @@ Index.prototype.update = function (oldDoc, newDoc) {
}; };
/**
* Search
*/
// Interface // Interface
module.exports = Index; module.exports = Index;

@ -69,6 +69,28 @@ describe('Indexing', function () {
doc2.a.should.equal(12); doc2.a.should.equal(12);
}); });
it('Works with dot notation', function () {
var idx = new Index({ fieldName: 'tf.nested' })
, doc1 = { a: 5, tf: { nested: 'hello' } }
, doc2 = { a: 8, tf: { nested: 'world', additional: true } }
, doc3 = { a: 2, tf: { nested: 'bloup', age: 42 } }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
// The underlying BST now has 3 nodes which contain the docs where it's expected
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('hello'), [doc1]);
assert.deepEqual(idx.tree.search('world'), [doc2]);
assert.deepEqual(idx.tree.search('bloup'), [doc3]);
// The nodes contain pointers to the actual documents
idx.tree.search('bloup')[0].a = 42;
doc3.a.should.equal(42);
});
}); // ==== End of 'Insertion' ==== // }); // ==== End of 'Insertion' ==== //
@ -114,6 +136,30 @@ describe('Indexing', function () {
assert.deepEqual(idx.nonindexedDocs, [doc2]); assert.deepEqual(idx.nonindexedDocs, [doc2]);
}); });
it('Works with dot notation', function () {
var idx = new Index({ fieldName: 'tf.nested' })
, doc1 = { a: 5, tf: { nested: 'hello' } }
, doc2 = { a: 8, tf: { nested: 'world', additional: true } }
, doc3 = { a: 2, tf: { nested: 'bloup', age: 42 } }
, doc4 = { a: 2, tf: { nested: 'world', fruits: ['apple', 'carrot'] } }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
idx.tree.getNumberOfKeys().should.equal(3);
idx.remove(doc1);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('hello').length.should.equal(0);
idx.remove(doc2);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('world').length.should.equal(1);
idx.tree.search('world')[0].should.equal(doc4);
});
}); // ==== End of 'Removal' ==== // }); // ==== End of 'Removal' ==== //

Loading…
Cancel
Save