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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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