mirror of
https://github.com/vega-org/vega-providers.git
synced 2026-04-17 15:41:45 +00:00
fix: limit results in getPosts and enhance search filtering with fuzzy matching
This commit is contained in:
@@ -18,7 +18,8 @@ export const getPosts = async function ({
|
||||
return [];
|
||||
}
|
||||
const url = `${baseUrl}${filter}`;
|
||||
return posts({ baseUrl, url, signal, axios, cheerio });
|
||||
const result = await posts({ baseUrl, url, signal, axios, cheerio });
|
||||
return result.slice(0, 50); // Limit only for getPosts
|
||||
};
|
||||
|
||||
export const getSearchPosts = async function ({
|
||||
@@ -58,10 +59,59 @@ export const getSearchPosts = async function ({
|
||||
// Combine all posts
|
||||
const allPosts = [...moviesPosts, ...tvsPosts];
|
||||
|
||||
// Filter posts based on search query
|
||||
const filteredPosts = allPosts.filter((post) =>
|
||||
post.title.toLowerCase().includes(searchQuery.toLowerCase())
|
||||
);
|
||||
// Filter posts based on search query with improved matching
|
||||
const filteredPosts = allPosts.filter((post) => {
|
||||
const title = post.title.toLowerCase();
|
||||
const query = searchQuery.toLowerCase();
|
||||
|
||||
// Direct substring match
|
||||
if (title.includes(query)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Word boundary matching
|
||||
const queryWords = query.split(/\s+/).filter((word) => word.length > 0);
|
||||
const titleWords = title
|
||||
.split(/[\s\-\.\(\)\[\]]+/)
|
||||
.filter((word) => word.length > 0);
|
||||
|
||||
// Check if all query words are found in title words
|
||||
const allWordsMatch = queryWords.every((queryWord) =>
|
||||
titleWords.some((titleWord) => titleWord.includes(queryWord))
|
||||
);
|
||||
|
||||
if (allWordsMatch) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fuzzy matching for single word queries
|
||||
if (queryWords.length === 1) {
|
||||
const queryWord = queryWords[0];
|
||||
if (queryWord.length >= 3) {
|
||||
// Check if any title word starts with the query
|
||||
const startsWithMatch = titleWords.some((titleWord) =>
|
||||
titleWord.startsWith(queryWord)
|
||||
);
|
||||
|
||||
if (startsWithMatch) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Levenshtein distance for close matches
|
||||
const hasCloseMatch = titleWords.some((titleWord) => {
|
||||
if (Math.abs(titleWord.length - queryWord.length) > 2) return false;
|
||||
const distance = levenshteinDistance(titleWord, queryWord);
|
||||
return distance <= Math.max(1, Math.floor(queryWord.length * 0.2));
|
||||
});
|
||||
|
||||
if (hasCloseMatch) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return filteredPosts;
|
||||
};
|
||||
@@ -120,9 +170,33 @@ async function posts({
|
||||
}
|
||||
});
|
||||
|
||||
return catalog.slice(0, 50);
|
||||
// Only limit for regular getPosts, not for search
|
||||
return catalog;
|
||||
} catch (err) {
|
||||
console.error("111477 directory listing error:", err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Levenshtein distance function for fuzzy matching
|
||||
function 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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user