sql: unaccent strings for full text search (#9112)

This commit is contained in:
Frédéric Péters 2017-12-14 22:16:53 +01:00
parent cccecb7f12
commit def9802431
2 changed files with 13 additions and 3 deletions

View File

@ -904,6 +904,15 @@ def test_sql_criteria_fts():
t.store()
assert data_class.count([st.FtsMatch('Pierre')]) == 1
# check unaccent
user = sql.SqlUser()
user.name = u'Frédéric'.encode('utf-8')
user.store()
t.user_id = user.id
t.store()
assert data_class.count([st.FtsMatch(user.name)]) == 1
assert data_class.count([st.FtsMatch('Frederic')]) == 1
def table_exists(cur, table_name):
cur.execute('''SELECT COUNT(*) FROM information_schema.tables
WHERE table_name = %s''', (table_name,))

View File

@ -192,7 +192,7 @@ class ILike(Criteria):
class FtsMatch(Criteria):
def __init__(self, value):
self.value = value
self.value = qommon.misc.simplify(value, space=' ')
def as_sql(self):
return 'fts @@ plainto_tsquery(%%(c%s)s)' % id(self.value)
@ -1319,11 +1319,12 @@ class SqlFormData(SqlMixin, wcs.formdata.FormData):
user = self.get_user()
if user:
fts_strings.append(user.get_display_name())
fts_strings.append(user.ascii_name)
sql_statement = '''UPDATE %s SET fts = to_tsvector( %%(fts)s)
WHERE id = %%(id)s''' % self._table_name
cur.execute(sql_statement, {'id': self.id, 'fts': ' '.join(fts_strings)})
cur.execute(sql_statement, {
'id': self.id,
'fts': qommon.misc.simplify(' '.join(fts_strings), space=' ')})
conn.commit()
cur.close()