Skip to content

Commit 36314a9

Browse files
committed
[chrome] improve results ranking algorithm
1 parent f697563 commit 36314a9

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

packages/chrome/src/components/Omnibar/Suggestion.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function Suggestion(props: {
3737
title={item.url.href}
3838
>
3939
<div class="result-icon">
40-
{item.kind === "search" ? (
40+
{item.kind === "search" || item.kind === "directsearch" ? (
4141
<Icon icon={iconSearch}></Icon>
4242
) : item.kind === "trending" ? (
4343
<Icon icon={iconTrendingUp}></Icon>

packages/chrome/src/components/Omnibar/suggestions.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { browser } from "../../Browser";
22
import { bare } from "../../IsolatedFrame";
33

4+
import * as tldts from "tldts";
5+
46
export type OmniboxResult = {
57
kind:
68
| "search"
@@ -109,11 +111,15 @@ function calculateRelevanceScore(result: OmniboxResult, query: string): number {
109111

110112
let score = 0;
111113

112-
if (urlString === lowerQuery || title === lowerQuery) {
113-
return 100;
114+
// if (urlString === lowerQuery || title === lowerQuery) {
115+
// return 100;
116+
// }
117+
118+
if (result.kind === "direct") {
119+
return 95;
114120
}
115121

116-
if (result.kind === "direct" || result.kind === "directsearch") {
122+
if (result.kind === "directsearch") {
117123
return 90;
118124
}
119125

@@ -163,7 +169,7 @@ function rankResults(
163169
suggestionDenied &&
164170
(b.kind === "direct" || b.kind === "directsearch")
165171
) {
166-
// force direct results to the top
172+
// don't allow something other than What Was Typed to be higher if the user just backspaced
167173
return 1;
168174
} else {
169175
return (b.relevanceScore || 0) - (a.relevanceScore || 0);
@@ -196,35 +202,39 @@ const fetchHistoryResults = (query: string): OmniboxResult[] => {
196202
return results.slice(0, 5);
197203
};
198204

199-
const addDirectResult = (
200-
query: string,
201-
results: OmniboxResult[]
202-
): OmniboxResult[] => {
205+
const addDirectResult = (query: string, results: OmniboxResult[]) => {
206+
let directurl;
203207
if (URL.canParse(query)) {
204-
return [
205-
{
206-
kind: "direct",
207-
url: new URL(query),
208-
title: null,
209-
favicon: null,
210-
},
211-
...results,
212-
];
208+
directurl = new URL(query);
213209
} else {
214-
return [
215-
{
216-
kind: "directsearch",
217-
url: new URL(
218-
AVAILABLE_SEARCH_ENGINES[
219-
browser.settings.defaultSearchEngine
220-
].searchUrlBuilder(query)
221-
),
222-
title: query,
223-
favicon: null,
224-
},
225-
...results,
226-
];
210+
let parsed = tldts.parse(query);
211+
if ((parsed.domain && parsed.isIcann) || parsed.isIp) {
212+
// TODO: this probably isn't right for all cases
213+
// i think typing in `://a.com` would break it because it's an invalid url but passes tldts
214+
// but tldts doesn't parse path/port/schema so we can't use its parser
215+
directurl = new URL("https://" + query);
216+
}
227217
}
218+
219+
if (directurl) {
220+
results.unshift({
221+
kind: "direct",
222+
url: directurl,
223+
title: null,
224+
favicon: null,
225+
});
226+
}
227+
228+
results.unshift({
229+
kind: "directsearch",
230+
url: new URL(
231+
AVAILABLE_SEARCH_ENGINES[
232+
browser.settings.defaultSearchEngine
233+
].searchUrlBuilder(query)
234+
),
235+
title: query,
236+
favicon: null,
237+
});
228238
};
229239

230240
const fetchGoogleSuggestions = async (
@@ -290,15 +300,15 @@ export async function fetchSuggestions(
290300
...cachedGoogleResults,
291301
];
292302

293-
combinedResults = addDirectResult(query, combinedResults);
303+
addDirectResult(query, combinedResults);
294304

295305
// first update, so the user sees something quickly
296306
setResults(rankResults(combinedResults, query, suggestionDenied));
297307

298308
const googleResults = await fetchGoogleSuggestions(query);
299309

300310
combinedResults = [...historyResults, ...googleResults];
301-
combinedResults = addDirectResult(query, combinedResults);
311+
addDirectResult(query, combinedResults);
302312

303313
// update with the new google results
304314
setResults(rankResults(combinedResults, query, suggestionDenied));

0 commit comments

Comments
 (0)