|
| 1 | +.. image:: https://odoo-community.org/readme-banner-image |
| 2 | + :target: https://odoo-community.org/get-involved?utm_source=readme |
| 3 | + :alt: Odoo Community Association |
| 4 | + |
| 5 | +================ |
| 6 | +Full Text Search |
| 7 | +================ |
| 8 | + |
| 9 | +.. |
| 10 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 11 | + !! This file is generated by oca-gen-addon-readme !! |
| 12 | + !! changes will be overwritten. !! |
| 13 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 14 | + !! source digest: sha256:5374048b8bc825d4893d68c0f697227e4f6f76c1b21971351a5b223910a6cc46 |
| 15 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 16 | +
|
| 17 | +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png |
| 18 | + :target: https://odoo-community.org/page/development-status |
| 19 | + :alt: Beta |
| 20 | +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png |
| 21 | + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html |
| 22 | + :alt: License: AGPL-3 |
| 23 | +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github |
| 24 | + :target: https://github.com/OCA/server-tools/tree/14.0/full_text_search |
| 25 | + :alt: OCA/server-tools |
| 26 | +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png |
| 27 | + :target: https://translation.odoo-community.org/projects/server-tools-14-0/server-tools-14-0-full_text_search |
| 28 | + :alt: Translate me on Weblate |
| 29 | +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png |
| 30 | + :target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=14.0 |
| 31 | + :alt: Try me on Runboat |
| 32 | + |
| 33 | +|badge1| |badge2| |badge3| |badge4| |badge5| |
| 34 | + |
| 35 | +This module provides a simple way to use full-text search functionality |
| 36 | +in Odoo models. |
| 37 | + |
| 38 | +It is fully based on `PostgreSQL full-text |
| 39 | +search <https://www.postgresql.org/docs/current/textsearch.html>`__, and |
| 40 | +adds a new ``Searchable`` field that represents the TSVector column in |
| 41 | +the database that will store the weighted full-text vector. It also adds |
| 42 | +the ``full_text`` matching operator : ``@@`` to odoo domains. |
| 43 | + |
| 44 | +To add this functionality to a model, simply add a ``Searchable`` field |
| 45 | +to the model with the fields you want to search on weighted by their |
| 46 | +importance: ``A``, ``B``, ``C``, ``D`` with ``A`` being the most |
| 47 | +important. |
| 48 | + |
| 49 | +.. code:: python |
| 50 | +
|
| 51 | + from odoo import fields, models |
| 52 | +
|
| 53 | + class YourModel(models.Model): |
| 54 | + _name = 'your.model' |
| 55 | +
|
| 56 | + full_text = fields.Searchable( |
| 57 | + "Full Text", |
| 58 | + fields={ |
| 59 | + "name": "A", |
| 60 | + "description": "B", |
| 61 | + "notes": "C", |
| 62 | + }, |
| 63 | + dictionary="english", |
| 64 | + ) |
| 65 | +
|
| 66 | +And add the ``full_text`` field to the model's search view (first |
| 67 | +position is recommended): |
| 68 | + |
| 69 | +.. code:: xml |
| 70 | +
|
| 71 | + <odoo> |
| 72 | + <record id="view_your_model_search" model="ir.ui.view"> |
| 73 | + <field name="model">your.model</field> |
| 74 | + <field name="inherit_id" ref="base.view_your_model_search" /> |
| 75 | + <field name="arch" type="xml"> |
| 76 | + <field name="name" position="before"> |
| 77 | + <field name="full_text" operator="@@" /> |
| 78 | + </field> |
| 79 | + </field> |
| 80 | + </record> |
| 81 | + </odoo> |
| 82 | +
|
| 83 | +You can also use the ``full_text`` operator in your own code to search |
| 84 | +for records: |
| 85 | + |
| 86 | +.. code:: python |
| 87 | +
|
| 88 | + records = self.env['your.model'].search([('full_text', '@@', 'search query')]) |
| 89 | +
|
| 90 | +The search query uses the PostgreSQL websearch syntax with additional |
| 91 | +prefix matching: |
| 92 | + |
| 93 | +- unquoted text will be ANDed (``&``) |
| 94 | +- quoted text will be searched for with followed by operator (``<->``) |
| 95 | +- text around the OR keyword will be ORed (``|``) |
| 96 | +- a dash ``-`` will be treated as a negation operator (``!``) |
| 97 | +- every term will be treated as a prefix match (``:*``) |
| 98 | + |
| 99 | +The results will be sorted by relevance if no other sort order is |
| 100 | +specified. |
| 101 | + |
| 102 | +By default, the ``full_text`` field is computed at database level using |
| 103 | +a generated field (PostgreSQL 12+). |
| 104 | + |
| 105 | +For more fine grained control, you can also use a compute to generate |
| 106 | +the ``full_text`` field value yourself. |
| 107 | + |
| 108 | +The usage is as follows: |
| 109 | + |
| 110 | +.. code:: python |
| 111 | +
|
| 112 | + from odoo import fields, models |
| 113 | +
|
| 114 | + class YourModel(models.Model): |
| 115 | + _name = 'your.model' |
| 116 | +
|
| 117 | + full_text = fields.Searchable( |
| 118 | + "Full Text", |
| 119 | + fields={ |
| 120 | + "name": "A", |
| 121 | + "description": "B", |
| 122 | + "notes": "C", |
| 123 | + }, |
| 124 | + dictionary="english", |
| 125 | + compute="_compute_full_text", |
| 126 | + store=True, |
| 127 | + ) |
| 128 | +
|
| 129 | + @api.depends('name', 'description', 'notes') |
| 130 | + def _compute_full_text(self): |
| 131 | + for record in self: |
| 132 | + record.full_text = { |
| 133 | + "name": record.name, |
| 134 | + "description": record.description, |
| 135 | + "notes": " ".join(record.relation_ids.mapped('name')), |
| 136 | + } |
| 137 | +
|
| 138 | +**Table of contents** |
| 139 | + |
| 140 | +.. contents:: |
| 141 | + :local: |
| 142 | + |
| 143 | +Bug Tracker |
| 144 | +=========== |
| 145 | + |
| 146 | +Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_. |
| 147 | +In case of trouble, please check there if your issue has already been reported. |
| 148 | +If you spotted it first, help us to smash it by providing a detailed and welcomed |
| 149 | +`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20full_text_search%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. |
| 150 | + |
| 151 | +Do not contact contributors directly about support or help with technical issues. |
| 152 | + |
| 153 | +Credits |
| 154 | +======= |
| 155 | + |
| 156 | +Authors |
| 157 | +------- |
| 158 | + |
| 159 | +* Akretion |
| 160 | + |
| 161 | +Contributors |
| 162 | +------------ |
| 163 | + |
| 164 | +- Florian Mounier florian.mounier@akretion.com |
| 165 | + |
| 166 | +Maintainers |
| 167 | +----------- |
| 168 | + |
| 169 | +This module is maintained by the OCA. |
| 170 | + |
| 171 | +.. image:: https://odoo-community.org/logo.png |
| 172 | + :alt: Odoo Community Association |
| 173 | + :target: https://odoo-community.org |
| 174 | + |
| 175 | +OCA, or the Odoo Community Association, is a nonprofit organization whose |
| 176 | +mission is to support the collaborative development of Odoo features and |
| 177 | +promote its widespread use. |
| 178 | + |
| 179 | +.. |maintainer-paradoxxxzero| image:: https://github.com/paradoxxxzero.png?size=40px |
| 180 | + :target: https://github.com/paradoxxxzero |
| 181 | + :alt: paradoxxxzero |
| 182 | + |
| 183 | +Current `maintainer <https://odoo-community.org/page/maintainer-role>`__: |
| 184 | + |
| 185 | +|maintainer-paradoxxxzero| |
| 186 | + |
| 187 | +This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/14.0/full_text_search>`_ project on GitHub. |
| 188 | + |
| 189 | +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
0 commit comments