Browse Source

[IMP] base_search_fuzzy: Updated README, translated UserError Exception, Added one more test

pull/462/head
Christoph Giesel 9 years ago
parent
commit
94376b3d9e
  1. 49
      base_search_fuzzy/README.rst
  2. 6
      base_search_fuzzy/i18n/base_search_fuzzy.pot
  3. 6
      base_search_fuzzy/i18n/de.po
  4. 6
      base_search_fuzzy/models/trgm_index.py
  5. 21
      base_search_fuzzy/tests/test_query_generation.py

49
base_search_fuzzy/README.rst

@ -8,49 +8,54 @@ PostgreSQL Trigram Search
This addon provides the ability to create GIN or GiST indexes of char and text This addon provides the ability to create GIN or GiST indexes of char and text
fields and also to use the search operator `%` in search domains. Currently fields and also to use the search operator `%` in search domains. Currently
this module doesn't change the backend search or something else. It provides
only the possibilty to do a fuzzy search for external addons.
this module doesn't change the backend search or anything else. It provides
only the possibilty to perfrom the fuzzy search for external addons.
Installation Installation
============ ============
First you need to have the ``pg_trgm`` extension available. In debian based
distribution you have to install the `postgresql-contrib` module. Then you have
to either install the ``pg_trgm`` extension to your database or you have to give
your postgresql user the superadmin right (this allows the odoo module to
install the extension to the database).
#. The PostgreSQL extension ``pg_trgm`` should be available. In debian based
distribution you have to install the `postgresql-contrib` module.
#. Install the ``pg_trgm`` extension to your database or give your postgresql
user the ``SUEPRUSER`` right (this allows the odoo module to install the
extension to the database).
Configuration Configuration
============= =============
If you installed the odoo module you can define ``GIN`` and ``GiST`` indexes for
`char` and `text` via `Settings -> Database Structure -> Trigram Index`. The
index name will automatically created for new entries.
If the odoo module is installed:
#. You can define ``GIN`` and ``GiST`` indexes for `char` and `text` via
`Settings -> Database Structure -> Trigram Index`. The index name will
automatically created for new entries.
Usage Usage
===== =====
For example you can create an index for the `name` field of `res.partner`. Then
in a search you can use
#. You can create an index for the `name` field of `res.partner`.
#. In the search you can use:
``self.env['res.partner'].search([('name', '%', 'Jon Miller)])``
``self.env['res.partner'].search([('name', '%', 'Jon Miller)])``
In this Example it can find existing names like `John Miller` or `John Mill`.
#. In this example the function will return positive result for `John Miller` or
`John Mill`.
Which strings can be found depends on the limit which is set (default: 0.3).
Currently you have to set the limit by executing SQL:
#. You can tweak the number of strings to be returned by adjusting the set limit
(default: 0.3). NB: Currently you have to set the limit by executing the
following SQL statment:
``self.env.cr.execute("SELECT set_limit(0.2);")``
``self.env.cr.execute("SELECT set_limit(0.2);")``
Also you can use the ``similarity(column, 'text')`` function in the ``order``
parameter to order by the similarity. This is just a basic implementation which
doesn't contains validations and has to start with this function. For example
you can define this like:
#. Another interesting feature is the use of ``similarity(column, 'text')``
function in the ``order`` parameter to order by similarity. This module just
contains a basic implementation which doesn't perform validations and has to
start with this function. For example you can define the function as
followed:
``similarity(%s.name, 'John Mil') DESC" % self.env['res.partner']._table``
``similarity(%s.name, 'John Mil') DESC" % self.env['res.partner']._table``
For further questions read the Documentation of the For further questions read the Documentation of the
`pg_trgm <https://www.postgresql.org/docs/current/static/pgtrgm.html>`_ module. `pg_trgm <https://www.postgresql.org/docs/current/static/pgtrgm.html>`_ module.

6
base_search_fuzzy/i18n/base_search_fuzzy.pot

@ -92,3 +92,9 @@ msgstr ""
#: help:trgm.index,field_id:0 #: help:trgm.index,field_id:0
msgid "You can either select a field of type \"text\" or \"char\"." msgid "You can either select a field of type \"text\" or \"char\"."
msgstr "" msgstr ""
#. module: base_search_fuzzy
#: code:addons/base_search_fuzzy/models/trgm_index.py:123
#, python-format
msgid "The pg_trgm extension does not exists or cannot be installed."
msgstr ""

6
base_search_fuzzy/i18n/de.po

@ -92,3 +92,9 @@ msgstr "Trigram Index"
#: help:trgm.index,field_id:0 #: help:trgm.index,field_id:0
msgid "You can either select a field of type \"text\" or \"char\"." msgid "You can either select a field of type \"text\" or \"char\"."
msgstr "Sie können entweder Felder vom Typ \"text\" oder \"char\" auswählen." msgstr "Sie können entweder Felder vom Typ \"text\" oder \"char\" auswählen."
#. module: base_search_fuzzy
#: code:addons/base_search_fuzzy/models/trgm_index.py:123
#, python-format
msgid "The pg_trgm extension does not exists or cannot be installed."
msgstr "Die pg_trgm Erweiterung existiert nicht oder kann nicht installiert werden."

6
base_search_fuzzy/models/trgm_index.py

@ -2,7 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging import logging
from openerp import SUPERUSER_ID, api, exceptions, fields, models
from openerp import _, SUPERUSER_ID, api, exceptions, fields, models
from psycopg2.extensions import AsIs from psycopg2.extensions import AsIs
@ -119,9 +119,9 @@ class TrgmIndex(models.Model):
self.ensure_one() self.ensure_one()
if not self._install_trgm_extension(): if not self._install_trgm_extension():
raise exceptions.UserError(
raise exceptions.UserError(_(
'The pg_trgm extension does not exists or cannot be ' 'The pg_trgm extension does not exists or cannot be '
'installed.')
'installed.'))
table_name = self.env[self.field_id.model_id.model]._table table_name = self.env[self.field_id.model_id.model]._table
column_name = self.field_id.name column_name = self.field_id.name

21
base_search_fuzzy/tests/test_query_generation.py

@ -37,6 +37,27 @@ class QueryGenerationCase(TransactionCase):
'SELECT FROM "res_partner" WHERE ' 'SELECT FROM "res_partner" WHERE '
'("res_partner"."name" % \'test\')') '("res_partner"."name" % \'test\')')
def test_fuzzy_where_generation_translatable(self):
"""Check the generation of the where clause for translatable fields."""
ctx = {'lang': 'de_DE'}
# create new query with fuzzy search operator
query = self.ResPartnerCategory.with_context(ctx)\
._where_calc([('name', '%', 'Goschaeftlic')], active_test=False)
from_clause, where_clause, where_clause_params = query.get_sql()
# the % parameter has to be escaped (%%) for the string replation
self.assertIn("""SELECT id FROM temp_irt_current WHERE name %% %s""",
where_clause)
complete_where = self.env.cr.mogrify(
"SELECT FROM %s WHERE %s" % (from_clause, where_clause),
where_clause_params)
self.assertIn(
"""SELECT id FROM temp_irt_current WHERE name % 'Goschaeftlic'""",
complete_where)
def test_fuzzy_order_generation(self): def test_fuzzy_order_generation(self):
"""Check the generation of the where clause.""" """Check the generation of the where clause."""
order = "similarity(%s.name, 'test') DESC" % self.ResPartner._table order = "similarity(%s.name, 'test') DESC" % self.ResPartner._table

Loading…
Cancel
Save