summaryrefslogtreecommitdiff
path: root/internal/discovery/discovery.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-05-23 15:33:11 +0200
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-05-29 14:36:10 +0200
commitbe74ef76b30e7ad9fc74294c8e94ff1f40e87b4e (patch)
treeb92150ebaed0d37f4baf822adc7979c846ba5091 /internal/discovery/discovery.go
parentef7f44e4bb7713b18e6c0ab1b6e3510075b6623b (diff)
Discovery: Improve search using levenshtein distance and sorting
Diffstat (limited to 'internal/discovery/discovery.go')
-rw-r--r--internal/discovery/discovery.go33
1 files changed, 5 insertions, 28 deletions
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.