[IMP] crm: improve lead searching and duplicate detection

Accelerate duplicate lead detection by adding indexes on field involved in the
search.

In this commit we add trigram indexes on email_normalized, partner_name and
contact_name that are used a lot for searching and finding leads.

Task-3007714

Part-of: odoo/odoo#105873
This commit is contained in:
Pierre-Yves Dufays
2022-12-12 13:18:46 +01:00
committed by Thibault Delavallée
parent 181eabbe58
commit 33d23cc246
2 changed files with 7 additions and 12 deletions
+4 -3
View File
@@ -176,10 +176,10 @@ class Lead(models.Model):
help="Linked partner (optional). Usually created when converting the lead. You can find a partner by its Name, TIN, Email or Internal Reference.")
partner_is_blacklisted = fields.Boolean('Partner is blacklisted', related='partner_id.is_blacklisted', readonly=True)
contact_name = fields.Char(
'Contact Name', tracking=30,
'Contact Name', index='trigram', tracking=30,
compute='_compute_contact_name', readonly=False, store=True)
partner_name = fields.Char(
'Company Name', tracking=20,
'Company Name', index='trigram', tracking=20,
compute='_compute_partner_name', readonly=False, store=True,
help='The name of the future partner company that will be created while converting the lead into opportunity')
function = fields.Char('Job Position', compute='_compute_function', readonly=False, store=True)
@@ -187,6 +187,7 @@ class Lead(models.Model):
email_from = fields.Char(
'Email', tracking=40, index='trigram',
compute='_compute_email_from', inverse='_inverse_email_from', readonly=False, store=True)
email_normalized = fields.Char(index='trigram') # inherited via mail.thread.blacklist
phone = fields.Char(
'Phone', tracking=50,
compute='_compute_phone', inverse='_inverse_phone', readonly=False, store=True)
@@ -566,7 +567,7 @@ class Lead(models.Model):
if email_search:
duplicate_lead_ids |= return_if_relevant('crm.lead', common_lead_domain + [
'|', ('email_normalized', 'ilike', email_search), ('email_from', 'ilike', email_search)
('email_normalized', 'ilike', email_search)
])
if lead.partner_name and len(lead.partner_name) >= MIN_NAME_LENGTH:
duplicate_lead_ids |= return_if_relevant('crm.lead', common_lead_domain + [
+3 -9
View File
@@ -214,14 +214,8 @@ class TestLeadConvert(TestCrmCommon):
'type': 'opportunity',
'email_from': 'odoo.com'
})
lead_5 = self.env['crm.lead'].create({
'name': 'Opportunity 3',
'type': 'opportunity',
'email_from': 'myodoo.com'
})
self.assertEqual(lead_1 + lead_2, lead_1.duplicate_lead_ids)
self.assertEqual(lead_1 + lead_2, lead_2.duplicate_lead_ids)
self.assertEqual(lead_3 + lead_4 + lead_5, lead_3.duplicate_lead_ids)
self.assertEqual(lead_3 + lead_4 + lead_5, lead_4.duplicate_lead_ids)
self.assertEqual(lead_5, lead_5.duplicate_lead_ids)
self.assertEqual(lead_2, lead_2.duplicate_lead_ids, 'Using email_normalized: does not found invalid lead_1, not that annoying')
self.assertEqual(lead_3, lead_3.duplicate_lead_ids, 'Using email_normalized: does not found invalid lead_4, not that annoying')
self.assertEqual(lead_4, lead_4.duplicate_lead_ids, 'Using email_normalized: does not found invalid lead_3, not that annoying')