Search: find addresses by local part, domain or name on PostgreSQL and MySQL
A 3-node PostgreSQL rehearsal found IMAP SEARCH FROM "noreply" matched 0-2 messages where RocksDB matched 23 of 930. The message indexer hands each address and display name of From/To/Cc/Bcc to the search store as keyword text (Language::None). The built-in index splits keyword text into lowercase runs of alphanumerics, so an address is found by its full form, its local part, its domain or a display-name word. The SQL backends didn't: - PostgreSQL's text parser keeps "[email protected]" as one email token (host names and URLs likewise), so neither "noreply" nor "amazon.com" ever matched it. Keyword text is now split the same way as the built-in index (SpaceTokenizer) before to_tsvector on insert and before plainto_tsquery/phraseto_tsquery on search, still under the 'simple' configuration, so the GIN index keeps serving the query. The sort columns keep the raw text. - MySQL's FULLTEXT parser already splits on punctuation, but InnoDB never indexes its stopwords ("com", "de", "www", ...) or words under innodb_ft_min_token_size (3), and a required +word it hasn't indexed matches no row. So "amazon.com", "[email protected]" or "jane doe" found nothing. Those words are now matched with a word-boundary REGEXP on the rows the indexed words select. In language text (bodies, subjects) they are dropped when other words remain, and only checked when nothing else is left, so "the invoice" no longer finds nothing either. Existing PostgreSQL search indexes hold the old single-token vectors and need a reindex (the reindexAccounts task) before address searches find old messages. MySQL needs none: only the query changed. store::search_tests gains test_address_search: five messages, 28 FROM/TO/CC/BCC searches by full address, local part, domain, domain labels, display name and hyphenated local part, plus a TEXT-style OR, with the same expected ids on every backend. It passes on RocksDB, SQLite, PostgreSQL and MySQL; on main it fails on PostgreSQL (From "noreply") and MySQL (From "[email protected]").
This commit is contained in:
@@ -128,6 +128,11 @@ pub async fn test(test: &TestServer) {
|
||||
println!("Running trace document tests...");
|
||||
test_trace_documents(store.clone()).await;
|
||||
|
||||
// inbuxa: address fields match by full address, local part, domain and
|
||||
// display name on every backend
|
||||
println!("Running address search tests...");
|
||||
test_address_search(store.clone()).await;
|
||||
|
||||
// Large document insert test
|
||||
println!("Running large document insert tests...");
|
||||
let mut large_text = String::with_capacity(20 * 1024 * 1024);
|
||||
@@ -972,3 +977,155 @@ async fn test_trace_documents(store: SearchStore) {
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// inbuxa: the message indexer passes each display name and each address of
|
||||
// From/To/Cc/Bcc as keyword text (Language::None). The built-in index splits
|
||||
// that text into words, so an address is found by its full form, its local
|
||||
// part, its domain or a display-name word; PostgreSQL kept the whole address
|
||||
// as one token and MySQL dropped stopwords such as "com" and words under three
|
||||
// characters. The expected results below are the built-in (RocksDB/SQLite)
|
||||
// results and must be the same on every backend.
|
||||
async fn test_address_search(store: SearchStore) {
|
||||
const ACCOUNT_ID: u32 = 7;
|
||||
let messages: [[&[(&str, &str)]; 4]; 5] = [
|
||||
// From, To, Cc, Bcc
|
||||
[
|
||||
&[("Amazon.com", "[email protected]")],
|
||||
&[("Jane Doe", "[email protected]")],
|
||||
&[],
|
||||
&[],
|
||||
],
|
||||
[
|
||||
&[("", "[email protected]")],
|
||||
&[("", "[email protected]")],
|
||||
&[("Jane Doe", "[email protected]")],
|
||||
&[],
|
||||
],
|
||||
[
|
||||
&[("GitHub", "[email protected]")],
|
||||
&[("Jo Li", "[email protected]")],
|
||||
&[],
|
||||
&[("Audit", "[email protected]")],
|
||||
],
|
||||
[
|
||||
&[("Jane Doe", "[email protected]")],
|
||||
&[("Amazon Web Services", "[email protected]")],
|
||||
&[("Bob", "[email protected]")],
|
||||
&[("", "[email protected]")],
|
||||
],
|
||||
[
|
||||
&[("Newsletter", "[email protected]")],
|
||||
&[("", "[email protected]")],
|
||||
&[],
|
||||
&[],
|
||||
],
|
||||
];
|
||||
let fields = [
|
||||
EmailSearchField::From,
|
||||
EmailSearchField::To,
|
||||
EmailSearchField::Cc,
|
||||
EmailSearchField::Bcc,
|
||||
];
|
||||
|
||||
let mut documents = Vec::new();
|
||||
let mut mask = RoaringBitmap::new();
|
||||
for (document_id, message) in messages.iter().enumerate() {
|
||||
let mut document = IndexDocument::new(SearchIndex::Email)
|
||||
.with_account_id(ACCOUNT_ID)
|
||||
.with_document_id(document_id as u32);
|
||||
for (field, addresses) in fields.iter().zip(message.iter()) {
|
||||
for (name, address) in addresses.iter() {
|
||||
if !name.is_empty() {
|
||||
document.index_text(field.clone(), name, Language::None);
|
||||
}
|
||||
document.index_text(field.clone(), address, Language::None);
|
||||
}
|
||||
}
|
||||
document.index_unsigned(EmailSearchField::ReceivedAt, document_id as u64);
|
||||
documents.push(document);
|
||||
mask.insert(document_id as u32);
|
||||
}
|
||||
store.index(documents).await.unwrap();
|
||||
if let SearchStore::ElasticSearch(store) = &store {
|
||||
store.refresh_index(SearchIndex::Email).await.unwrap();
|
||||
}
|
||||
|
||||
for (field, text, expected) in [
|
||||
// full address
|
||||
(EmailSearchField::From, "[email protected]", vec![0u32]),
|
||||
(EmailSearchField::To, "[email protected]", vec![0]),
|
||||
(EmailSearchField::Cc, "[email protected]", vec![1]),
|
||||
(EmailSearchField::Bcc, "[email protected]", vec![3]),
|
||||
(EmailSearchField::To, "[email protected]", vec![1, 2]),
|
||||
// local part
|
||||
(EmailSearchField::From, "noreply", vec![0, 2]),
|
||||
(EmailSearchField::To, "jo", vec![1, 2]),
|
||||
(EmailSearchField::Cc, "bob", vec![3]),
|
||||
(EmailSearchField::Bcc, "audit", vec![2]),
|
||||
// domain
|
||||
(EmailSearchField::From, "amazon.com", vec![0, 1]),
|
||||
(EmailSearchField::From, "amazon", vec![0, 1]),
|
||||
(EmailSearchField::To, "example.org", vec![0, 4]),
|
||||
(EmailSearchField::To, "io.de", vec![1, 2]),
|
||||
(EmailSearchField::Cc, "example.net", vec![3]),
|
||||
(EmailSearchField::Bcc, "example.org", vec![2]),
|
||||
(EmailSearchField::From, "www.example.com", vec![4]),
|
||||
(EmailSearchField::From, "com", vec![0, 1, 2, 4]),
|
||||
// display name
|
||||
(EmailSearchField::From, "Jane", vec![3]),
|
||||
(EmailSearchField::From, "jane doe", vec![3]),
|
||||
(EmailSearchField::To, "Web Services", vec![3]),
|
||||
(EmailSearchField::To, "Li", vec![2]),
|
||||
(EmailSearchField::Cc, "Doe", vec![1]),
|
||||
(EmailSearchField::Bcc, "Audit", vec![2]),
|
||||
// hyphenated local part
|
||||
(EmailSearchField::From, "shipment-tracking", vec![1]),
|
||||
(EmailSearchField::From, "tracking", vec![1]),
|
||||
// no match
|
||||
(EmailSearchField::From, "amazon.org", vec![]),
|
||||
(EmailSearchField::To, "noreply", vec![]),
|
||||
(EmailSearchField::Bcc, "jane", vec![]),
|
||||
] {
|
||||
let ids = store
|
||||
.query_account(
|
||||
SearchQuery::new(SearchIndex::Email)
|
||||
.with_filters(vec![
|
||||
SearchFilter::eq(SearchField::AccountId, ACCOUNT_ID),
|
||||
SearchFilter::has_keyword(field.clone(), text),
|
||||
])
|
||||
.with_comparator(SearchComparator::ascending(EmailSearchField::ReceivedAt))
|
||||
.with_mask(mask.clone()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(ids, expected, "{field:?} {text:?}");
|
||||
}
|
||||
|
||||
// TEXT-style search across all address fields
|
||||
let ids = store
|
||||
.query_account(
|
||||
SearchQuery::new(SearchIndex::Email)
|
||||
.with_filters(vec![
|
||||
SearchFilter::eq(SearchField::AccountId, ACCOUNT_ID),
|
||||
SearchFilter::Or,
|
||||
SearchFilter::has_keyword(EmailSearchField::From, "example.org"),
|
||||
SearchFilter::has_keyword(EmailSearchField::To, "example.org"),
|
||||
SearchFilter::has_keyword(EmailSearchField::Cc, "example.org"),
|
||||
SearchFilter::has_keyword(EmailSearchField::Bcc, "example.org"),
|
||||
SearchFilter::End,
|
||||
])
|
||||
.with_comparator(SearchComparator::ascending(EmailSearchField::ReceivedAt))
|
||||
.with_mask(mask.clone()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(ids, vec![0, 1, 2, 3, 4]);
|
||||
|
||||
store
|
||||
.unindex(
|
||||
SearchQuery::new(SearchIndex::Email)
|
||||
.with_filter(SearchFilter::eq(SearchField::AccountId, ACCOUNT_ID)),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user