Skip to content

Commit

Permalink
feat: sort by first name score then overall
Browse files Browse the repository at this point in the history
  • Loading branch information
sercancicek committed Mar 20, 2024
1 parent e56754c commit 3dd5623
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/app/components/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>
<div class="app-details">
<div class="app-frame app-pad">
<div class="results">
<div ng-repeat="result in results | filter:limit_search | orderBy:'overallWeight':true track by result.model.unique_id"
<div ng-repeat="result in filteredResults | filter:limit_search | orderBy:'overallWeight':true track by result.model.unique_id"
data-ui-state="getState(result.model)" data-ui-state-params="{unique_id: result.model.unique_id}"
ng-click="onSelect()"
class="result search-result a">
Expand Down
13 changes: 7 additions & 6 deletions src/app/components/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ angular

var watchExpressions = ['query', 'checkboxStatus.show_names', 'checkboxStatus.show_descriptions', 'checkboxStatus.show_columns', 'checkboxStatus.show_column_descriptions', 'checkboxStatus.show_code', 'checkboxStatus.show_tags'];
scope.$watchGroup(watchExpressions, function() {
scope.results = filterResults(projectService.search(scope.query), scope.checkboxStatus);
scope.filteredResults = filterResults(scope.results, scope.checkboxStatus);
});

scope.shorten = function(text) {
if(text != null && text.trim().length > 0 && scope.query != null && scope.query.trim().length > 0){
let modified = text.replace(/\s+/g, ' ');
//choose the first word in the search as the anchor for shortening.
if(text != null && text.trim().length > 0 && scope.query != null && scope.query.trim().length > 0){
let modified = text.replace(/\s+/g, ' ');
//choose the first word in the search as the anchor for shortening.
//Escaping in case the first token is "*" or another reserved regex character
let first_token = escapeRegExp(getQueryTokens(scope.query)[0]);
let first_token = escapeRegExp(getQueryTokens(scope.query)[0]);
let indexOfInstance = modified.search(new RegExp(first_token));
let startIndex = (indexOfInstance - 75) < 0 ? 0 : indexOfInstance - 75;
let endIndex = (indexOfInstance + 75) > modified.length ? modified.length : indexOfInstance + 75;
Expand All @@ -116,7 +116,7 @@ angular
//e.g. "hello WORLD" changes to "(hello)|(world)"
let query_segments = getQueryTokens(scope.query);
let escaped_segments = query_segments.map(segment => escapeRegExp(segment));
let highlight_words = "(" + escaped_segments.join(")|(") + ")";
let highlight_words = "(" + escaped_segments.join(")|(") + ")";
return $sce.trustAsHtml(text.replace(new RegExp(highlight_words, 'gi'), '<span class="search-result-match">$&</span>'));
}

Expand Down Expand Up @@ -154,3 +154,4 @@ angular
}
}
}]);

17 changes: 16 additions & 1 deletion src/app/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ angular
};
_.each(results, function(result){
result.overallWeight = 0;
result.overallNameWeight = 0;
_.each(Object.keys(criteriaArr), function(criteria){
if(result.model[criteria] != undefined){
let count = 0;
Expand Down Expand Up @@ -197,6 +198,19 @@ angular
}
});
}
else if(criteria === "name"){
const calculateNameMatchWeight = (body, query) => {
if (body === query) return 10;
const lowerBody = body.toLowerCase();
if (lowerBody.startsWith(query)) return 5;
if (lowerBody.endsWith(query)) return 3;
if (lowerBody.includes(query)) return 1;
return 0;
};

count += calculateNameMatchWeight(body, ($scope.search.query).toLowerCase());
result.overallNameWeight += (count * criteriaArr[criteria]);
}
else{
body = body.toLowerCase();
let index = 0;
Expand All @@ -210,7 +224,8 @@ angular
result.overallWeight += (count * criteriaArr[criteria]);
}
});
});
});
results.sort((a, b) => b.overallNameWeight - a.overallNameWeight || b.overallWeight - a.overallWeight);
return results;
}

Expand Down

0 comments on commit 3dd5623

Please sign in to comment.