From 0cca1c8d57ef35080c5338c258e12ba8a6cf14ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 12 Aug 2021 18:38:15 +0200 Subject: [PATCH] Use subquery when searching for similarity on translated Char issue10624 review363621002 --- trytond/model/fields/char.py | 5 +++++ trytond/tests/test_field_char.py | 32 ++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/trytond/model/fields/char.py b/trytond/model/fields/char.py index 6d5147f07a..3664d83a53 100644 --- a/trytond/model/fields/char.py +++ b/trytond/model/fields/char.py @@ -2,6 +2,7 @@ # this repository contains the full copyright notices and license terms. import warnings +from sql.conditionals import Coalesce, NullIf from sql.operators import Not from trytond.rpc import RPC @@ -91,6 +92,7 @@ class Char(FieldTranslate): language = transaction.language model, join, column = self._get_translation_column( Model, name) + column = Coalesce(NullIf(column, ''), self.sql_column(model)) else: language = None column = self.sql_column(table) @@ -106,6 +108,9 @@ class Char(FieldTranslate): database.similarity(column, sim_value) >= threshold) if operator.startswith('not'): expression = Not(expression) + if self.translate: + expression = table.id.in_( + join.select(model.id, where=expression)) key = '%s.%s.search_full_text' % (Model.__name__, name) if ((self.search_full_text or context.get(key)) diff --git a/trytond/tests/test_field_char.py b/trytond/tests/test_field_char.py index 7c92b6b405..01351798cf 100644 --- a/trytond/tests/test_field_char.py +++ b/trytond/tests/test_field_char.py @@ -586,11 +586,7 @@ class FieldCharSimilarityTestCase(ExtensionTestCase): activate_module('tests') super().setUpClass() - @with_transaction() - def test_search(self): - "Test search" - pool = Pool() - Model = pool.get('test.char') + def _test_search(self, Model): record1, record2 = Model.create([{ 'char': "word", }, { @@ -602,10 +598,18 @@ class FieldCharSimilarityTestCase(ExtensionTestCase): ]), [record1]) @with_transaction() - def test_order(self): - "Test order" + def test_search(self): + "Test search" pool = Pool() - Model = pool.get('test.char') + self._test_search(pool.get('test.char')) + + @with_transaction() + def test_search_translated(self): + "Test search translated" + pool = Pool() + self._test_search(pool.get('test.char_translate')) + + def _test_order(self, Model): record1, record2 = Model.create([{ 'char': "word", }, { @@ -618,6 +622,18 @@ class FieldCharSimilarityTestCase(ExtensionTestCase): ], order=[('char', 'DESC')]), [record2, record1]) + @with_transaction() + def test_order(self): + "Test order" + pool = Pool() + self._test_order(pool.get('test.char')) + + @with_transaction() + def test_order_translated(self): + "Test order translated" + pool = Pool() + self._test_order(pool.get('test.char_translate')) + def suite(): suite_ = unittest.TestSuite()