1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-09-25 04:51:29 +02:00

update index.d.ts

This commit is contained in:
Thomas Wilkerling
2025-05-08 20:47:58 +02:00
parent 650cf81d9e
commit 2431b51a4f
61 changed files with 4455 additions and 4607 deletions

View File

@@ -1,3 +1,4 @@
import Document from "./document.js";
import Index from "./index.js";
import WorkerIndex from "./worker.js";
@@ -8,6 +9,8 @@ export default function (prototype) {
register.call(prototype, "search");
register.call(prototype, "update");
register.call(prototype, "remove");
register.call(prototype, "searchCache");
}
let timer, timestamp, cycle;

View File

@@ -22,6 +22,7 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js";
/** @export */Index.prototype.addAsync;
/** @export */Index.prototype.appendAsync;
/** @export */Index.prototype.searchAsync;
/** @export */Index.prototype.searchCacheAsync;
/** @export */Index.prototype.updateAsync;
/** @export */Index.prototype.removeAsync;
/** @export */Index.prototype.export;
@@ -66,6 +67,7 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js";
/** @export */Document.prototype.addAsync;
/** @export */Document.prototype.appendAsync;
/** @export */Document.prototype.searchAsync;
/** @export */Document.prototype.searchCacheAsync;
/** @export */Document.prototype.updateAsync;
/** @export */Document.prototype.removeAsync;
/** @export */Document.prototype.mount;
@@ -217,6 +219,7 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js";
/** @export */SearchOptions.suggest;
/** @export */SearchOptions.resolve;
/** @export */SearchOptions.enrich;
/** @export */SearchOptions.cache;
/** @export */SearchOptions.resolution;
/** @export */DocumentSearchOptions.query;
@@ -224,7 +227,10 @@ import { KeystoreMap, KeystoreArray, KeystoreSet } from "./keystore.js";
/** @export */DocumentSearchOptions.offset;
/** @export */DocumentSearchOptions.context;
/** @export */DocumentSearchOptions.suggest;
/** @export */DocumentSearchOptions.resolve;
/** @export */DocumentSearchOptions.enrich;
/** @export */DocumentSearchOptions.cache;
/** @export */DocumentSearchOptions.resolution;
/** @export */DocumentSearchOptions.tag;
/** @export */DocumentSearchOptions.field;
/** @export */DocumentSearchOptions.index;

View File

