Commit ce77af41 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

Include table name in fulltext search score's alias.

parent 4b37cb22
......@@ -83,7 +83,7 @@ class TestI18NSearch(ERP5TypeTestCase):
self.assertTrue('ORDER BY\n `full_text`.`SearchableText` ASC' in self.portal.portal_catalog(SearchableText='Faure', sort_on=(('SearchableText', 'ascending'),), src__=1))
# check sort on fulltext search score
self.assertTrue('ORDER BY\n SearchableText__score__ ASC' in self.portal.portal_catalog(SearchableText='Faure', sort_on=(('SearchableText__score__', 'ascending'),), src__=1))
self.assertTrue('ORDER BY\n full_text_SearchableText__score__ ASC' in self.portal.portal_catalog(SearchableText='Faure', sort_on=(('SearchableText__score__', 'ascending'),), src__=1))
def test_catalog_full_text_title(self):
# check if 'é' == 'e' collation works
......@@ -115,12 +115,12 @@ class TestI18NSearch(ERP5TypeTestCase):
}))
# check sort on fulltext search score
self.assertFalse('ORDER BY\n title__score__' in self.portal.portal_catalog(**{
self.assertTrue('ORDER BY\n catalog_full_text_title__score__' in self.portal.portal_catalog(**{
'catalog_full_text.title':'Faure',
'sort_on':(('catalog_full_text.title__score__', 'ascending'),),
'src__':1
}))
self.assertTrue('ORDER BY\n title__score__' in self.portal.portal_catalog(**{
self.assertTrue('ORDER BY\n catalog_full_text_title__score__' in self.portal.portal_catalog(**{
'catalog_full_text.title':'Faure',
'sort_on':(('title__score__', 'ascending'),),
'src__':1
......@@ -156,7 +156,7 @@ class TestI18NSearch(ERP5TypeTestCase):
self.assertTrue('ORDER BY\n `catalog`.`title` ASC' in self.portal.portal_catalog(title='Faure', sort_on=(('title', 'ascending'),), src__=1))
# check sort on fulltext search score
self.assertTrue('ORDER BY\n title__score__' in self.portal.portal_catalog(title='Faure', sort_on=(('title__score__', 'ascending'),), src__=1))
self.assertTrue('ORDER BY\n catalog_full_text_title__score__' in self.portal.portal_catalog(title='Faure', sort_on=(('title__score__', 'ascending'),), src__=1))
def test_suite():
suite = unittest.TestSuite()
......
......@@ -453,7 +453,10 @@ class ColumnMap(object):
def asSQLColumn(self, raw_column, group=DEFAULT_GROUP_ID):
if self.catalog_table_name is None or raw_column in self.column_ignore_set or \
'.' in raw_column or '*' in raw_column:
result = raw_column
if raw_column.endswith('__score__'):
result = raw_column.replace('.', '_')
else:
result = raw_column
else:
if raw_column.endswith('__score__'):
raw_column = raw_column[:-9]
......@@ -464,7 +467,10 @@ class ColumnMap(object):
if group is DEFAULT_GROUP_ID:
group, column = self.related_key_dict.get(column, (group, raw_column))
alias = self.table_alias_dict[(group, self.column_map[(group, column)])]
result = '`%s`.`%s%s`' % (alias, column, column_suffix)
if column_suffix:
result = '%s_%s%s' % (alias, column, column_suffix)
else:
result = '`%s`.`%s`' % (alias, column)
if function is not None:
result = '%s(%s)' % (function, result)
return result
......
......@@ -116,10 +116,9 @@ class MatchComparisonOperator(MonovaluedComparisonOperator):
}
select_dict = {}
if not only_group_columns:
select_dict['%s__score__' % column.replace('`', '').rsplit('.', 1)[-1]] = match_string
# Support sort on the relevance by using (column)__score__ key.
select_dict['%s__score__' % column.replace('`', '').replace('.', '_')] = match_string
order_by_dict = {
'`%s__score__`' % '`.`'.join([x.strip('`') for x in column.split('.')]): match_string,
'%s__score__' % column.replace('`', '').replace('.', '_'): match_string,
}
return SQLExpression(
self,
......
......@@ -238,8 +238,6 @@ class SQLExpression(object):
append = result.append
order_by_dict = self._getOrderByDict()
for (column, direction, cast) in self.getOrderByList():
if column.endswith('__score__') and column not in order_by_dict:
continue
expression = conflictSafeGet(order_by_dict, column, str(column))
expression = self._reversed_select_dict.get(expression, expression)
if cast not in (None, ''):
......
......@@ -735,20 +735,20 @@ class TestSQLCatalog(ERP5TypeTestCase):
order_by_expression = sql_expression.getOrderByExpression()
self.assertNotEqual(order_by_expression, '')
# ... and must sort by relevance
self.assertEqual('fulltext__score__', order_by_expression)
self.assertEqual('foo_fulltext__score__', order_by_expression)
# ordering on fulltext column with sort order specified must preserve
# sorting by relevance.
for direction in ('ASC', 'DESC'):
sql_expression = self.asSQLExpression({'fulltext': 'foo',
'order_by_list': [('fulltext__score__', direction), ]})
order_by_expression = sql_expression.getOrderByExpression()
self.assertEqual('fulltext__score__ %s' % direction, order_by_expression)
self.assertEqual('foo_fulltext__score__ %s' % direction, order_by_expression)
# Providing a None cast should work too
for direction in ('ASC', 'DESC'):
sql_expression = self.asSQLExpression({'fulltext': 'foo',
'order_by_list': [('fulltext__score__', direction, None), ]})
order_by_expression = sql_expression.getOrderByExpression()
self.assertEqual('fulltext__score__ %s' % direction, order_by_expression)
self.assertEqual('foo_fulltext__score__ %s' % direction, order_by_expression)
def test_logicalOperators(self):
self.catalog(ReferenceQuery(ReferenceQuery(operator='=', default='AN ORB'),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment