Files
it-tools/src/composable/flexSearch.ts
T
Dark_Phoenix_ 02df1bc9f2 Improve IES Lighting guidelines performance (#83)
* Improved performance on the Unicode Search page:
- Created new flexSearch component (Faster than fuzzySearch). For more information, see https://github.com/nextapps-de/flexsearch
- Replaced fuzzySearch with the flexSearch component on the unicode search page
- Fixed a bug where the search limit counter didn't have any effect
- Reduced the default search limit from 100 to 40 to reduce lag from loading all the ui elements

* Added clarification to comment

* components.d.ts update

* - Replaced FuzzySearch with FlexSearch on the ISO 3166 Searcher page
- Reduced the debounce from 500ms to 250ms
- Reduced the default search limit from 100 to 20, and fixed a bug where the search limit wasn't applied when the value changed
- Added the `readonly` prop to the `input-copyable` field
- Added the `readonly` flag to all input copyable fields on the unicode search and iso 3166 searcher pages

* Fixed incorrect case usage for functions and variables

* - Replaced fuzzysearch with flexsearch on the IES Lighting Guidelines page
- Reduced the debounce from 500ms to 250ms
- Changed the search keys so all columns are now searchable

* - Added logic to sort the limited results using a similarity score created by calculating the Levenshtein distance
- Added the `shouldSort` boolean to control whether the results should be filtered by similarity. `true` by default

* Merged new changes to flexSearch

* - Added readonly flag to input copyable fields
2025-06-08 11:09:47 +02:00

224 lines
6.7 KiB
TypeScript

import { type MaybeRef, get } from '@vueuse/core';
import { computed, ref, watch } from 'vue';
import FlexSearch from 'flexsearch';
// Define key types to match Fuse.js format
type SearchKey = string | { name: string; weight?: number };
export function useFlexSearch<Data extends Record<string, any>>({
search,
data,
options = {
keys: [],
},
limit = -1,
}: {
search: MaybeRef<string>
data: Data[]
options?: {
keys: SearchKey[]
/**
* Whether to show or not show items with an empty search query. `true` won't show any items, `false` will show all items.
* @default true
*/
filterEmpty?: boolean
/**
* Whether the results should be sorted by similarity score using the Levenshtein distance.
* @default true
*/
shouldSort?: boolean
/**
* Type of tokenization logic to use (Determines how the search logic will behave).
* @default 'forward'
* @see {@link https://github.com/nextapps-de/flexsearch?tab=readme-ov-file#tokenizer-partial-match | FlexSearch Tokenizer Documentation}
*
* Note: The `'tolerant'` tokenizer option mentioned in the documentation doesn't exist anymore in the current version of FlexSearch.
*/
tokenize?: 'strict' | 'forward' | 'reverse' | 'full'
resolution?: number
optimize?: boolean
cache?: boolean | number
}
limit?: MaybeRef<number>
}) {
// Extract options
const { keys, filterEmpty = true, shouldSort = true, tokenize = 'forward', ...indexOptions } = options;
// Normalize keys to include weights
const normalizedKeys = keys.map((key) => {
if (typeof key === 'string') {
return { name: key, weight: 1 };
}
return { name: key.name, weight: key.weight ?? 1 };
});
// Helper to get unique key for each item (prefer 'id', fallback to index)
const getItemKey = (item: Data, idx: number) => (item.id !== undefined ? item.id : idx);
// Map to store original data items by unique key
const dataMap = ref(new Map<any, Data>());
// Create separate indices for each key with weight info
const indices = normalizedKeys.map(({ name, weight }) => ({
key: name,
weight,
index: new FlexSearch.Index({
tokenize,
preset: 'performance',
resolution: 9,
optimize: true,
cache: 100,
fastupdate: true,
...indexOptions,
}),
}));
// Initialize indices with data
const initializeIndices = () => {
dataMap.value.clear();
indices.forEach(({ index }) => index.clear());
data.forEach((item, idx) => {
const itemKey = getItemKey(item, idx);
dataMap.value.set(itemKey, item);
indices.forEach(({ key, index }) => {
// Support nested keys (e.g., 'foo.bar')
const value = key.split('.').reduce((obj, path) => obj?.[path], item);
if (value) {
index.add(itemKey, String(value));
}
});
});
};
// Initialize on creation
initializeIndices();
// Watch for data changes (deep watch for array content)
watch(
() => data,
initializeIndices,
{ deep: true },
);
// Function to search across all indices with weight consideration
const searchAllIndices = (query: string, searchLimit: number) => {
if (!query) {
return [];
}
// Use a higher individual limit for each index to ensure we don't miss potential matches
const individualLimit = searchLimit > 0 ? searchLimit * 2 : undefined;
// Search each index and collect results with weights
const weightedResults = new Map<any, number>();
indices.forEach(({ index, weight }) => {
const results = index.search(query, individualLimit ? { limit: individualLimit } : undefined);
results.forEach((id) => {
const currentScore = weightedResults.get(id) || 0;
weightedResults.set(id, currentScore + weight);
});
});
// Sort by weight (higher weight = better match) and convert to array
const sortedIds = Array.from(weightedResults.entries())
.sort((a, b) => b[1] - a[1])
.map(([id]) => id);
// Apply limit and convert back to original items
const limitedIds = searchLimit > 0 ? sortedIds.slice(0, searchLimit) : sortedIds;
// Return the limited results early if it shouldn't be sorted by similarity
if (!shouldSort) {
return limitedIds
.map(id => dataMap.value.get(id))
.filter(Boolean) as Data[];
}
// Calculate Levenshtein distance
const levenshteinDistance = (str1: string, str2: string): number => {
const matrix = Array(str2.length + 1).fill(null).map(() => Array(str1.length + 1).fill(null));
for (let i = 0; i <= str1.length; i++) {
matrix[0][i] = i;
}
for (let j = 0; j <= str2.length; j++) {
matrix[j][0] = j;
}
for (let j = 1; j <= str2.length; j++) {
for (let i = 1; i <= str1.length; i++) {
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
matrix[j][i] = Math.min(
matrix[j][i - 1] + 1, // deletion
matrix[j - 1][i] + 1, // insertion
matrix[j - 1][i - 1] + indicator, // substitution
);
}
}
return matrix[str2.length][str1.length];
};
// Calculate similarity score (0-1, where 1 is perfect match)
const calculateSimilarity = (str1: string, str2: string): number => {
const maxLength = Math.max(str1.length, str2.length);
if (maxLength === 0) {
return 1;
}
const distance = levenshteinDistance(str1.toLowerCase(), str2.toLowerCase());
return 1 - (distance / maxLength);
};
// Sort limited results by similarity score
const sortedResults = limitedIds
.map((id) => {
const item = dataMap.value.get(id);
if (!item) {
return null;
}
// Calculate best similarity score across all searchable fields
const similarities = normalizedKeys.map(({ name }) => {
const value = name.split('.').reduce((obj, path) => obj?.[path], item);
return value ? calculateSimilarity(query, String(value)) : 0;
});
const bestSimilarity = Math.max(...similarities);
return {
item,
similarity: bestSimilarity,
};
})
.filter(Boolean)
.sort((a, b) => b!.similarity - a!.similarity)
.map(result => result!.item);
return sortedResults as Data[];
};
// Computed results
const searchResult = computed<Data[]>(() => {
const query = get(search);
const searchLimit: number = get(limit);
// Handle empty query case
if (!query && !filterEmpty) {
return searchLimit > 0 ? data.slice(0, searchLimit) : data;
}
if (!query) {
return [];
}
return searchAllIndices(query, searchLimit);
});
return { searchResult };
}