1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-08-29 08:40:45 +02:00
This commit is contained in:
Thomas Wilkerling
2025-05-21 10:57:43 +02:00
parent e9c26f06b4
commit 103f617ad5
174 changed files with 16528 additions and 14980 deletions

View File

@@ -329,13 +329,13 @@ export default function(DB, DBClass){
field: "primaryTitle",
result: [{
id: data[0]["tconst"],
doc: data[0],
doc: data[0]
}]
},{
field: "originalTitle",
result: [{
id: data[0]["tconst"],
doc: data[0],
doc: data[0]
}]
}]);
@@ -355,11 +355,11 @@ export default function(DB, DBClass){
expect(result).to.eql([
{ field: 'primaryTitle', result: [{
id: data[0]["tconst"],
doc: data[0],
doc: data[0]
}] },
{ field: 'originalTitle', result: [{
id: data[0]["tconst"],
doc: data[0],
doc: data[0]
}] }
]);
@@ -415,6 +415,7 @@ export default function(DB, DBClass){
}
});
//await index.mount(db);
await index.db;
// add test data
@@ -428,8 +429,6 @@ export default function(DB, DBClass){
let result = await index.searchCache({
query: "karmen or clown or not found",
suggest: true,
// highlight template
// $1 is a placeholder for the matched partial
highlight: "<b>$1</b>"
});
@@ -447,8 +446,6 @@ export default function(DB, DBClass){
result = await index.searchCache({
query: "karmen or clown or not found",
suggest: true,
// highlight template
// $1 is a placeholder for the matched partial
highlight: "<b>$1</b>"
});
@@ -467,8 +464,6 @@ export default function(DB, DBClass){
query: "karmen or clown or not found",
suggest: true,
field: "title",
// highlight template
// $1 is a placeholder for the matched partial
highlight: "<b>$1</b>"
});
@@ -482,4 +477,792 @@ export default function(DB, DBClass){
highlight: 'Le <b>clown</b> et ses chiens'
}]);
});
it("Resolver (Persistent)", async function(){
// some test data
const data = [{
"id": 1,
"title": "Carmencita",
"description": "Description: Carmencita"
},{
"id": 2,
"title": "Le clown et ses chiens",
"description": "Description: Le clown et ses chiens"
}];
// create the document index
const index = new Document({
db: new DB("test-store", {
type: "integer"
}),
encoder: Charset.LatinBalance,
document: {
store: true,
index: [{
field: "title",
tokenize: "forward"
},{
field: "description",
tokenize: "forward"
}]
}
});
await index.db;
// add test data
for(let i = 0; i < data.length; i++){
index.add(data[i]);
}
await index.commit();
let result = new Resolver({
index: index,
query: "karmen",
field: "title"
});
expect(result).to.be.instanceof(Resolver);
result = result.or({
query: "clown",
pluck: "description",
});
result = result.and({
query: "not found",
field: "title",
suggest: true,
enrich: true,
resolve: true,
// TODO
//highlight: "<b>$1</b>"
});
expect(result).to.be.instanceof(Promise);
expect(await result).to.eql([{
id: 1,
doc: data[0],
//highlight: "<b>Carmen</b>cita"
},{
id: 2,
doc: data[1],
//highlight: "Le <b>clown</b> et ses chiens"
}]);
// -----------------------------------
result = new Resolver({
index: index,
query: "karmen",
field: "title"
});
expect(result).to.be.instanceof(Resolver);
result = result.or({
query: "clown",
pluck: "description",
}).and({
query: "not found",
field: "title",
suggest: true
}).resolve({
enrich: true
});
expect(result).to.be.instanceof(Promise);
expect(await result).to.eql([{
id: 1,
doc: data[0]
},{
id: 2,
doc: data[1]
}]);
});
it("Should have been resolved a Resolver properly (Async)", async function(){
const db = new DB("test-store", {
type: "integer"
});
const index = new Index({
tokenize: "reverse"
});
await index.mount(db);
await index.clear();
index.add(1, "foo");
await index.addAsync(2, "bar");
index.add(3, "FooBar");
await index.commit();
let resolver = new Resolver({
index: index,
query: "foo bar",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
let tmp = resolver.await;
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
expect((await tmp)[0]).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: true,
query: "foo bar",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
tmp = resolver.await;
resolver = resolver.resolve({
limit: 1,
offset: 1
});
expect(resolver).to.be.instanceof(Promise);
expect((await resolver)[0]).oneOf([3, 1, 2]);
expect((await resolver)[1]).to.be.undefined;
expect((await tmp)[0]).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: true,
query: "bar"
}).and({
async: true,
query: "foo",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: true,
query: "bar"
}).and({
async: true,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: true,
cache: true,
query: "bar"
}).and({
async: true,
cache: true,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: true,
cache: true,
query: "bar"
}).and({
async: true,
cache: true,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: false,
cache: true,
query: "bar"
}).and({
async: true,
cache: false,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: true,
cache: true,
query: "bar"
}).and({
async: false,
cache: false,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
});
it("Should have been resolved a Resolver properly (Document Persistent)", async function(){
// create DB instance with namespace
const db = new DB("my-store");
// create the document index
const document = new Document({
encoder: Charset.LatinBalance,
document: {
id: "tconst",
store: true,
index: [{
field: "primaryTitle",
tokenize: "forward"
},{
field: "originalTitle",
tokenize: "forward"
}],
tag: [{
field: "startYear"
},{
field: "genres"
}]
}
});
// mount database to the index
await document.mount(db);
// add test data
for(let i = 0; i < data.length; i++){
document.add(data[i]);
}
// transfer changes in bulk
await document.commit();
let resolver = new Resolver({
index: document,
query: "karmen or clown or nothing",
field: "primaryTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
let tmp = resolver.await;
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members(["tt0000001", "tt0000002"]);
expect((await tmp)[0]).to.have.members(["tt0000001"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen or clown or nothing",
field: "primaryTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
tmp = resolver.await;
resolver = resolver.resolve({ enrich: true });
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql([{
id: data[0].tconst,
doc: data[0]
}, {
id: data[1].tconst,
doc: data[1]
}]);
expect((await tmp)[0]).to.have.members(["tt0000001"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen or clown or nothing",
field: "primaryTitle",
suggest: true
}).or({
index: document,
queue: true,
query: "karmen or clown or nothing",
pluck: "primaryTitle",
suggest: true,
enrich: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql([{
id: data[0].tconst,
doc: data[0]
}, {
id: data[1].tconst,
doc: data[1]
}]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen or clown or nothing",
pluck: "primaryTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
resolver = resolver.resolve({
limit: 1,
offset: 1
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql(["tt0000002"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen",
pluck: "primaryTitle"
}).or({
queue: true,
cache: true,
query: "clown",
field: "originalTitle"
}).and({
async: true,
query: "not found",
pluck: "originalTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql(["tt0000001", "tt0000002"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
// TODO
//cache: true,
query: "karmen",
pluck: "primaryTitle"
}).or({
and: [{
async: true,
//cache: true,
query: "not found",
pluck: "originalTitle",
suggest: true
},{
queue: true,
//cache: true,
query: "clown",
field: "originalTitle",
suggest: true
}]
}).resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql(["tt0000001", "tt0000002"]);
});
it("Should have been resolved a Resolver properly (Document Worker)", async function(){
// create the document index
const document = await new Document({
worker: true,
encoder: Charset.LatinBalance,
document: {
id: "tconst",
store: true,
index: [{
field: "primaryTitle",
tokenize: "forward"
},{
field: "originalTitle",
tokenize: "forward"
}],
tag: [{
field: "startYear"
},{
field: "genres"
}]
}
});
// add test data
for(let i = 0; i < data.length; i++){
await document.addAsync(data[i]);
}
let resolver = new Resolver({
index: document,
query: "karmen or clown or nothing",
field: "primaryTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
let tmp = resolver.await;
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members(["tt0000001", "tt0000002"]);
expect((await tmp)[0]).to.have.members(["tt0000001"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen or clown or nothing",
field: "primaryTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
tmp = resolver.await;
resolver = resolver.resolve({ enrich: true });
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql([{
id: data[0].tconst,
doc: data[0]
}, {
id: data[1].tconst,
doc: data[1]
}]);
expect((await tmp)[0]).to.have.members(["tt0000001"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen or clown or nothing",
field: "primaryTitle",
suggest: true
}).or({
index: document,
queue: true,
query: "karmen or clown or nothing",
pluck: "primaryTitle",
suggest: true,
enrich: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql([{
id: data[0].tconst,
doc: data[0]
}, {
id: data[1].tconst,
doc: data[1]
}]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen or clown or nothing",
pluck: "primaryTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
resolver = resolver.resolve({
limit: 1,
offset: 1
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql(["tt0000002"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
query: "karmen",
pluck: "primaryTitle"
}).or({
queue: true,
cache: true,
query: "clown",
field: "originalTitle"
}).and({
async: true,
query: "not found",
pluck: "originalTitle",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql(["tt0000001", "tt0000002"]);
// -----------------------------------
resolver = new Resolver({
index: document,
async: true,
// TODO
//cache: true,
query: "karmen",
pluck: "primaryTitle"
}).or({
and: [{
async: true,
//cache: true,
query: "not found",
pluck: "originalTitle",
suggest: true
},{
queue: true,
//cache: true,
query: "clown",
field: "originalTitle",
suggest: true
}]
}).resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.eql(["tt0000001", "tt0000002"]);
});
it("Should have been resolved a Resolver properly (Queue)", async function(){
const db = new DB("test-store", {
type: "integer"
});
const index = new Index({
tokenize: "reverse"
});
await index.mount(db);
await index.clear();
index.add(1, "foo");
await index.addAsync(2, "bar");
index.add(3, "FooBar");
await index.commit();
let resolver = new Resolver({
index: index,
queue: true,
query: "foo bar",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
let tmp = resolver.await;
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
expect((await tmp)[0]).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
queue: true,
query: "foo bar",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
expect(resolver.result).to.eql([]);
expect(resolver.await).to.be.instanceof(Promise);
tmp = resolver.await;
resolver = resolver.resolve({
limit: 1,
offset: 1
});
expect(resolver).to.be.instanceof(Promise);
expect((await resolver)[0]).oneOf([3, 1, 2]);
expect((await resolver)[1]).to.be.undefined;
expect((await tmp)[0]).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: true,
query: "bar"
}).and({
queue: true,
query: "foo",
suggest: true
});
expect(resolver).to.be.instanceof(Resolver);
resolver = resolver.resolve();
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
queue: true,
query: "bar"
}).and({
async: true,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
queue: true,
cache: true,
query: "bar"
}).and({
queue: true,
cache: true,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
queue: true,
cache: true,
query: "bar"
}).and({
async: true,
cache: true,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
async: false,
cache: true,
query: "bar"
}).and({
queue: true,
cache: false,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
// -----------------------------------
resolver = new Resolver({
index: index,
queue: true,
cache: true,
query: "bar"
}).and({
async: false,
cache: false,
query: "foo",
suggest: true,
resolve: true
});
expect(resolver).to.be.instanceof(Promise);
expect(await resolver).to.have.members([3, 1, 2]);
});
}