mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-09-01 01:51:57 +02:00
re-implement append
This commit is contained in:
105
test/basic.js
105
test/basic.js
@@ -10,9 +10,6 @@ const build_compact = env && env.includes("compact");
|
||||
const build_esm = !env || env.startsWith("module");
|
||||
const Charset = _Charset || (await import("../src/charset.js")).default;
|
||||
|
||||
// global.FlexSearch = { Index, Document, Worker, Charset, Encoder, Resolver };
|
||||
// global.build = { build_light, build_compact, build_esm };
|
||||
|
||||
describe("Initialize", function(){
|
||||
|
||||
const index = new Index();
|
||||
@@ -157,6 +154,108 @@ describe("Add", function(){
|
||||
});
|
||||
});
|
||||
|
||||
describe("Append", function(){
|
||||
|
||||
it("Should have been properly appended to the index", function(){
|
||||
|
||||
const index = new Index();
|
||||
|
||||
index.append(0, "foo");
|
||||
index.append(2, "bar");
|
||||
index.append(1, "FooBar");
|
||||
index.append(3, "Some 'short' content.");
|
||||
|
||||
expect(Array.from(index.reg.keys())).to.have.members([0, 1, 2, 3]);
|
||||
expect(Array.from(index.map.keys())).to.have.members(["fo", "bar", "fobar", "some", "short", "content"]);
|
||||
expect(index.ctx.size).to.equal(0);
|
||||
expect(index.reg.size).to.equal(4);
|
||||
});
|
||||
|
||||
it("Should have been properly appended by score", function(){
|
||||
|
||||
let index = new Index();
|
||||
index.add(0, "foo A B C D E F bar");
|
||||
index.add(1, "foo A B C bar D E F");
|
||||
index.add(2, "foo bar A B C D E F");
|
||||
|
||||
expect(index.search("foo")).to.eql([0, 1, 2]);
|
||||
expect(index.search("bar")).to.eql([2, 1, 0]);
|
||||
expect(index.reg.size).to.equal(3);
|
||||
expect(index.map.size).to.equal(8);
|
||||
expect(index.ctx.size).to.equal(0);
|
||||
|
||||
index.append(0, "bar");
|
||||
index.append(0, "foo");
|
||||
index.append(0, "bar");
|
||||
|
||||
expect(index.search("foo")).to.eql([0, 1, 2]);
|
||||
expect(index.search("bar")).to.eql([0, 2, 1]);
|
||||
expect(index.reg.size).to.equal(3);
|
||||
expect(index.map.size).to.equal(8);
|
||||
expect(index.ctx.size).to.equal(0);
|
||||
|
||||
index.remove(2).remove(1).remove(0);
|
||||
expect(index.reg.size).to.equal(0);
|
||||
expect(index.map.size).to.equal(0);
|
||||
expect(index.ctx.size).to.equal(0);
|
||||
|
||||
index = new Index({ context: true });
|
||||
index.add(0, "foo A B C D E F bar");
|
||||
index.add(1, "foo A B C bar D E F");
|
||||
index.add(2, "foo bar A B C D E F");
|
||||
|
||||
expect(index.search("foo")).to.eql([0, 1, 2]);
|
||||
expect(index.search("bar")).to.eql([2, 1, 0]);
|
||||
expect(index.reg.size).to.equal(3);
|
||||
expect(index.map.size).to.equal(8);
|
||||
expect(index.ctx.size).to.equal(7);
|
||||
|
||||
index.append(0, "bar");
|
||||
index.append(0, "foo");
|
||||
index.append(0, "bar");
|
||||
|
||||
expect(index.search("foo")).to.eql([0, 1, 2]);
|
||||
expect(index.search("bar")).to.eql([0, 2, 1]);
|
||||
expect(index.reg.size).to.equal(3);
|
||||
expect(index.map.size).to.equal(8);
|
||||
expect(index.ctx.size).to.equal(7);
|
||||
|
||||
index.remove(2).remove(1).remove(0);
|
||||
expect(index.reg.size).to.equal(0);
|
||||
expect(index.map.size).to.equal(0);
|
||||
expect(index.ctx.size).to.equal(0);
|
||||
});
|
||||
|
||||
it("Should not have been appended to the index (Parameter)", function(){
|
||||
|
||||
const index = new Index();
|
||||
|
||||
index.append("foo");
|
||||
index.append(3);
|
||||
index.append(null, "foobar");
|
||||
index.append(void 0, "foobar");
|
||||
index.append(3, null);
|
||||
index.append(3, false);
|
||||
|
||||
expect(index.reg.size).to.equal(0);
|
||||
});
|
||||
|
||||
it("Should not have been appended to the index (Empty)", function(){
|
||||
|
||||
const index = new Index();
|
||||
|
||||
index.append(1, "");
|
||||
index.append(2, " ");
|
||||
index.append(3, " ");
|
||||
index.append(4, " - ");
|
||||
index.append(5, ` ...
|
||||
- : ,
|
||||
<-- `);
|
||||
|
||||
expect(index.reg.size).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Search (Sync)", function(){
|
||||
|
||||
it("Should have been matched properly", function(){
|
||||
|
@@ -14,13 +14,13 @@ if(!build_light) describe("Document (Multi-Field Search)", function(){
|
||||
|
||||
const data = [{
|
||||
id: 2,
|
||||
data: { title: "Title 3", body: "Body 3" }
|
||||
data: { title: "Title 3", body: "Body 3", cat: "A" }
|
||||
},{
|
||||
id: 1,
|
||||
data: { title: "Title 2", body: "Body 2" }
|
||||
data: { title: "Title 2", body: "Body 2", cat: "B" }
|
||||
},{
|
||||
id: 0,
|
||||
data: { title: "Title 1", body: "Body 1" }
|
||||
data: { title: "Title 1", body: "Body 1", cat: "A" }
|
||||
}];
|
||||
|
||||
const update = [{
|
||||
@@ -404,6 +404,89 @@ if(!build_light) describe("Document (Multi-Field Search)", function(){
|
||||
})).to.eql([]);
|
||||
});
|
||||
|
||||
it("Merge Results", function(){
|
||||
|
||||
const document = new Document({
|
||||
document: {
|
||||
store: [
|
||||
"data:title"
|
||||
],
|
||||
id: "id",
|
||||
field: [
|
||||
"data:title",
|
||||
"data:body"
|
||||
],
|
||||
tag: "data:cat"
|
||||
}
|
||||
});
|
||||
|
||||
for(let i = 0; i < data.length; i++){
|
||||
document.add(data[i]);
|
||||
}
|
||||
|
||||
expect(document.search({
|
||||
query: "title",
|
||||
enrich: false,
|
||||
suggest: true,
|
||||
merge: true
|
||||
})).to.eql([
|
||||
{ id: 2, field: [ 'data:title' ] },
|
||||
{ id: 1, field: [ 'data:title' ] },
|
||||
{ id: 0, field: [ 'data:title' ] }
|
||||
]);
|
||||
|
||||
expect(document.search({
|
||||
query: "title",
|
||||
enrich: true,
|
||||
suggest: true,
|
||||
merge: true
|
||||
}).map(res => res.doc)).to.eql([
|
||||
{ data: { title: data[0].data.title }},
|
||||
{ data: { title: data[1].data.title }},
|
||||
{ data: { title: data[2].data.title }}
|
||||
]);
|
||||
|
||||
expect(document.search({
|
||||
query: "title",
|
||||
tag: { "data:cat": "A" },
|
||||
enrich: true,
|
||||
suggest: true,
|
||||
merge: true
|
||||
}).map(res => res.doc)).to.eql([
|
||||
{ data: { title: data[0].data.title }},
|
||||
{ data: { title: data[2].data.title }}
|
||||
]);
|
||||
});
|
||||
|
||||
it("Using BigInt", function(){
|
||||
|
||||
const document = new Document({
|
||||
document: {
|
||||
store: "data:title",
|
||||
id: "id",
|
||||
field: [
|
||||
"data:title",
|
||||
"data:body"
|
||||
],
|
||||
tag: "data:cat"
|
||||
}
|
||||
});
|
||||
|
||||
for(let i = 0, tmp; i < data.length; i++){
|
||||
tmp = Object.assign({}, data[i], { id: BigInt(i + 1) });
|
||||
document.add(tmp);
|
||||
}
|
||||
|
||||
expect(document.search({
|
||||
query: "title",
|
||||
tag: { "data:cat": "A" },
|
||||
merge: true
|
||||
})).to.eql([
|
||||
{ id: BigInt(1), field: [ "data:title" ] },
|
||||
{ id: BigInt(3), field: [ "data:title" ] }
|
||||
]);
|
||||
});
|
||||
|
||||
it("Custom Document Store", function(){
|
||||
|
||||
const document = new Document({
|
||||
|
Reference in New Issue
Block a user