faiss-node
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -32,2 +32,8 @@ const { IndexFlatL2 } = require('../'); | ||
newIndex.mergeFrom(index); | ||
console.log(newIndex.ntotal()); | ||
console.log(newIndex.ntotal()); | ||
console.log(newIndex.search([1, 2], 1)); | ||
const removedCount = newIndex.removeIds([0]); | ||
console.log(removedCount); | ||
console.log(newIndex.ntotal()); | ||
console.log(newIndex.search([1, 2], 1)); |
@@ -63,2 +63,8 @@ /** Searh result object. */ | ||
mergeFrom(otherIndex: IndexFlatL2): void; | ||
/** | ||
* Remove IDs from the index. | ||
* @param {string} ids IDs to read. | ||
* @return {IndexFlatL2} number of IDs removed. | ||
*/ | ||
removeIds(ids: number[]): number | ||
} |
{ | ||
"name": "faiss-node", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Node.js bindings for faiss", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -65,2 +65,9 @@ # faiss-node | ||
console.log(newIndex.ntotal()); // 4 | ||
// Remove items | ||
console.log(newIndex.search([1, 2], 1)); // { distances: [ 0 ], labels: [ 1 ] } | ||
const removedCount = newIndex.removeIds([0]); | ||
console.log(removedCount); // 1 | ||
console.log(newIndex.ntotal()); // 3 | ||
console.log(newIndex.search([1, 2], 1)); // { distances: [ 0 ], labels: [ 0 ] } | ||
``` | ||
@@ -67,0 +74,0 @@ |
@@ -138,50 +138,100 @@ const { IndexFlatL2 } = require('../lib'); | ||
describe("#merge", () => { | ||
const index1 = new IndexFlatL2(2); | ||
beforeAll(() => { | ||
index1.add([1, 0]); | ||
index1.add([1, 2]); | ||
index1.add([1, 3]); | ||
index1.add([1, 1]); | ||
}); | ||
const index2 = new IndexFlatL2(2); | ||
beforeAll(() => { | ||
index2.mergeFrom(index1); | ||
}); | ||
const index1 = new IndexFlatL2(2); | ||
beforeAll(() => { | ||
index1.add([1, 0]); | ||
index1.add([1, 2]); | ||
index1.add([1, 3]); | ||
index1.add([1, 1]); | ||
}); | ||
it("throws an error if the number of arguments is not 1", () => { | ||
expect(() => { index2.mergeFrom() }).toThrow('Expected 1 argument, but got 0.'); | ||
expect(() => { index2.mergeFrom(index1, 2) }).toThrow('Expected 1 argument, but got 2.'); | ||
}); | ||
const index2 = new IndexFlatL2(2); | ||
beforeAll(() => { | ||
index2.mergeFrom(index1); | ||
}); | ||
it("throws an error if argument is not an object", () => { | ||
expect(() => { index2.mergeFrom(1) }).toThrow('Invalid argument type, must be an object.'); | ||
expect(() => { index2.mergeFrom("string") }).toThrow('Invalid argument type, must be an object.'); | ||
}); | ||
it("throws an error if the number of arguments is not 1", () => { | ||
expect(() => { index2.mergeFrom() }).toThrow('Expected 1 argument, but got 0.'); | ||
expect(() => { index2.mergeFrom(index1, 2) }).toThrow('Expected 1 argument, but got 2.'); | ||
}); | ||
it("throws an error if argument is not an instance of IndexFlatL2", () => { | ||
expect(() => { index2.mergeFrom({}) }).toThrow('Invalid argument'); | ||
expect(() => { index2.mergeFrom({"foo": "bar"}) }).toThrow('Invalid argument'); | ||
}); | ||
it("throws an error if argument is not an object", () => { | ||
expect(() => { index2.mergeFrom(1) }).toThrow('Invalid argument type, must be an object.'); | ||
expect(() => { index2.mergeFrom("string") }).toThrow('Invalid argument type, must be an object.'); | ||
}); | ||
it("throws an error if merging index has different dimensions", () => { | ||
const index3 = new IndexFlatL2(3); | ||
expect(() => { index2.mergeFrom(index3) }).toThrow('The merging index must have the same dimension.'); | ||
}); | ||
it("throws an error if argument is not an instance of IndexFlatL2", () => { | ||
expect(() => { index2.mergeFrom({}) }).toThrow('Invalid argument'); | ||
expect(() => { index2.mergeFrom({ "foo": "bar" }) }).toThrow('Invalid argument'); | ||
}); | ||
it("returns search results on merged index", () => { | ||
expect(index2.search([1, 0], 1)).toMatchObject({ | ||
distances: [0], | ||
labels: [0], | ||
it("throws an error if merging index has different dimensions", () => { | ||
const index3 = new IndexFlatL2(3); | ||
expect(() => { index2.mergeFrom(index3) }).toThrow('The merging index must have the same dimension.'); | ||
}); | ||
expect(index2.search([1, 0], 4)).toMatchObject({ | ||
distances: [0, 1, 4, 9], | ||
labels: [0, 3, 1, 2], | ||
it("returns search results on merged index", () => { | ||
expect(index2.search([1, 0], 1)).toMatchObject({ | ||
distances: [0], | ||
labels: [0], | ||
}); | ||
expect(index2.search([1, 0], 4)).toMatchObject({ | ||
distances: [0, 1, 4, 9], | ||
labels: [0, 3, 1, 2], | ||
}); | ||
expect(index2.search([1, 1], 4)).toMatchObject({ | ||
distances: [0, 1, 1, 4], | ||
labels: [3, 0, 1, 2], | ||
}); | ||
}); | ||
expect(index2.search([1, 1], 4)).toMatchObject({ | ||
distances: [0, 1, 1, 4], | ||
labels: [3, 0, 1, 2], | ||
}); | ||
describe("#removeIds", () => { | ||
let index; | ||
beforeEach(() => { | ||
index = new IndexFlatL2(2); | ||
index.add([1, 0]); | ||
index.add([1, 1]); | ||
index.add([1, 2]); | ||
index.add([1, 3]); | ||
}); | ||
}); | ||
it('throws an error if the count of given param is not 1', () => { | ||
expect(() => { index.removeIds() }).toThrow('Expected 1 argument, but got 0.'); | ||
expect(() => { index.removeIds([], 1) }).toThrow('Expected 1 argument, but got 2.'); | ||
}); | ||
it('throws an error if given a non-Array object', () => { | ||
expect(() => { index.removeIds('[1, 2, 3]') }).toThrow('Invalid the first argument type, must be an Array.'); | ||
}); | ||
it('throws an error if the element of given array is not a number', () => { | ||
expect(() => { index.removeIds([1, '2']) }).toThrow('Expected a Number as array item. (at: 1)'); | ||
}); | ||
it("returns number of IDs removed", () => { | ||
expect(index.removeIds([])).toBe(0); | ||
expect(index.removeIds([0])).toBe(1); | ||
expect(index.removeIds([0, 1])).toBe(2); | ||
expect(index.removeIds([2])).toBe(0); | ||
expect(index.removeIds([0, 1, 2])).toBe(1); | ||
}); | ||
it("correctly removed", () => { | ||
expect(index.search([1, 1], 1)).toMatchObject({ distances: [0], labels: [1] }); | ||
expect(index.removeIds([0])).toBe(1); | ||
expect(index.search([1, 1], 1)).toMatchObject({ distances: [0], labels: [0] }); | ||
}); | ||
it("correctly removed multiple elements", () => { | ||
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [3] }); | ||
expect(index.removeIds([0, 1])).toBe(2); | ||
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [1] }); | ||
}); | ||
it("correctly removed partal elements", () => { | ||
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [3] }); | ||
expect(index.removeIds([0, 1, 2, 4, 5])).toBe(3); | ||
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [0] }); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38045
308
76