Skip to content

Commit

Permalink
[DISCO-2649] Suggest: Amo keyword logic to match prefix and suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
tiftran committed Oct 24, 2023
1 parent cb3fb56 commit 42f2c56
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
- Fixed rust-log-forwarder bindings for Focus iOS ([5858](https://github.com/mozilla/application-services/pull/5858)).

## Suggest
### ✨ What's New ✨

- Edits to AMO provider keyword matching to match keyword prefix

### ⚠️ Breaking Changes ⚠️

Expand Down
26 changes: 21 additions & 5 deletions components/suggest/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ impl<'a> SuggestDao<'a> {
let (keyword_prefix, keyword_suffix) = split_keyword(&query.keyword);
let suggestions_limit = query.limit.unwrap_or(-1);

let (mut statement, params) = if query.providers.contains(&SuggestionProvider::Pocket) {
let (mut statement, params) = if query.providers.contains(&SuggestionProvider::Pocket)
|| query.providers.contains(&SuggestionProvider::Amo)
{
(self.conn.prepare_cached(
&format!(
"SELECT s.id, k.rank, s.title, s.url, s.provider, NULL as confidence, NULL as keyword_suffix
Expand All @@ -141,6 +143,11 @@ impl<'a> SuggestDao<'a> {
FROM suggestions s
JOIN pocket_keywords k ON k.suggestion_id = s.id
WHERE k.keyword_prefix = :keyword_prefix
UNION ALL
SELECT s.id, k.rank, s.title, s.url, s.provider, NULL as confidence, k.keyword_suffix
FROM suggestions s
JOIN amo_keywords k ON k.suggestion_id = s.id
WHERE k.keyword_prefix = :keyword_prefix
ORDER BY s.provider
LIMIT :suggestions_limit",
providers_to_sql_list(&query.providers),
Expand Down Expand Up @@ -234,6 +241,7 @@ impl<'a> SuggestDao<'a> {
}))
}
SuggestionProvider::Amo => {
let full_suffix = row.get::<_, String>("keyword_suffix")?;
self.conn.query_row_and_then(
"SELECT amo.description, amo.guid, amo.rating, amo.icon_url, amo.number_of_ratings, amo.score
FROM amo_custom_details amo
Expand All @@ -242,6 +250,7 @@ impl<'a> SuggestDao<'a> {
":suggestion_id": suggestion_id
},
|row| {
if full_suffix.contains(keyword_suffix){
Ok(Some(Suggestion::Amo{
title,
url: raw_url,
Expand All @@ -252,6 +261,9 @@ impl<'a> SuggestDao<'a> {
guid: row.get("guid")?,
score: row.get("score")?,
}))
} else {
Ok(None)
}
})
},
SuggestionProvider::Pocket => {
Expand Down Expand Up @@ -355,19 +367,23 @@ impl<'a> SuggestDao<'a> {
},
)?;
for (index, keyword) in suggestion.keywords.iter().enumerate() {
let (keyword_prefix, keyword_suffix) = split_keyword(keyword);
self.conn.execute(
"INSERT INTO keywords(
keyword,
"INSERT INTO amo_keywords(
keyword_prefix,
keyword_suffix,
suggestion_id,
rank
)
VALUES(
:keyword,
:keyword_prefix,
:keyword_suffix,
:suggestion_id,
:rank
)",
named_params! {
":keyword": keyword,
":keyword_prefix": keyword_prefix,
":keyword_suffix": keyword_suffix,
":rank": index,
":suggestion_id": suggestion_id,
},
Expand Down
10 changes: 9 additions & 1 deletion components/suggest/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ pub const SQL: &str = "
rank INTEGER NOT NULL,
PRIMARY KEY (keyword, suggestion_id)
) WITHOUT ROWID;
CREATE TABLE amo_keywords(
keyword_prefix TEXT NOT NULL,
keyword_suffix TEXT NOT NULL DEFAULT '',
rank INTEGER NOT NULL,
suggestion_id INTEGER NOT NULL REFERENCES suggestions(id),
PRIMARY KEY (keyword_prefix, keyword_suffix, suggestion_id)
) WITHOUT ROWID;
CREATE TABLE pocket_keywords(
keyword_prefix TEXT NOT NULL,
keyword_suffix TEXT NOT NULL DEFAULT '',
Expand Down
34 changes: 27 additions & 7 deletions components/suggest/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ mod tests {
"description": "amo suggestion",
"url": "https://addons.mozilla.org/en-US/firefox/addon/example",
"guid": "{b9db16a4-6edc-47ec-a1f4-b86292ed211d}",
"keywords": ["relay", "spam", "masking", "alias"],
"keywords": ["relay", "spam", "masking email", "alias"],
"title": "Firefox Relay",
"icon": "https://addons.mozilla.org/user-media/addon_icons/2633/2633704-64.png?modified=2c11a80b",
"rating": "4.9",
Expand Down Expand Up @@ -1945,14 +1945,34 @@ mod tests {
"#]],
),
(
"keyword = `soft`; AMP, Wikipedia, and AMO",
"keyword = `masking e`; AMO only",
SuggestionQuery {
keyword: "masking e".into(),
providers: vec![SuggestionProvider::Amo],
limit: None,
},
expect![[r#"
[
Amo {
title: "Firefox Relay",
url: "https://addons.mozilla.org/en-US/firefox/addon/example",
icon_url: "https://addons.mozilla.org/user-media/addon_icons/2633/2633704-64.png?modified=2c11a80b",
description: "amo suggestion",
rating: Some(
"4.9",
),
number_of_ratings: 888,
guid: "{b9db16a4-6edc-47ec-a1f4-b86292ed211d}",
score: 0.25,
},
]
"#]],
),
(
"keyword = `soft`; AMP and Wikipedia",
SuggestionQuery {
keyword: "soft".into(),
providers: vec![
SuggestionProvider::Amp,
SuggestionProvider::Wikipedia,
SuggestionProvider::Amo,
],
providers: vec![SuggestionProvider::Amp, SuggestionProvider::Wikipedia],
limit: None,
},
expect![[r#"
Expand Down

0 comments on commit 42f2c56

Please sign in to comment.