Skip to content

Commit

Permalink
Merge PR #2310 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Mar 14, 2022
2 parents bb0690b + a7224f1 commit 151cab3
Show file tree
Hide file tree
Showing 92 changed files with 9,690 additions and 0 deletions.
143 changes: 143 additions & 0 deletions base_search_fuzzy/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
============
Fuzzy Search
============

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/15.0/base_search_fuzzy
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-15-0/server-tools-15-0-base_search_fuzzy
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/15.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

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 anything else. It provides
only the possibility to perform the fuzzy search for external addons.

**Table of contents**

.. contents::
:local:

Installation
============

#. 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 ``SUPERUSER`` right (this allows the odoo module to install the
extension to the database).

Configuration
=============

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
=====

#. 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)])``

#. In this example the function will return positive result for `John Miller`
or `John Mill`.

#. 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 statement:

``self.env.cr.execute("SELECT set_limit(0.2);")``

For further questions read the Documentation of the
`pg_trgm <https://www.postgresql.org/docs/current/static/pgtrgm.html>`_ module.


Usage with demo data
====================

There are some demo data that allow you to test functionally this module
if you are in a **demo** database. The steps are the following:

#. Go to *Contacts* and type the text **Jon Smith** or **Smith John** in
the search box and select **Search Display Name for: ...**
#. You will see two contacts, and they are the ones with display names
**John Smith** and **John Smizz**.

Known issues / Roadmap
======================

* Modify the general search parts (e.g. in tree view or many2one fields)
* Add better `order by` handling
* This module will not be necessary from version 16 (`[IMP] Better handling of indexes #83015 <https://github.com/odoo/odoo/pull/83015>`_)

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_search_fuzzy%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* bloopark systems GmbH & Co. KG
* ForgeFlow
* Serpent CS

Contributors
~~~~~~~~~~~~

* Christoph Giesel <https://github.com/christophlsa>
* Jordi Ballester <[email protected]>
* Serpent Consulting Services Pvt. Ltd. <[email protected]>
* Dave Lasley <[email protected]>

* `Tecnativa <https://www.tecnativa.com>`_:
* Vicent Cubells
* Ernesto Tejeda
* [email protected] 2020 NextERP SRL.

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/15.0/base_search_fuzzy>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
6 changes: 6 additions & 0 deletions base_search_fuzzy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright 2016 ForgeFlow S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# Copyright 2020 NextERP SRL.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from . import models
from .hooks import post_load
20 changes: 20 additions & 0 deletions base_search_fuzzy/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2016 ForgeFlow S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Fuzzy Search",
"summary": "Fuzzy search with the PostgreSQL trigram extension",
"category": "Uncategorized",
"version": "15.0.1.0.0",
"website": "https://github.com/OCA/server-tools",
"author": "bloopark systems GmbH & Co. KG, "
"ForgeFlow, "
"Serpent CS, "
"Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["base"],
"data": ["views/trgm_index.xml", "security/ir.model.access.csv"],
"demo": ["demo/res_partner_demo.xml", "demo/TrgmIndex_demo.xml"],
"installable": True,
"post_load": "post_load",
}
7 changes: 7 additions & 0 deletions base_search_fuzzy/demo/TrgmIndex_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="trgm_index_gin_partner_name" model="trgm.index">
<field name="field_id" ref="base.field_res_partner__name" />
<field name="index_type">gin</field>
</record>
</odoo>
21 changes: 21 additions & 0 deletions base_search_fuzzy/demo/res_partner_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="res_partner_John_Smith" model="res.partner">
<field name="name">John Smith</field>
</record>
<record id="res_partner_John_Smizz" model="res.partner">
<field name="name">John Smizz</field>
</record>
<record id="res_partner_Linus_Torvalds" model="res.partner">
<field name="name">Linus Torvalds</field>
</record>
<record id="view_res_partner_filter" model="ir.ui.view">
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_res_partner_filter" />
<field name="arch" type="xml">
<field name="name" position="after">
<field name="display_name" operator="%" />
</field>
</field>
</record>
</odoo>
45 changes: 45 additions & 0 deletions base_search_fuzzy/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2016 Eficent Business and IT Consulting Services S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# Copyright 2017 LasLabs Inc.
# Copyright 2021 Tecnativa - Jairo Llopis
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from functools import wraps

