@@ -8,6 +8,7 @@ import 'package:_pub_shared/search/search_form.dart';
88import 'package:clock/clock.dart' ;
99import 'package:logging/logging.dart' ;
1010import 'package:meta/meta.dart' ;
11+ import 'package:pub_dev/service/topics/models.dart' ;
1112
1213import '../shared/utils.dart' show boundedList;
1314import 'models.dart' ;
@@ -37,6 +38,13 @@ class InMemoryPackageIndex {
3738 late final List <PackageHit > _likesOrderedHits;
3839 late final List <PackageHit > _pointsOrderedHits;
3940
41+ // Contains all of the topics the index had seen so far.
42+ // TODO: consider moving this into a separate index
43+ // TODO: get the list of topics from the bucket
44+ final _topics = < String > {
45+ ...canonicalTopics.aliasToCanonicalMap.values,
46+ };
47+
4048 late final DateTime _lastUpdated;
4149
4250 InMemoryPackageIndex ({
@@ -57,6 +65,12 @@ class InMemoryPackageIndex {
5765 }
5866 }
5967 }
68+
69+ // Note: we are not removing topics from this set, only adding them, no
70+ // need for tracking the current topic count.
71+ _topics.addAll (doc.tags
72+ .where ((t) => t.startsWith ('topic:' ))
73+ .map ((t) => t.split ('topic:' ).last));
6074 }
6175
6276 final packageKeys = _documents.map ((d) => d.package).toList ();
@@ -170,7 +184,21 @@ class InMemoryPackageIndex {
170184 }
171185
172186 final nameMatches = textResults? .nameMatches;
187+ List <String >? topicMatches;
173188 List <PackageHit > packageHits;
189+
190+ if (parsedQueryText != null ) {
191+ final parts = parsedQueryText
192+ .split (' ' )
193+ .map ((t) => canonicalTopics.aliasToCanonicalMap[t] ?? t)
194+ .toSet ()
195+ .where (_topics.contains)
196+ .toList ();
197+ if (parts.isNotEmpty) {
198+ topicMatches = parts;
199+ }
200+ }
201+
174202 switch (query.effectiveOrder ?? SearchOrder .top) {
175203 case SearchOrder .top:
176204 if (textResults == null ) {
@@ -229,6 +257,7 @@ class InMemoryPackageIndex {
229257 timestamp: clock.now ().toUtc (),
230258 totalCount: totalCount,
231259 nameMatches: nameMatches,
260+ topicMatches: topicMatches,
232261 packageHits: packageHits,
233262 );
234263 }
0 commit comments