mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-15 02:50:10 +01:00
Improve search of document picker (#2097)
improve search of document picker
This commit is contained in:
parent
26959563e5
commit
fce0d8b709
@ -1,6 +1,6 @@
|
||||
import strDistance from "js-levenshtein";
|
||||
|
||||
const LEVENSHTEIN_MIN = 8;
|
||||
const LEVENSHTEIN_MIN = 2;
|
||||
|
||||
// Regular expression pattern to match the v4 UUID and the ending .json
|
||||
const uuidPattern =
|
||||
@ -18,20 +18,33 @@ export const stripUuidAndJsonFromString = (input = "") => {
|
||||
export function filterFileSearchResults(files = [], searchTerm = "") {
|
||||
if (!searchTerm) return files?.items || [];
|
||||
|
||||
const normalizedSearchTerm = searchTerm.toLowerCase().trim();
|
||||
|
||||
const searchResult = [];
|
||||
for (const folder of files?.items) {
|
||||
// If folder is a good match then add all its children
|
||||
if (strDistance(folder.name, searchTerm) <= LEVENSHTEIN_MIN) {
|
||||
const folderNameNormalized = folder.name.toLowerCase();
|
||||
|
||||
// Check for exact match first, then fuzzy match
|
||||
if (folderNameNormalized.includes(normalizedSearchTerm)) {
|
||||
searchResult.push(folder);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Otherwise check children for good results
|
||||
// Check children for matches
|
||||
const fileSearchResults = [];
|
||||
for (const file of folder?.items) {
|
||||
if (
|
||||
strDistance(stripUuidAndJsonFromString(file.name), searchTerm) <=
|
||||
LEVENSHTEIN_MIN
|
||||
const fileNameNormalized = stripUuidAndJsonFromString(
|
||||
file.name
|
||||
).toLowerCase();
|
||||
|
||||
// Exact match check
|
||||
if (fileNameNormalized.includes(normalizedSearchTerm)) {
|
||||
fileSearchResults.push(file);
|
||||
}
|
||||
// Fuzzy match only if no exact matches found
|
||||
else if (
|
||||
fileSearchResults.length === 0 &&
|
||||
strDistance(fileNameNormalized, normalizedSearchTerm) <= LEVENSHTEIN_MIN
|
||||
) {
|
||||
fileSearchResults.push(file);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user