Merge pull request #307 from teaxyz/search-adjustments

#294 improve search exact match
This commit is contained in:
Neil 2023-03-22 09:03:45 +08:00 committed by GitHub
commit d1b7ccfcad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View file

@ -121,11 +121,13 @@
if (searchInput.value) { if (searchInput.value) {
const term = searchInput.value; const term = searchInput.value;
const packages = getPackageThumbs(); const packages = getPackageThumbs();
for(let pkg of packages) {
pkg.match_score = getMatchScore(term, pkg.dataset);
}
const sortedPackages = packages.sort((a, b) => { const sortedPackages = packages.sort((a, b) => {
const aScore = getMatchScore(term, a.dataset); return b.match_score - (a ? a.match_score : 0);
const bScore = getMatchScore(term, b.dataset);
return bScore - aScore;
}); });
const grid = document.getElementById('packageGrid'); const grid = document.getElementById('packageGrid');
grid.textContent = ''; grid.textContent = '';
let searchCount = 0; let searchCount = 0;
@ -209,10 +211,11 @@
function getMatchScore(term, dataset) { function getMatchScore(term, dataset) {
// provide higher value with name // provide higher value with name
const { name, description } = dataset; const { name, maintainer, description } = dataset;
const exactMatch = [maintainer, name].join(" ").toLocaleLowerCase().includes(term.toLocaleLowerCase());
const nameScore = stringSimilarity.compareTwoStrings(name, term); const nameScore = stringSimilarity.compareTwoStrings(name, term);
const descriptionScore = stringSimilarity.compareTwoStrings(description, term); const descriptionScore = stringSimilarity.compareTwoStrings(description, term);
return (nameScore*80) + (descriptionScore*20) return (nameScore*80) + (descriptionScore*20) + (exactMatch ? 100 : 0);
} }
const loadMoreButton = document.getElementById('loadMorePackagesBtn'); const loadMoreButton = document.getElementById('loadMorePackagesBtn');

View file

@ -1,4 +1,4 @@
<div class="card card-thumbnail" style="width: 100%" data-name="{{- .name -}}" data-popularity="{{- .dl_count -}}" data-last_modified="{{- .last_modified -}}" data-description="{{- .desc -}}"> <div class="card card-thumbnail" style="width: 100%" data-name="{{- .name -}}" data-popularity="{{- .dl_count -}}" data-last_modified="{{- .last_modified -}}" data-description="{{- .desc -}}" data-maintainer="{{- .maintainer -}}">
<a href="/+{{- lower .full_name -}}/"> <a href="/+{{- lower .full_name -}}/">
<div class="p-3"> <div class="p-3">
<figure class="card-img-top" > <figure class="card-img-top" >