1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-08-28 08:10:54 +02:00

fix redis enrich

This commit is contained in:
Thomas Wilkerling
2025-04-14 14:23:20 +02:00
parent 8d89fd5b14
commit ac4e0cd8d1
9 changed files with 75 additions and 71 deletions

View File

@@ -80,7 +80,7 @@ let Index;
*/
function ClickhouseDB(name, config = {}){
if(!this){
if(!this || this.constructor !== ClickhouseDB){
return new ClickhouseDB(name, config);
}
if(typeof name === "object"){
@@ -106,7 +106,7 @@ function ClickhouseDB(name, config = {}){
}
ClickhouseDB.prototype.mount = function(flexsearch){
//if(flexsearch.constructor === Document){
if(!flexsearch.encoder){
if(flexsearch.index){
return flexsearch.mount(this);
}
defaults.resolution = Math.max(flexsearch.resolution, flexsearch.resolution_ctx);

View File

@@ -55,7 +55,7 @@ const Index = create_object();
*/
function IdxDB(name, config = {}){
if(!this){
if(!this || this.constructor !== IdxDB){
return new IdxDB(name, config);
}
if(typeof name === "object"){
@@ -75,7 +75,7 @@ function IdxDB(name, config = {}){
}
IdxDB.prototype.mount = function(flexsearch){
//if(flexsearch.constructor === Document){
if(!flexsearch.encoder){
if(flexsearch.index){
return flexsearch.mount(this);
}
flexsearch.db = this;

View File

@@ -45,7 +45,7 @@ let Index = Object.create(null);
*/
function MongoDB(name, config = {}){
if(!this){
if(!this || this.constructor !== MongoDB){
return new MongoDB(name, config);
}
if(typeof name === "object"){
@@ -70,7 +70,7 @@ function MongoDB(name, config = {}){
MongoDB.prototype.mount = function(flexsearch){
//if(flexsearch.constructor === Document){
if(!flexsearch.encoder){
if(flexsearch.index){
return flexsearch.mount(this);
}
flexsearch.db = this;

View File

@@ -74,7 +74,7 @@ let DB;
*/
function PostgresDB(name, config = {}){
if(!this){
if(!this || this.constructor !== PostgresDB){
return new PostgresDB(name, config);
}
if(typeof name === "object"){
@@ -95,7 +95,7 @@ function PostgresDB(name, config = {}){
}
PostgresDB.prototype.mount = function(flexsearch){
//if(flexsearch.constructor === Document){
if(!flexsearch.encoder){
if(flexsearch.index){
return flexsearch.mount(this);
}
flexsearch.db = this;

View File

@@ -43,7 +43,7 @@ let DB, TRX;
*/
function RedisDB(name, config = {}){
if(!this){
if(!this || this.constructor !== RedisDB){
return new RedisDB(name, config);
}
if(typeof name === "object"){
@@ -69,7 +69,7 @@ function RedisDB(name, config = {}){
RedisDB.prototype.mount = function(flexsearch){
//if(flexsearch.constructor === Document){
if(!flexsearch.encoder){
if(flexsearch.index){
return flexsearch.mount(this);
}
flexsearch.db = this;
@@ -82,20 +82,24 @@ RedisDB.prototype.open = async function(){
if(this.db){
return this.db
}
if(DB){
return this.db = DB;
}
let url = defaults.url;
if(!url){
url = defaults.user
? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}`
: `redis://${defaults.host}:${defaults.port}`;
}
return this.db =
return this.db = DB =
await redis.createClient(url)
.on("error", err => console.error(err))
.connect();
};
RedisDB.prototype.close = async function(){
this.db = null;
DB && await this.db.disconnect(); // this.db.client.disconnect();
this.db = DB = null;
return this;
};
@@ -116,14 +120,16 @@ RedisDB.prototype.clear = function(){
function create_result(range, type, resolve, enrich){
if(resolve){
for(let i = 0, tmp, id; i < range.length; i++){
tmp = range[i];
id = type === "number"
? parseInt(tmp.value || tmp, 10)
: tmp.value || tmp;
range[i] = /*enrich
? { id, doc: tmp.doc }
:*/ id;
if(type === "number"){
for(let i = 0, tmp, id; i < range.length; i++){
tmp = range[i];
id = type === "number"
? parseInt(tmp.id || tmp, 10)
: tmp.id || tmp;
range[i] = enrich
? { id, doc: tmp.doc }
: id;
}
}
return range;
}
@@ -132,15 +138,11 @@ function create_result(range, type, resolve, enrich){
for(let i = 0, tmp, id, score; i < range.length; i++){
tmp = range[i];
id = type === "number"
? parseInt(tmp.value, 10)
: tmp.value;
? parseInt(tmp.id || tmp, 10)
: tmp.id || tmp;
score = tmp.score;
result[score] || (result[score] = []);
result[score].push(
enrich
? { id, doc: tmp.doc }
: id
);
result[score].push(id);
}
return result;
}

View File

@@ -68,7 +68,7 @@ const Index = Object.create(null);
*/
function SqliteDB(name, config = {}){
if(!this){
if(!this || this.constructor !== SqliteDB){
return new SqliteDB(name, config);
}
if(typeof name === "object"){
@@ -92,7 +92,7 @@ function SqliteDB(name, config = {}){
}
SqliteDB.prototype.mount = function(flexsearch){
//if(flexsearch.constructor === Document){
if(!flexsearch.encoder){
if(flexsearch.index){
return flexsearch.mount(this);
}
flexsearch.db = this;

View File

@@ -63,17 +63,19 @@ RedisDB.prototype.open = async function () {
if (this.db) {
return this.db;
}
if (DB) {
return this.db = DB;
}
let url = defaults.url;
if (!url) {
url = defaults.user ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` : `redis://${defaults.host}:${defaults.port}`;
}
return this.db = await createClient(url).on("error", err => console.error(err)).connect();
return this.db = DB = await createClient(url).on("error", err => console.error(err)).connect();
};
RedisDB.prototype.close = async function () {
// this.db.client.disconnect();
this.db = null;
DB && (await this.db.disconnect()); // this.db.client.disconnect();
this.db = DB = null;
return this;
};
@@ -87,22 +89,22 @@ RedisDB.prototype.clear = function () {
function create_result(range, type, resolve, enrich) {
if (resolve) {
for (let i = 0, tmp, id; i < range.length; i++) {
tmp = range[i];
id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp;
range[i] = /*enrich
? { id, doc: tmp.doc }
:*/id;
if ("number" === type) {
for (let i = 0, tmp, id; i < range.length; i++) {
tmp = range[i];
id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp;
range[i] = enrich ? { id, doc: tmp.doc } : id;
}
}
return range;
} else {
let result = [];
for (let i = 0, tmp, id, score; i < range.length; i++) {
tmp = range[i];
id = "number" === type ? parseInt(tmp.value, 10) : tmp.value;
id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp;
score = tmp.score;
result[score] || (result[score] = []);
result[score].push(enrich ? { id, doc: tmp.doc } : id);
result[score].push(id);
}
return result;
}

View File

@@ -63,17 +63,19 @@ RedisDB.prototype.open = async function () {
if (this.db) {
return this.db;
}
if (DB) {
return this.db = DB;
}
let url = defaults.url;
if (!url) {
url = defaults.user ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` : `redis://${defaults.host}:${defaults.port}`;
}
return this.db = await createClient(url).on("error", err => console.error(err)).connect();
return this.db = DB = await createClient(url).on("error", err => console.error(err)).connect();
};
RedisDB.prototype.close = async function () {
// this.db.client.disconnect();
this.db = null;
DB && (await this.db.disconnect()); // this.db.client.disconnect();
this.db = DB = null;
return this;
};
@@ -87,22 +89,22 @@ RedisDB.prototype.clear = function () {
function create_result(range, type, resolve, enrich) {
if (resolve) {
for (let i = 0, tmp, id; i < range.length; i++) {
tmp = range[i];
id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp;
range[i] = /*enrich
? { id, doc: tmp.doc }
:*/id;
if ("number" === type) {
for (let i = 0, tmp, id; i < range.length; i++) {
tmp = range[i];
id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp;
range[i] = enrich ? { id, doc: tmp.doc } : id;
}
}
return range;
} else {
let result = [];
for (let i = 0, tmp, id, score; i < range.length; i++) {
tmp = range[i];
id = "number" === type ? parseInt(tmp.value, 10) : tmp.value;
id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp;
score = tmp.score;
result[score] || (result[score] = []);
result[score].push(enrich ? { id, doc: tmp.doc } : id);
result[score].push(id);
}
return result;
}

View File

@@ -71,7 +71,7 @@ RedisDB.prototype.open = async function(){
? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}`
: `redis://${defaults.host}:${defaults.port}`;
}
return this.db =
return this.db = DB =
await createClient(url)
.on("error", err => console.error(err))
.connect();
@@ -79,7 +79,7 @@ RedisDB.prototype.open = async function(){
RedisDB.prototype.close = async function(){
DB && await this.db.disconnect(); // this.db.client.disconnect();
this.db = null;
this.db = DB = null;
return this;
};
@@ -100,31 +100,29 @@ RedisDB.prototype.clear = function(){
function create_result(range, type, resolve, enrich){
if(resolve){
for(let i = 0, tmp, id; i < range.length; i++){
tmp = range[i];
id = type === "number"
? parseInt(tmp.value || tmp, 10)
: tmp.value || tmp;
range[i] = /*enrich
? { id, doc: tmp.doc }
:*/ id;
if(type === "number"){
for(let i = 0, tmp, id; i < range.length; i++){
tmp = range[i];
id = type === "number"
? parseInt(tmp.id || tmp, 10)
: tmp.id || tmp;
range[i] = enrich
? { id, doc: tmp.doc }
: id;
}
}
return range;
}
else{
else {
let result = [];
for(let i = 0, tmp, id, score; i < range.length; i++){
tmp = range[i];
id = type === "number"
? parseInt(tmp.value, 10)
: tmp.value
? parseInt(tmp.id || tmp, 10)
: tmp.id || tmp;
score = tmp.score;
result[score] || (result[score] = []);
result[score].push(
enrich
? { id, doc: tmp.doc }
: id
);
result[score].push(id);
}
return result;
}