trim search suggestions if there is too many

This commit is contained in:
partisan 2024-09-25 18:26:27 +02:00
parent 2679b04284
commit afe420a0ed

View file

@ -99,6 +99,9 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
} }
} }
// Trim the suggestions to a maximum of 8 items
suggestions = trimSuggestions(suggestions)
if len(suggestions) == 0 { if len(suggestions) == 0 {
printErr("All suggestion services failed. Returning empty response.") printErr("All suggestion services failed. Returning empty response.")
} }
@ -108,6 +111,14 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `["",%s]`, toJSONStringArray(suggestions)) fmt.Fprintf(w, `["",%s]`, toJSONStringArray(suggestions))
} }
// trimSuggestions trims the suggestion list to a maximum of 8 suggestions.
func trimSuggestions(suggestions []string) []string {
if len(suggestions) > 8 {
return suggestions[:8]
}
return suggestions
}
// updateLatency updates the latency of a suggestion source using an exponential moving average. // updateLatency updates the latency of a suggestion source using an exponential moving average.
func updateLatency(source *SuggestionSource, newLatency time.Duration) { func updateLatency(source *SuggestionSource, newLatency time.Duration) {
source.mu.Lock() source.mu.Lock()
@ -180,13 +191,6 @@ func fetchYahooSuggestions(query string) []string {
// return fetchSuggestionsFromURL(url) // return fetchSuggestionsFromURL(url)
// } // }
// func fetchSogouSuggestions(query string) []string {
// encodedQuery := url.QueryEscape(query)
// url := fmt.Sprintf("https://w.sugg.sogou.com/sugg/ajaj_json.jsp?key=%s", encodedQuery)
// printDebug("Fetching suggestions from Sogou: %s", url)
// return fetchSuggestionsFromURL(url)
// }
func fetchSuggestionsFromURL(url string) []string { func fetchSuggestionsFromURL(url string) []string {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {