Skip to content

Commit ffc5b3b

Browse files
authored
Case- (and underscore-)insensitive name matching in search. (#8686)
1 parent 95628c9 commit ffc5b3b

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

app/lib/search/mem_index.dart

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,12 @@ class InMemoryPackageIndex {
368368
}
369369

370370
Set<String>? nameMatches;
371-
if (includeNameMatches && _documentsByName.containsKey(text)) {
372-
nameMatches ??= <String>{};
373-
nameMatches.add(text);
371+
if (includeNameMatches) {
372+
final matches = _packageNameIndex.lookupMatchingNames(text);
373+
if (matches != null) {
374+
nameMatches ??= <String>{};
375+
nameMatches.addAll(matches);
376+
}
374377
}
375378

376379
// Multiple words are scored separately, and then the individual scores
@@ -384,9 +387,12 @@ class InMemoryPackageIndex {
384387
final matchApi = textMatchExtent.shouldMatchApi();
385388

386389
for (final word in words) {
387-
if (includeNameMatches && _documentsByName.containsKey(word)) {
388-
nameMatches ??= <String>{};
389-
nameMatches.add(word);
390+
if (includeNameMatches) {
391+
final matches = _packageNameIndex.lookupMatchingNames(word);
392+
if (matches != null) {
393+
nameMatches ??= <String>{};
394+
nameMatches.addAll(matches);
395+
}
390396
}
391397

392398
_scorePool.withScore(
@@ -567,12 +573,21 @@ class PackageNameIndex {
567573
final List<String> _packageNames;
568574
late final List<_PkgNameData> _data;
569575

576+
/// Maps the collapsed name to all the original names (e.g. `asyncmap`=> [`async_map`, `as_y_n_cmaP`]).
577+
late final Map<String, List<String>> _collapsedNameResolvesToMap;
578+
570579
PackageNameIndex(this._packageNames) {
571580
_data = _packageNames.map((package) {
572581
final lowercased = package.toLowerCase();
573582
final collapsed = _removeUnderscores(lowercased);
574583
return _PkgNameData(lowercased, collapsed, trigrams(collapsed).toSet());
575584
}).toList();
585+
_collapsedNameResolvesToMap = {};
586+
for (var i = 0; i < _data.length; i++) {
587+
_collapsedNameResolvesToMap
588+
.putIfAbsent(_data[i].collapsed, () => [])
589+
.add(_packageNames[i]);
590+
}
576591
}
577592

578593
String _removeUnderscores(String text) => text.replaceAll('_', '');
@@ -654,6 +669,11 @@ class PackageNameIndex {
654669
}
655670
}
656671
}
672+
673+
/// Returns the list of package names where the collapsed name matches.
674+
List<String>? lookupMatchingNames(String text) {
675+
return _collapsedNameResolvesToMap[_removeUnderscores(text.toLowerCase())];
676+
}
657677
}
658678

659679
class _PkgNameData {

app/test/search/json_tool_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void main() {
2727
expect(json.decode(json.encode(result)), {
2828
'timestamp': isNotNull,
2929
'totalCount': 1,
30+
'nameMatches': ['jsontool'],
3031
'sdkLibraryHits': [],
3132
'packageHits': [
3233
{'package': 'jsontool', 'score': 1.0},
@@ -66,6 +67,7 @@ void main() {
6667
expect(json.decode(json.encode(result)), {
6768
'timestamp': isNotNull,
6869
'totalCount': 3,
70+
'nameMatches': ['jsontool'],
6971
'sdkLibraryHits': [],
7072
'packageHits': [
7173
{'package': 'json2entity', 'score': 1.0},

app/test/search/mem_index_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
9191

9292
test('package name match: async', () async {
9393
final PackageSearchResult result =
94-
index.search(ServiceSearchQuery.parse(query: 'async'));
94+
index.search(ServiceSearchQuery.parse(query: 'AsynC'));
9595
expect(json.decode(json.encode(result)), {
9696
'timestamp': isNotNull,
9797
'totalCount': 1,

app/test/search/stack_trace_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void main() {
3232
expect(json.decode(json.encode(result)), {
3333
'timestamp': isNotNull,
3434
'totalCount': 1,
35+
'nameMatches': ['stack_trace'],
3536
'sdkLibraryHits': [],
3637
'packageHits': [
3738
{

0 commit comments

Comments
 (0)