ScoreAndSuggestProducts returns a scored, ranked list of product suggestions.
( query string, products []string, attributeValues map[string]map[string][]string, productGroups map[string]uint64, selectedTags []string, )
| 85 | |
| 86 | // ScoreAndSuggestProducts returns a scored, ranked list of product suggestions. |
| 87 | func ScoreAndSuggestProducts( |
| 88 | query string, |
| 89 | products []string, |
| 90 | attributeValues map[string]map[string][]string, |
| 91 | productGroups map[string]uint64, |
| 92 | selectedTags []string, |
| 93 | ) []SuggestionItem { |
| 94 | // Find allowed group from selected tags |
| 95 | var allowedGroup *uint64 |
| 96 | for _, t := range selectedTags { |
| 97 | if g, ok := productGroups[t]; ok { |
| 98 | g := g |
| 99 | allowedGroup = &g |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | filtered := make([]string, 0, len(products)) |
| 105 | for _, p := range products { |
| 106 | if allowedGroup != nil { |
| 107 | if g, ok := productGroups[p]; !ok || g != *allowedGroup { |
| 108 | continue |
| 109 | } |
| 110 | } |
| 111 | filtered = append(filtered, p) |
| 112 | } |
| 113 | |
| 114 | if query == "" { |
| 115 | items := make([]SuggestionItem, 0, len(filtered)) |
| 116 | for _, p := range filtered { |
| 117 | already := false |
| 118 | for _, t := range selectedTags { |
| 119 | if t == p { |
| 120 | already = true |
| 121 | break |
| 122 | } |
| 123 | } |
| 124 | items = append(items, SuggestionItem{Value: p, Display: p, AlreadySelected: already}) |
| 125 | } |
| 126 | return items |
| 127 | } |
| 128 | |
| 129 | q := strings.ToLower(query) |
| 130 | |
| 131 | type scored struct { |
| 132 | item SuggestionItem |
| 133 | score int64 |
| 134 | } |
| 135 | var result []scored |
| 136 | |
| 137 | fuzzyMatches := fuzzy.Find(q, filtered) |
| 138 | fuzzyScores := map[string]int{} |
| 139 | for _, m := range fuzzyMatches { |
| 140 | fuzzyScores[filtered[m.Index]] = m.Score |
| 141 | } |
| 142 | |
| 143 | for _, product := range filtered { |
| 144 | idLower := strings.ToLower(product) |
no test coverage detected