From 33d23cc246712bfcebb36dcc9c939fa944765d9a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Dufays Date: Mon, 12 Dec 2022 09:34:56 +0000 Subject: [PATCH] [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 --- addons/crm/models/crm_lead.py | 7 ++++--- addons/crm/tests/test_crm_lead_duplicates.py | 12 +++--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index e73e01833d4..c85b7c91224 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -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 + [ diff --git a/addons/crm/tests/test_crm_lead_duplicates.py b/addons/crm/tests/test_crm_lead_duplicates.py index 35c7927715c..20bf3cd6e50 100644 --- a/addons/crm/tests/test_crm_lead_duplicates.py +++ b/addons/crm/tests/test_crm_lead_duplicates.py @@ -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')