@@ -12,7 +12,7 @@ import { SearchOptions, DocumentSearchOptions } from "./type.js";
export function searchCache(query_or_options, limit_or_options, options) {
const query = ("object" == typeof query_or_options ? "" + query_or_options.query : "" + query_or_options).toLowerCase();
const query = (limit_or_options ? "" + query_or_options : "object" == typeof query_or_options ? "" + query_or_options.query : query_or_options).toLowerCase();
if (!this.cache) {
this.cache = new CacheClass();

View File

@@ -36,8 +36,7 @@ export default function RedisDB(name, config = {}) {
this.id = (name ? sanitize(name) : "flexsearch") + "|";
this.field = config.field ? "-" + sanitize(config.field) : "";
this.type = config.type || "";
this.fastupdate = /* tag? */ /* stringify */ /* stringify */!0 /*await rows.hasNext()*/ /*await rows.hasNext()*/
/*await rows.hasNext()*/;
this.fastupdate = /* tag? */ /* stringify */ /* stringify */!0 /*await rows.hasNext()*/ /*await rows.hasNext()*/ /*await rows.hasNext()*/;
this.db = config.db || DB || null;
this.support_tag_search = !0;
this.resolution = 9;
@@ -156,7 +155,7 @@ RedisDB.prototype.enrich = function (ids) {
if ("object" != typeof ids) {
ids = [ids];
}
return this.db.hmGet(this.id + "doc", ids).then(function (res) {
return this.db.hmGet(this.id + "doc", "number" === this.type ? ids.map(i => "" + i) : ids).then(function (res) {
for (let i = 0; i < res.length; i++) {
res[i] = {
id: ids[i],
@@ -384,7 +383,7 @@ RedisDB.prototype.commit = async function (flexsearch, _replace, _append) {
const ref = this.id + "ctx" + this.field + ":" + ctx_key + ":" + key;
trx.zAdd(ref, result);
// if(this.fastupdate) for(let j = 0; j < ids.length; j++){
// trx.sAdd("ref" + this.field + ":" + ids[j], ref);
// }
if (this.fastupdate) for (let j = 0, id; j < ids.length; j++) {
// Map performs bad when pushing numeric-like values as key

View File

@@ -87,6 +87,7 @@ export function highlight_fields(query, result, index, pluck, config) {
let res;
if (pluck) {
//res = result[0].result;
res = result;
path = pluck;
} else {

View File

@@ -45,6 +45,13 @@ Document.prototype.search = function (query, limit, options, _promises) {
}
}
if (options && options.cache) {
options.cache = /* suggest */ /* append: */ /* enrich */!1;
const res = this.searchCache(query, limit, options);
options.cache = /* tag? */ /* stringify */ /* stringify */ /* single param */ /* skip update: */ /* append: */ /* skip update: */ /* skip_update: */!0 /*await rows.hasNext()*/ /*await rows.hasNext()*/ /*await rows.hasNext()*/;
return res;
}
/** @type {
* DocumentSearchResults|
* EnrichedDocumentSearchResults|
@@ -63,8 +70,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
tag,
offset,
count = 0,
resolve = /* tag? */ /* stringify */ /* stringify */
/* single param */ /* skip update: */ /* append: */ /* skip update: */ /* skip_update: */!0 /*await rows.hasNext()*/ /*await rows.hasNext()*/ /*await rows.hasNext()*/,
resolve = !0,
highlight;
@@ -82,7 +88,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
field = pluck || options.field || (field = options.index) && (field.index ? null : field);
tag = this.tag && options.tag;
suggest = options.suggest;
resolve = /* suggest */ /* append: */ /* enrich */!1 !== options.resolve;
resolve = !1 !== options.resolve;
// upgrade pluck when missing
if (!resolve && !pluck) {
@@ -102,13 +108,15 @@ Document.prototype.search = function (query, limit, options, _promises) {
}
}
if (this.store && options.enrich && !resolve) {
if (this.store && options.highlight && !resolve) {
console.warn("Highlighting results can only be done on a final resolver task or when calling .resolve({ highlight: ... })");
} else if (this.store && options.enrich && !resolve) {
console.warn("Enrich results can only be done on a final resolver task or when calling .resolve({ enrich: true })");
}
highlight = resolve && this.store && options.highlight;
enrich = highlight || resolve && this.store && options.enrich;
enrich = !!highlight || resolve && this.store && options.enrich;
limit = options.limit || limit;
offset = options.offset || 0;
limit || (limit = 100);
@@ -211,8 +219,11 @@ Document.prototype.search = function (query, limit, options, _promises) {
}
field || (field = this.field);
let promises = !_promises && (this.worker || this.db /*|| this.async*/) && [],
db_tag_search;
let db_tag_search,
promises = (this.worker || this.db /*||
(SUPPORT_ASYNC && this.async)*/
) && !_promises && [];
// multi field search
@@ -235,11 +246,11 @@ Document.prototype.search = function (query, limit, options, _promises) {
field_options = key;
key = field_options.field;
query = field_options.query || query;
limit = field_options.limit || limit;
offset = field_options.offset || offset;
suggest = field_options.suggest || suggest;
enrich = this.store && (field_options.enrich || enrich);
highlight = enrich && (options.highlight || highlight);
limit = inherit(field_options.limit, limit);
offset = inherit(field_options.offset, offset);
suggest = inherit(field_options.suggest, suggest);
highlight = resolve && this.store && inherit(field_options.highlight, highlight);
enrich = !!highlight || resolve && this.store && inherit(field_options.enrich, enrich);
}
if (_promises) {
@@ -296,7 +307,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
arr.push(ids);
} else if (!suggest) {
// no tags found
return resolve ? result : new Resolver(result);
return resolve || !!0 ? result : new Resolver(result);
}
}
}
@@ -312,7 +323,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
if (suggest) {
continue;
} else {
return resolve ? result : new Resolver(result);
return resolve || !!0 ? result : new Resolver(result);
}
}
@@ -324,7 +335,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
arr.push(ids);
} else if (!suggest) {
// no tags found
return resolve ? result : new Resolver(result);
return resolve || !!0 ? result : new Resolver(result);
}
}
}
@@ -334,7 +345,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
len = res.length;
if (!len && !suggest) {
// nothing matched
return resolve ? res : new Resolver( /** @type {IntermediateSearchResults} */res);
return resolve || !!0 ? res : new Resolver( /** @type {IntermediateSearchResults} */res);
}
// move counter back by 1
count--;
@@ -347,7 +358,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
count++;
} else if (1 === field.length) {
// fast path: nothing matched
return resolve ? result : new Resolver(result);
return resolve || !!0 ? result : new Resolver(result);
}
}
@@ -365,7 +376,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
if (suggest) {
continue;
} else {
return resolve ? result : new Resolver(result);
return resolve || !!0 ? result : new Resolver(result);
}
}
@@ -383,7 +394,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
}
if (!count) {
return resolve ? result : new Resolver(result);
return resolve || !!0 ? result : new Resolver(result);
}
if (pluck && (!enrich || !this.store)) {
return (/** @type {SearchResults} */result[0]
@@ -411,7 +422,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
}
if (pluck) {
return resolve ? highlight ? highlight_fields( /** @type {string} */query, res, this.index, pluck, highlight) : /** @type {SearchResults|EnrichedSearchResults} */res : new Resolver( /** @type {IntermediateSearchResults} */res);
return resolve || !!0 ? highlight ? highlight_fields( /** @type {string} */query, res, this.index, pluck, highlight) : /** @type {SearchResults|EnrichedSearchResults} */res : new Resolver( /** @type {IntermediateSearchResults} */res);
}
result[i] = {
@@ -433,6 +444,10 @@ Document.prototype.search = function (query, limit, options, _promises) {
return merge ? merge_fields(result) : highlight ? highlight_fields( /** @type {string} */query, result, this.index, pluck, highlight) : /** @type {DocumentSearchResults} */result;
};
function inherit(target_value, default_value) {
return "undefined" == typeof target_value ? default_value : target_value;
}
// todo support Resolver
// todo when searching through multiple fields each term should
// be found at least by one field to get a valid match without

View File

@@ -92,8 +92,7 @@ Encoder.prototype.assign = function (options) {
* pre-processing string input
* @type {Function|boolean}
*/
this.normalize = /** @type {Function|boolean} */merge_option(options.normalize, /* tag? */ /* stringify */
/* stringify */ /* single param */ /* skip update: */ /* append: */ /* skip update: */ /* skip_update: */ /* skip deletion */!0 /*await rows.hasNext()*/
this.normalize = /** @type {Function|boolean} */merge_option(options.normalize, /* tag? */ /* stringify */ /* stringify */ /* single param */ /* skip update: */ /* append: */ /* skip update: */ /* skip_update: */ /* skip deletion */!0 /*await rows.hasNext()*/
/*await rows.hasNext()*/ /*await rows.hasNext()*/, this.normalize);
// {

View File

@@ -8,12 +8,6 @@ import Resolver from "../resolver.js";
import { intersect } from "../intersect.js";
import resolve_default from "../resolve/default.js";
// todo remove
// let global_resolve = 1;
// export function set_resolve(resolve){
// global_resolve = resolve;
// }
/**
* @param {string|SearchOptions} query
* @param {number|SearchOptions=} limit
@@ -35,6 +29,14 @@ Index.prototype.search = function (query, limit, options) {
}
}
if (options && options.cache) {
options.cache = /* suggest */ /* append: */ /* enrich */!1;
const res = this.searchCache(query, limit, options);
options.cache = /* tag? */ /* stringify */ /* stringify */ /* single param */ /* skip update: */ /* append: */ /* skip update: */ /* skip_update: */ /* skip deletion */!0 /*await rows.hasNext()*/ /*await rows.hasNext()*/ /*await rows.hasNext()*/
;
return res;
}
/** @type {!Array<IntermediateSearchResults>} */
let result = [],
length,
@@ -57,14 +59,15 @@ Index.prototype.search = function (query, limit, options) {
offset = options.offset || 0;
context = options.context;
suggest = options.suggest;
resolve = /*global_resolve &&*/ /* suggest */ /* append: */ /* enrich */!1 !== options.resolve;
//resolve || (global_resolve = 0);
resolve = options.resolve;
enrich = resolve && options.enrich;
boost = options.boost;
resolution = options.resolution;
tag = this.db && options.tag;
} else {
resolve = this.resolve; // || global_resolve;
}
if ("undefined" == typeof resolve) {
resolve = this.resolve;
}
context = this.depth && !1 !== context;

View File

@@ -119,6 +119,7 @@ export function intersect(arrays, resolution, limit, offset, suggest, boost, res
const final = [];
for (let i = 0, arr; i < result.length; i++) {
arr = result[i];
//if(!arr) continue;
if (arr.length > offset) {
offset -= arr.length;
continue;

View File

@@ -1,6 +1,5 @@
import Index from "./index.js";
import default_resolver from "./resolve/default.js";
//import { set_resolve } from "./index/search.js";
import { apply_enrich } from "./document/search.js";
import { ResolverOptions, IntermediateSearchResults } from "./type.js";
import "./resolve/handler.js";
@@ -100,8 +99,6 @@ Resolver.prototype.boost = function (boost) {
* @param {boolean=} enrich
*/
Resolver.prototype.resolve = function (limit, offset, enrich) {
//set_resolve(1);
const result = this.result,
index = this.index;

View File

@@ -125,6 +125,7 @@ export let StoreOptions = {};
* suggest: (boolean|undefined),
* resolve: (boolean|undefined),
* enrich: (boolean|undefined),
* cache: (boolean|undefined)
* }}
*/
export let SearchOptions = {};
@@ -134,9 +135,12 @@ export let SearchOptions = {};
* query: (string|undefined),
* limit: (number|undefined),
* offset: (number|undefined),
* resolution: (number|undefined),
* context: (boolean|undefined),
* suggest: (boolean|undefined),
* resolve: (boolean|undefined),
* enrich: (boolean|undefined),
* cache: (boolean|undefined),
* tag: (Object|Array<Object>|undefined),
* field: (Array<string>|Array<DocumentSearchOptions>|DocumentSearchOptions|string|undefined),
* index: (Array<string>|Array<DocumentSearchOptions>|DocumentSearchOptions|string|undefined),