Skip to content

Commit 686db69

Browse files
committed
Remove fuzzy logic from core search command
1 parent a9a5123 commit 686db69

File tree

2 files changed

+53
-61
lines changed

2 files changed

+53
-61
lines changed

commands/core/search.go

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"strings"
2222

2323
"github.com/arduino/arduino-cli/arduino/cores"
24+
"github.com/arduino/arduino-cli/arduino/utils"
2425
"github.com/arduino/arduino-cli/commands"
2526
rpc "github.com/arduino/arduino-cli/rpc/commands"
26-
"github.com/lithammer/fuzzysearch/fuzzy"
2727
)
2828

2929
// maximumSearchDistance is the maximum Levenshtein distance accepted when using fuzzy search.
@@ -44,6 +44,26 @@ func PlatformSearch(req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error)
4444
vid, pid := searchArgs[:4], searchArgs[5:]
4545
res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
4646
} else {
47+
48+
searchArgs := strings.Split(searchArgs, " ")
49+
50+
match := func(toTest []string) (bool, error) {
51+
if len(searchArgs) == 0 {
52+
return true, nil
53+
}
54+
55+
for _, t := range toTest {
56+
matches, err := utils.Match(t, searchArgs)
57+
if err != nil {
58+
return false, err
59+
}
60+
if matches {
61+
return matches, nil
62+
}
63+
}
64+
return false, nil
65+
}
66+
4767
for _, targetPackage := range pm.Packages {
4868
for _, platform := range targetPackage.Platforms {
4969
// discard invalid platforms
@@ -60,15 +80,6 @@ func PlatformSearch(req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error)
6080
continue
6181
}
6282

63-
if searchArgs == "" {
64-
if allVersions {
65-
res = append(res, platform.GetAllReleases()...)
66-
} else {
67-
res = append(res, platformRelease)
68-
}
69-
continue
70-
}
71-
7283
// Gather all strings that can be used for searching
7384
toTest := []string{
7485
platform.String(),
@@ -82,32 +93,18 @@ func PlatformSearch(req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error)
8293
toTest = append(toTest, board.Name)
8394
}
8495

85-
// Removes some chars from query strings to enhance results
86-
cleanSearchArgs := strings.Map(func(r rune) rune {
87-
switch r {
88-
case '_':
89-
case '-':
90-
case ' ':
91-
return -1
92-
}
93-
return r
94-
}, searchArgs)
96+
// Search
97+
if ok, err := match(toTest); err != nil {
98+
return nil, err
99+
} else if !ok {
100+
continue
101+
}
95102

96-
// Fuzzy search
97-
for _, arg := range []string{searchArgs, cleanSearchArgs} {
98-
for _, rank := range fuzzy.RankFindNormalizedFold(arg, toTest) {
99-
// Accepts only results that close to the searched terms
100-
if rank.Distance < maximumSearchDistance {
101-
if allVersions {
102-
res = append(res, platform.GetAllReleases()...)
103-
} else {
104-
res = append(res, platformRelease)
105-
}
106-
goto nextPlatform
107-
}
108-
}
103+
if allVersions {
104+
res = append(res, platform.GetAllReleases()...)
105+
} else {
106+
res = append(res, platformRelease)
109107
}
110-
nextPlatform:
111108
}
112109
}
113110
}

test/test_core.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,33 @@ def get_platforms(stdout):
7676
assert "1.0.5" in platforms["Retrokits-RK002:arm"]
7777
assert "1.0.6" in platforms["Retrokits-RK002:arm"]
7878

79-
# Search using a board name
79+
# Search using board names
8080
result = run_command(f"core search myboard --all --additional-urls={url} --format json")
8181
assert result.ok
8282
platforms = get_platforms(result.stdout)
8383
assert "1.2.3" in platforms["Package:x86"]
8484

85+
def run_search(search_args, expected_ids):
86+
res = run_command(f"core search --format json {search_args}")
87+
assert res.ok
88+
data = json.loads(res.stdout)
89+
platform_ids = [p["ID"] for p in data]
90+
for platform_id in expected_ids:
91+
assert platform_id in platform_ids
92+
93+
run_search("mkr1000", ["arduino:samd"])
94+
run_search("mkr 1000", ["arduino:samd"])
95+
96+
run_search("yún", ["arduino:avr"])
97+
run_search("yùn", ["arduino:avr"])
98+
run_search("yun", ["arduino:avr"])
99+
100+
run_search("nano", ["arduino:avr", "arduino:megaavr", "arduino:samd", "arduino:mbed"])
101+
run_search("nano 33", ["arduino:samd", "arduino:mbed"])
102+
run_search("nano ble", ["arduino:mbed"])
103+
run_search("ble", ["arduino:mbed"])
104+
run_search("ble nano", ["arduino:mbed"])
105+
85106

86107
def test_core_search_no_args(run_command, httpserver):
87108
"""
@@ -146,32 +167,6 @@ def test_core_search_no_args(run_command, httpserver):
146167
assert len(platforms) == num_platforms
147168

148169

149-
def test_core_search_fuzzy(run_command):
150-
assert run_command("update")
151-
152-
def run_fuzzy_search(search_args, expected_ids):
153-
res = run_command(f"core search --format json {search_args}")
154-
assert res.ok
155-
data = json.loads(res.stdout)
156-
platform_ids = [p["ID"] for p in data]
157-
for platform_id in expected_ids:
158-
assert platform_id in platform_ids
159-
160-
run_fuzzy_search("mkr1000", ["arduino:samd"])
161-
run_fuzzy_search("mkr 1000", ["arduino:samd"])
162-
163-
run_fuzzy_search("yún", ["arduino:avr"])
164-
run_fuzzy_search("yùn", ["arduino:avr"])
165-
run_fuzzy_search("yun", ["arduino:avr"])
166-
167-
run_fuzzy_search("nano", ["arduino:avr", "arduino:megaavr", "arduino:samd", "arduino:mbed"])
168-
run_fuzzy_search("nano33", ["arduino:samd", "arduino:mbed"])
169-
run_fuzzy_search("nano 33", ["arduino:samd", "arduino:mbed"])
170-
run_fuzzy_search("nano ble", ["arduino:mbed"])
171-
run_fuzzy_search("ble", ["arduino:mbed"])
172-
run_fuzzy_search("ble nano", [])
173-
174-
175170
def test_core_updateindex_url_not_found(run_command, httpserver):
176171
assert run_command("core update-index")
177172

0 commit comments

Comments
 (0)