Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,20 @@ class InMemoryPackageIndex {
}

// do text matching
final textResults = _searchText(packages, query.parsedQuery.text);
final parsedQueryText = query.parsedQuery.text;
final textResults = _searchText(
packages,
parsedQueryText,
includeNameMatches: (query.offset ?? 0) == 0,
);

// filter packages that doesn't match text query
if (textResults != null) {
final keys = textResults.pkgScore.getKeys();
packages.removeWhere((x) => !keys.contains(x));
}

List<String>? nameMatches;
final nameMatches = textResults?.nameMatches;
List<PackageHit> packageHits;
switch (query.effectiveOrder ?? SearchOrder.top) {
case SearchOrder.top:
Expand All @@ -175,12 +180,6 @@ class InMemoryPackageIndex {
/// multiplication outcomes.
final overallScore = textResults.pkgScore
.map((key, value) => value * _adjustedOverallScores[key]!);
// If the search hits have an exact name match, we move it to the front of the result list.
final parsedQueryText = query.parsedQuery.text;
if (parsedQueryText != null &&
_documentsByName.containsKey(parsedQueryText)) {
nameMatches = <String>[parsedQueryText];
}
packageHits = _rankWithValues(overallScore.getValues());
break;
case SearchOrder.text:
Expand Down Expand Up @@ -242,12 +241,16 @@ class InMemoryPackageIndex {
}
}

_TextResults? _searchText(Set<String> packages, String? text) {
_TextResults? _searchText(
Set<String> packages,
String? text, {
required bool includeNameMatches,
}) {
final sw = Stopwatch()..start();
if (text != null && text.isNotEmpty) {
final words = splitForQuery(text);
if (words.isEmpty) {
return _TextResults(Score.empty(), {});
return _TextResults.empty();
}

bool aborted = false;
Expand All @@ -261,6 +264,12 @@ class InMemoryPackageIndex {
return aborted;
}

Set<String>? nameMatches;
if (includeNameMatches && _documentsByName.containsKey(text)) {
nameMatches ??= <String>{};
nameMatches.add(text);
}

// Multiple words are scored separately, and then the individual scores
// are multiplied. We can use a package filter that is applied after each
// word to reduce the scope of the later words based on the previous results.
Expand All @@ -272,6 +281,11 @@ class InMemoryPackageIndex {
for (final word in words) {
final nameScore =
_packageNameIndex.searchWord(word, packages: wordScopedPackages);
if (includeNameMatches && _documentsByName.containsKey(word)) {
nameMatches ??= <String>{};
nameMatches.add(word);
}

final descr = _descrIndex
.searchWords([word], weight: 0.90, limitToIds: wordScopedPackages);
final readme = _readmeIndex
Expand Down Expand Up @@ -343,7 +357,11 @@ class InMemoryPackageIndex {
score = Score(matched);
}

return _TextResults(score, topApiPages);
return _TextResults(
score,
topApiPages,
nameMatches: nameMatches?.toList(),
);
}
return null;
}
Expand Down Expand Up @@ -423,8 +441,19 @@ class InMemoryPackageIndex {
class _TextResults {
final Score pkgScore;
final Map<String, List<MapEntry<String, double>>> topApiPages;
final List<String>? nameMatches;

factory _TextResults.empty() => _TextResults(
Score.empty(),
{},
nameMatches: null,
);

_TextResults(this.pkgScore, this.topApiPages);
_TextResults(
this.pkgScore,
this.topApiPages, {
required this.nameMatches,
});
}

/// A simple (non-inverted) index designed for package name lookup.
Expand Down
1 change: 1 addition & 0 deletions app/test/search/angular_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 2,
'nameMatches': ['angular'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'angular', 'score': 1.0},
Expand Down
1 change: 1 addition & 0 deletions app/test/search/api_doc_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 2,
'nameMatches': ['foo'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'foo', 'score': 1.0}, // finds package name
Expand Down
1 change: 1 addition & 0 deletions app/test/search/flutter_iap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 1,
'nameMatches': ['flutter_iap'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'flutter_iap', 'score': 1.0},
Expand Down
1 change: 1 addition & 0 deletions app/test/search/haversine_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ MIT'''),
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 3,
'nameMatches': ['haversine'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'haversine', 'score': 1.0},
Expand Down
2 changes: 2 additions & 0 deletions app/test/search/maps_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 2,
'nameMatches': ['maps'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'maps', 'score': 1.0},
Expand All @@ -40,6 +41,7 @@ void main() {
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 2,
'nameMatches': ['map'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'maps', 'score': 1.0},
Expand Down
1 change: 1 addition & 0 deletions app/test/search/mem_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 2,
'nameMatches': ['http'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'http'},
Expand Down
1 change: 1 addition & 0 deletions app/test/search/travis_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i
expect(json.decode(json.encode(result)), {
'timestamp': isNotNull,
'totalCount': 1,
'nameMatches': ['travis'],
'sdkLibraryHits': [],
'packageHits': [
{'package': 'travis', 'score': 1.0},
Expand Down
Loading