mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-09-25 21:08:59 +02:00
document resolver, reduce code duplication, refactoring
This commit is contained in:
129
dist/module-debug/intersect.js
vendored
129
dist/module-debug/intersect.js
vendored
@@ -1,5 +1,6 @@
|
||||
|
||||
import { create_object, concat, sort_by_length_up, get_max_len } from "./common.js";
|
||||
import { SearchResults, IntermediateSearchResults } from "./type.js";
|
||||
|
||||
/*
|
||||
|
||||
@@ -13,20 +14,20 @@ import { create_object, concat, sort_by_length_up, get_max_len } from "./common.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param arrays
|
||||
* @param {!Array<IntermediateSearchResults>} arrays
|
||||
* @param {number} resolution
|
||||
* @param limit
|
||||
* @param offset
|
||||
* @param suggest
|
||||
* @param {number} limit
|
||||
* @param {number=} offset
|
||||
* @param {boolean=} suggest
|
||||
* @param {number=} boost
|
||||
* @param {boolean=} resolve
|
||||
* @returns {Array}
|
||||
* @returns {SearchResults|IntermediateSearchResults}
|
||||
*/
|
||||
|
||||
export function intersect(arrays, resolution, limit, offset, suggest, boost, resolve) {
|
||||
|
||||
const length = arrays.length;
|
||||
|
||||
/** @type {Array<SearchResults|IntermediateSearchResults>} */
|
||||
let result = [],
|
||||
check,
|
||||
count;
|
||||
@@ -71,7 +72,7 @@ export function intersect(arrays, resolution, limit, offset, suggest, boost, res
|
||||
|
||||
if (!resolve) {
|
||||
// boost everything after first result
|
||||
let score = y + (x ? 0 : boost || 0);
|
||||
let score = y + (x || !suggest ? 0 : boost || 0);
|
||||
tmp = tmp[score] || (tmp[score] = []);
|
||||
}
|
||||
|
||||
@@ -95,7 +96,7 @@ export function intersect(arrays, resolution, limit, offset, suggest, boost, res
|
||||
return [];
|
||||
}
|
||||
|
||||
result = result[result_len - 1];
|
||||
result = /** @type {SearchResults|IntermediateSearchResults} */result[result_len - 1];
|
||||
|
||||
if (limit || offset) {
|
||||
if (resolve) {
|
||||
@@ -123,36 +124,49 @@ export function intersect(arrays, resolution, limit, offset, suggest, boost, res
|
||||
result = 1 < final.length ? concat(final) : final[0];
|
||||
}
|
||||
|
||||
return result;
|
||||
return (/** @type {SearchResults|IntermediateSearchResults} */result
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
||||
result = 1 < result.length ? union(result, offset, limit, resolve, 0) : (result = result[0]).length > limit || offset ? result.slice(offset, limit + offset) : result;
|
||||
result = 1 < result.length ? union(result, limit, offset, resolve, boost) : (result = result[0]).length > limit || offset ? result.slice(offset, limit + offset) : result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return (/** @type {SearchResults|IntermediateSearchResults} */result
|
||||
);
|
||||
}
|
||||
|
||||
export function union(arrays, offset, limit, resolve, boost) {
|
||||
/**
|
||||
* @param {Array<SearchResults|IntermediateSearchResults>} arrays
|
||||
* @param {number} limit
|
||||
* @param {number=} offset
|
||||
* @param {boolean=} resolve
|
||||
* @param {number=} boost
|
||||
* @returns {SearchResults|IntermediateSearchResults}
|
||||
*/
|
||||
export function union(arrays, limit, offset, resolve, boost) {
|
||||
|
||||
/** @type {SearchResults|IntermediateSearchResults} */
|
||||
const result = [],
|
||||
check = create_object();
|
||||
|
||||
let ids,
|
||||
id,
|
||||
arr_len = arrays.length,
|
||||
ids_len,
|
||||
count = 0;
|
||||
ids_len;
|
||||
|
||||
//let maxres = get_max_len(arrays);
|
||||
|
||||
if (!resolve) {
|
||||
|
||||
let maxres = get_max_len(arrays);
|
||||
for (let i = arr_len - 1, res, count = 0; 0 <= i; i--) {
|
||||
|
||||
for (let k = 0; k < maxres; k++) {
|
||||
res = arrays[i];
|
||||
|
||||
for (let i = arr_len - 1; 0 <= i; i--) {
|
||||
for (let k = 0; k < res.length; k++) {
|
||||
|
||||
ids = arrays[i][k];
|
||||
ids = res[k];
|
||||
ids_len = ids && ids.length;
|
||||
|
||||
if (ids_len) for (let j = 0; j < ids_len; j++) {
|
||||
@@ -164,8 +178,14 @@ export function union(arrays, offset, limit, resolve, boost) {
|
||||
if (offset) {
|
||||
offset--;
|
||||
} else {
|
||||
let score = k + (i < arr_len - 1 ? boost || 0 : 0);
|
||||
const arr = result[score] || (result[score] = []);
|
||||
// adjust score to reduce resolution of suggestions
|
||||
// todo: instead of applying the resolve task directly it could
|
||||
// be added to the chain and resolved later, that will keep
|
||||
// the original score but also can't resolve early because of
|
||||
// nothing was found
|
||||
let score = 0 | (k + (i < arr_len - 1 ? boost || 0 : 0)) / (i + 1),
|
||||
arr = result[score] || (result[score] = []);
|
||||
|
||||
arr.push(id);
|
||||
if (++count === limit) {
|
||||
return result;
|
||||
@@ -175,26 +195,23 @@ export function union(arrays, offset, limit, resolve, boost) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else for (let i = arr_len - 1; 0 <= i; i--) {
|
||||
|
||||
for (let i = arr_len - 1; 0 <= i; i--) {
|
||||
ids = arrays[i];
|
||||
ids_len = ids && ids.length;
|
||||
|
||||
ids = arrays[i];
|
||||
ids_len = ids.length;
|
||||
if (ids_len) for (let j = 0; j < ids_len; j++) {
|
||||
|
||||
for (let j = 0; j < ids_len; j++) {
|
||||
id = ids[j];
|
||||
|
||||
id = ids[j];
|
||||
|
||||
if (!check[id]) {
|
||||
check[id] = 1;
|
||||
if (offset) {
|
||||
offset--;
|
||||
} else {
|
||||
result.push(id);
|
||||
if (result.length === limit) {
|
||||
return result;
|
||||
}
|
||||
if (!check[id]) {
|
||||
check[id] = 1;
|
||||
if (offset) {
|
||||
offset--;
|
||||
} else {
|
||||
result.push(id);
|
||||
if (result.length === limit) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -205,28 +222,42 @@ export function union(arrays, offset, limit, resolve, boost) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array<number|string>} mandatory
|
||||
* @param {Array<Array<number|string>>} arrays
|
||||
* @returns {Array<number|string>}
|
||||
* @param {SearchResults|IntermediateSearchResults} arrays
|
||||
* @param {Array<SearchResults>} mandatory
|
||||
* @param {boolean=} resolve
|
||||
* @returns {SearchResults}
|
||||
*/
|
||||
|
||||
export function intersect_union(mandatory, arrays) {
|
||||
export function intersect_union(arrays, mandatory, resolve) {
|
||||
const check = create_object(),
|
||||
result = [];
|
||||
/** @type {SearchResults|IntermediateSearchResults} */
|
||||
|
||||
|
||||
for (let x = 0, ids; x < arrays.length; x++) {
|
||||
ids = arrays[x];
|
||||
for (let x = 0, ids; x < mandatory.length; x++) {
|
||||
ids = mandatory[x];
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
check[ids[i]] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0, id; i < mandatory.length; i++) {
|
||||
id = mandatory[i];
|
||||
if (1 === check[id]) {
|
||||
result.push(id);
|
||||
check[id] = 2;
|
||||
if (resolve) {
|
||||
for (let i = 0, id; i < arrays.length; i++) {
|
||||
id = arrays[i];
|
||||
if (check[id]) {
|
||||
result.push(id);
|
||||
check[id] = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (let i = 0, ids, id; i < arrays.result.length; i++) {
|
||||
ids = arrays.result[i];
|
||||
for (let j = 0; j < ids.length; j++) {
|
||||
id = ids[j];
|
||||
if (check[id]) {
|
||||
const arr = result[i] || (result[i] = []);
|
||||
arr.push(id);
|
||||
check[id] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user