git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] brooklyn-ui pull request #96: Relevance and other composer tweaks


Github user tbouron commented on a diff in the pull request:

    https://github.com/apache/brooklyn-ui/pull/96#discussion_r230340735
  
    --- 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);
    +                            /* to debug the scoring function:
    +                            if (item.symbolicName.indexOf("EIP") >= 0 || item.symbolicName.indexOf("OpsWorks") >= 0) { 
    +                                console.log(item.symbolicName, ": match", part, "in", field,
    +                                    "#", fieldNum, wordNum, 
    +                                    "pos", index, "/", text.length, 
    +                                    ":", item.relevance, "+=", score);
    +                            }
    +                            */
    --- End diff --
    
    Seems like this can be removed


---