Skip to content

New addon: Fuzzy Search with PostgreSQL pg_trgm extension - #462

Merged
dreispt merged 9 commits into
OCA:8.0from
blooparksystems:8.0-base_trgm_search
Aug 10, 2016
Merged

New addon: Fuzzy Search with PostgreSQL pg_trgm extension#462
dreispt merged 9 commits into
OCA:8.0from
blooparksystems:8.0-base_trgm_search

Conversation

@christophlsa

@christophlsa christophlsa commented Jun 22, 2016

Copy link
Copy Markdown
Member

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. This is a first basic implementation which should be extended later.

Requirements

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_trgmextension to your database or you have to give your postgresql user the superadmin right (then the extension will be installed by this module).

Explanation

If you installed the odoo module you can define in Settings -> Database Structure -> Trigam Index the indexes. There the index name will automatically created.

For example you can create an index for the name field of res.partner. Then in a search you can use self.env['res.partner'].search([('name', '%', 'Jon Miller)]). In this Example it can find existing names like 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: 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 "similarity(%s.name, 'John Mil') DESC" % self.env['res.partner']._table.

For further questions read the Documentation of the pg_trgm module.

Tasks/Goals

For this PR:

  • Set permissions/groups
  • Add possibility to order by similarity
  • Add Readme etc
  • German Translations
  • Add unittests

Later:

  • Modify the general search parts (e.g. in tree view or many2one fields)

Comment thread base_trgm_search/models/trgm_index.py Outdated
def patch_generate_order_by(method):
def decorate_generate_order_by(self, order_spec, query):
if order_spec and order_spec.startswith('similarity('):
from pprint import pprint

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove pprint lines

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I think I committed to fast ^^

@dreispt dreispt added this to the 8.0 milestone Jun 22, 2016
@dreispt

dreispt commented Jun 22, 2016

Copy link
Copy Markdown
Member

The initial approach looks good.
Take care with the technique used to patch the core, it can be tricky, so unit tests will be your friend.

@christophlsa christophlsa changed the title [WIP] New addon: PostgreSQL Trigram Search New addon: Fuzzy Search with PostgreSQL pg_trgm extension Jun 24, 2016
@christophlsa

Copy link
Copy Markdown
Member Author
  • Renamed module base_trgm_search to base_search_fuzzy,
  • added Unit tests
  • added german translations
  • added access permissions
    moved the monkey patching to method _register_hook of ir.model
  • fixed _auto_init (wrong parameter)
  • added README
  • cleaned up some parts

@dreispt

dreispt commented Jun 24, 2016

Copy link
Copy Markdown
Member

Is it ready for review?

@christophlsa

Copy link
Copy Markdown
Member Author

for me yes :)

Comment thread base_search_fuzzy/README.rst Outdated

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
this module doesn't change the backend search or something else. It provides

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/something/anything

@elicoidal

Copy link
Copy Markdown

Thanks for the contribution!

@moylop260

moylop260 commented Jun 24, 2016

Copy link
Copy Markdown
Contributor

Please, Could you review our beta checks https://travis-ci.org/OCA/server-tools/jobs/139978373#L424-L430?

************* Module base_search_fuzzy.models.trgm_index
base_search_fuzzy/models/trgm_index.py:122: [C8107(translation-required), TrgmIndex.create_index] String parameter of raise "UserError" requires translation. Use _('The pg_trgm extension does not exists or cannot be installed.')

Ignore

************* Module base_search_fuzzy.models.ir_model
base_search_fuzzy/models/ir_model.py:24: [W0402(deprecated-module), ] Uses of a deprecated module 'openerp.osv'
************* Module base_search_fuzzy.tests.test_query_generation
base_search_fuzzy/tests/test_query_generation.py:3: [W0402(deprecated-module), ] Uses of a deprecated module 'openerp.osv'

@StefanRijnhart

Copy link
Copy Markdown
Member

@moylop260 FYI two of those are the dreaded false positive for imports from openerp.osv again. OCA/maintainer-quality-tools#306. What is the recommended solution for that?

@moylop260

Copy link
Copy Markdown
Contributor

@StefanRijnhart
You are right I'll review this check

@moylop260

Copy link
Copy Markdown
Contributor

👍
(Just add _translate method in raises)
Good job! thanks

@christophlsa

Copy link
Copy Markdown
Member Author

Thanks for the comments. I updated all. :)

Christoph Giesel added 4 commits July 19, 2016 10:48
…t tests, added translations, added access permissions, moved the monkey patching to method _register_hook of ir.model and fixed _auto_init, added README, cleaned up some aprts
@christophlsa

Copy link
Copy Markdown
Member Author

I added a fix to check if the functions which I overwrite are already decorated by the new decorator. We had problems that these functions got this decorator much times (in particular in the cron worker) and a maximum recursion depth error was thrown.

I also rebased the branch.

@gurneyalex

Copy link
Copy Markdown
Member

travis red build should be fixed by #507

Comment thread base_search_fuzzy/README.rst Outdated
#. 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/SUEPRUSER/SUPERUSER/

@gurneyalex

Copy link
Copy Markdown
Member

one small typo, but 👍

@dreispt

dreispt commented Aug 10, 2016

Copy link
Copy Markdown
Member

@dreispt

dreispt commented Aug 10, 2016

Copy link
Copy Markdown
Member

TravisCI is red 🚧

@christophlsa

Copy link
Copy Markdown
Member Author

After rerunning the checks (and merging the Readme fix) all are green.

@dreispt
dreispt merged commit 9d1099e into OCA:8.0 Aug 10, 2016
SiesslPhillip pushed a commit to grueneerde/OCA-server-tools that referenced this pull request Nov 20, 2024
Syncing from upstream OCA/server-tools (9.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants