Github user tbouron commented on a diff in the pull request:
https://github.com/apache/brooklyn-ui/pull/96#discussion_r230340893
--- Diff:
ui-modules/blueprint-composer/app/components/catalog-selector/catalog-selector.directive.js
---
@@ -88,23 +89,42 @@ export function catalogSelectorSearchFilter() {
return function (items, search) {
if (search) {
return items.filter(function (item) {
- return search.toLowerCase().split(' ').reduce( (found,
part) =>
- found &&
- FIELDS_TO_SEARCH
- .filter(field => item.hasOwnProperty(field) &&
item[field])
- .reduce((match, field) => {
+ item.relevance = 0;
+ let wordNum = 0;
+ return search.toLowerCase().split(' ').reduce( (found,
part) => {
+ wordNum++;
+ let fieldNum = 0;
+ return found &&
+ FIELDS_TO_SEARCH.reduce((match, field) => {
if (match) return true;
+ fieldNum++;
+ if (!item.hasOwnProperty(field) ||
!item[field]) return false;
let text = item[field];
if (!text.toLowerCase) {
text = JSON.stringify(text).toLowerCase();
} else {
text = text.toLowerCase();
}
- return match || text.indexOf(part) > -1;
+ let index = text.indexOf(part);
+ if (index == -1) return false;
+ // found, set relevance -- uses an ad hoc
heuristic preferring first fields and short text length,
+ // earlier occurrences and earlier words
weighted more highly (smaller number is better)
+ let score = fieldNum * (2 / (1 + wordNum)) *
Math.log(1 + text.length * index);
--- End diff --
A bit complicated to understand at first but I like it! ð
---