from odoo.osv import expression


def patch_leaf_trgm(original):
@wraps(original)
def _wrapper(self, leaf, model, alias):
left, operator, right = leaf
# No overload for other operators...
if operator != "%":
# Except translation "inselect" queries
if operator == "inselect":
right = (right[0].replace(" % ", " %% "), right[1])
leaf = (left, operator, right)
return original(self, leaf, model, alias)
# The field must exist
if left not in model._fields:
raise ValueError(
"Invalid field {!r} in domain term {!r}".format(left, leaf)
)
# Generate correct WHERE clause part
query = '("{}"."{}" %% {})'.format(
alias,
left,
model._fields[left].column_format,
)
params = [right]
return query, params

return _wrapper


def post_load():
"""Patch expression generators to enable the fuzzy search operator."""
expression.TERM_OPERATORS += ("%",)
expression.expression._expression__leaf_to_sql = patch_leaf_trgm(
expression.expression._expression__leaf_to_sql
)
121 changes: 121 additions & 0 deletions base_search_fuzzy/i18n/am.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_search_fuzzy
#
# Translators:
# OCA Transbot <[email protected]>, 2017
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 10.0c\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-01-14 04:21+0000\n"
"PO-Revision-Date: 2017-01-14 04:21+0000\n"
"Last-Translator: OCA Transbot <[email protected]>, 2017\n"
"Language-Team: Amharic (https://www.transifex.com/oca/teams/23907/am/)\n"
"Language: am\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#. module: base_search_fuzzy
#: model:ir.model.fields,help:base_search_fuzzy.field_trgm_index__index_type
msgid ""
"Cite from PostgreSQL documentation: GIN indexes are the preferred text "
"search index type.See: https://www.postgresql.org/docs/current/textsearch-"
"indexes.html"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__create_uid
msgid "Created by"
msgstr "Creado por"

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__create_date
msgid "Created on"
msgstr "Creado en"

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__display_name
msgid "Display Name"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__field_id
msgid "Field"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields.selection,name:base_search_fuzzy.selection__trgm_index__index_type__gin
msgid "GIN"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields.selection,name:base_search_fuzzy.selection__trgm_index__index_type__gist
msgid "GiST"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__id
msgid "ID"
msgstr "ID"

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__index_name
msgid "Index Name"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__index_type
msgid "Index Type"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index____last_update
msgid "Last Modified on"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__write_uid
msgid "Last Updated by"
msgstr "Última actualización por"

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__write_date
msgid "Last Updated on"
msgstr "Última actualización en"

#. module: base_search_fuzzy
#: model:ir.model.fields,field_description:base_search_fuzzy.field_trgm_index__smart_search
msgid "Smart Search"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,help:base_search_fuzzy.field_trgm_index__index_name
msgid ""
"The index name is automatically generated like fieldname_indextype_idx. If "
"the index already exists and the index is located in the same table then "
"this index is reused. If the index is located in another table then a number "
"is added at the end of the index name."
msgstr ""

#. module: base_search_fuzzy
#: code:addons/base_search_fuzzy/models/trgm_index.py:0
#, python-format
msgid "The pg_trgm extension does not exists or cannot be installed."
msgstr ""

#. module: base_search_fuzzy
#: model:ir.actions.act_window,name:base_search_fuzzy.trgm_index_action
#: model:ir.model,name:base_search_fuzzy.model_trgm_index
#: model:ir.ui.menu,name:base_search_fuzzy.trgm_index_menu
#: model_terms:ir.ui.view,arch_db:base_search_fuzzy.trgm_index_view_form
#: model_terms:ir.ui.view,arch_db:base_search_fuzzy.trgm_index_view_tree
msgid "Trigram Index"
msgstr ""

#. module: base_search_fuzzy
#: model:ir.model.fields,help:base_search_fuzzy.field_trgm_index__field_id
msgid "You can either select a field of type \"text\" or \"char\"."
msgstr ""
Loading

0 comments on commit 151cab3

Please sign in to comment.