Skip to content

Commit 80fcb3e

Browse files
authored
Merge library-related fields in SDK index. (#8773)
1 parent 3d75bce commit 80fcb3e

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

app/lib/search/sdk_mem_index.dart

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ const _defaultApiPageDirWeights = {
3434
class SdkMemIndex {
3535
final String _sdk;
3636
final Uri _baseUri;
37-
final _tokensPerLibrary = <String, TokenIndex<String>>{};
38-
final _baseUriPerLibrary = <String, String>{};
39-
final _descriptionPerLibrary = <String, String>{};
37+
final _libraries = <String, _Library>{};
4038
final Map<String, double> _apiPageDirWeights;
4139

4240
SdkMemIndex({
@@ -78,6 +76,9 @@ class SdkMemIndex {
7876
Set<String>? allowedLibraries,
7977
) {
8078
final textsPerLibrary = <String, Map<String, String>>{};
79+
final baseUrls = <String, String>{};
80+
final descriptions = <String, String>{};
81+
8182
for (final f in index.entries) {
8283
final library = f.qualifiedName?.split('.').first;
8384
if (library == null) continue;
@@ -88,11 +89,11 @@ class SdkMemIndex {
8889
continue;
8990
}
9091
if (f.isLibrary) {
91-
_baseUriPerLibrary[library] = _baseUri.resolve(f.href!).toString();
92+
baseUrls[library] = _baseUri.resolve(f.href!).toString();
9293

9394
final desc = f.desc?.replaceAll(RegExp(r'\s+'), ' ').trim();
9495
if (desc != null && desc.isNotEmpty) {
95-
_descriptionPerLibrary[library] = desc;
96+
descriptions[library] = desc;
9697
}
9798
}
9899

@@ -103,7 +104,12 @@ class SdkMemIndex {
103104
}
104105
}
105106
for (final e in textsPerLibrary.entries) {
106-
_tokensPerLibrary[e.key] = TokenIndex.fromMap(e.value);
107+
_libraries[e.key] = _Library(
108+
name: e.key,
109+
baseUrl: baseUrls[e.key],
110+
description: descriptions[e.key],
111+
tokenIndex: TokenIndex.fromMap(e.value),
112+
);
107113
}
108114
}
109115

@@ -116,24 +122,22 @@ class SdkMemIndex {
116122
if (words.isEmpty) return <SdkLibraryHit>[];
117123

118124
final hits = <_Hit>[];
119-
for (final library in _tokensPerLibrary.keys) {
125+
for (final library in _libraries.values) {
120126
// We may reduce the rank of certain libraries, except when their name is
121127
// also part of the query. E.g. `dart:html` with `query=cursor` may be
122128
// scored lower than `query=html cursor`.
123-
final isQualifiedQuery = query.contains(library.split(':').last);
129+
final isQualifiedQuery = query.contains(library.lastNamePart);
124130

125-
final tokens = _tokensPerLibrary[library]!;
126-
final plainResults = tokens.withSearchWords(
127-
words, (score) => score.top(3, minValue: 0.05));
131+
final plainResults = library.tokenIndex
132+
.withSearchWords(words, (score) => score.top(3, minValue: 0.05));
128133
if (plainResults.isEmpty) continue;
129134

130-
final libraryWeight = _libraryWeights[library] ?? 1.0;
131135
final weightedResults = isQualifiedQuery
132136
? plainResults
133137
: plainResults.map(
134138
(key, value) {
135139
final dir = p.dirname(key);
136-
final w = (_apiPageDirWeights[dir] ?? 1.0) * libraryWeight;
140+
final w = (_apiPageDirWeights[dir] ?? 1.0) * library.weight;
137141
return MapEntry(key, w * value);
138142
},
139143
);
@@ -153,9 +157,9 @@ class SdkMemIndex {
153157
.where((h) => h.score >= minScore)
154158
.map((hit) => SdkLibraryHit(
155159
sdk: _sdk,
156-
library: hit.library,
157-
description: _descriptionPerLibrary[hit.library],
158-
url: _baseUriPerLibrary[hit.library] ?? _baseUri.toString(),
160+
library: hit.library.name,
161+
description: hit.library.description,
162+
url: hit.library.baseUrl ?? _baseUri.toString(),
159163
score: hit.score,
160164
apiPages: hit.top.entries
161165
.map(
@@ -171,14 +175,31 @@ class SdkMemIndex {
171175

172176
@visibleForTesting
173177
String? getLibraryDescription(String library) =>
174-
_descriptionPerLibrary[library];
178+
_libraries[library]?.description;
175179
}
176180

177181
class _Hit {
178-
final String library;
182+
final _Library library;
179183
final Map<String, double> top;
180184

181185
_Hit(this.library, this.top);
182186

183187
late final score = top.values.fold(0.0, (a, b) => max(a, b));
184188
}
189+
190+
class _Library {
191+
final String name;
192+
final String? baseUrl;
193+
final String? description;
194+
final TokenIndex<String> tokenIndex;
195+
196+
_Library({
197+
required this.name,
198+
required this.baseUrl,
199+
required this.description,
200+
required this.tokenIndex,
201+
});
202+
203+
late final weight = _libraryWeights[name] ?? 1.0;
204+
late final lastNamePart = name.split(':').last;
205+
}

0 commit comments

Comments
 (0)