From be74ef76b30e7ad9fc74294c8e94ff1f40e87b4e Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Thu, 23 May 2024 15:33:11 +0200 Subject: Discovery: Improve search using levenshtein distance and sorting --- internal/discovery/discovery.go | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) (limited to 'internal/discovery/discovery.go') diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 783febf..383a696 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -5,10 +5,10 @@ import ( "context" "encoding/json" "fmt" - "strings" "time" "github.com/eduvpn/eduvpn-common/internal/http" + "github.com/eduvpn/eduvpn-common/internal/levenshtein" "github.com/eduvpn/eduvpn-common/internal/log" "github.com/eduvpn/eduvpn-common/internal/verify" discotypes "github.com/eduvpn/eduvpn-common/types/discovery" @@ -40,20 +40,8 @@ type Organization struct { KeywordList discotypes.MapOrString `json:"keyword_list,omitempty"` } -// Matches returns if the search query `str` matches with this organization -func (s *Organization) Matches(str string) bool { - var catalog strings.Builder - for _, v := range s.DisplayName { - // length and nil error is returned - _, _ = catalog.WriteString(strings.ToLower(v)) - _, _ = catalog.WriteString(" ") - } - for _, v := range s.KeywordList { - // length and nil error is returned - _, _ = catalog.WriteString(strings.ToLower(v)) - _, _ = catalog.WriteString(" ") - } - return strings.Contains(catalog.String(), strings.ToLower(str)) +func (o *Organization) Score(search string) int { + return levenshtein.DiscoveryScore(search, o.DisplayName, o.KeywordList) } // Servers are the list of servers from https://disco.eduvpn.org/v2/server_list.json @@ -82,19 +70,8 @@ type Server struct { } // Matches returns if the search query `str` matches with this server -func (s *Server) Matches(str string) bool { - var catalog strings.Builder - for _, v := range s.DisplayName { - // length and nil error is returned - _, _ = catalog.WriteString(strings.ToLower(v)) - _, _ = catalog.WriteString(" ") - } - for _, v := range s.KeywordList { - // length and nil error is returned - _, _ = catalog.WriteString(strings.ToLower(v)) - _, _ = catalog.WriteString(" ") - } - return strings.Contains(catalog.String(), strings.ToLower(str)) +func (s *Server) Score(search string) int { + return levenshtein.DiscoveryScore(search, s.DisplayName, s.KeywordList) } // Discovery is the main structure used for this package. -- cgit v1.2.3