Skip to main content

PostgreSQL Full Text Search integration with django orm.

Project description

Pgfulltext module of django orm extensions package (collection of third party plugins build in one unified package).

  • Now compatible with python2 and python3 with same code base.

  • Ready for django 1.3, 1.4, 1.5 and 1.6

Introduction

Full Text Searching (or just text search) provides the capability to identify natural-language documents that satisfy a query, and optionally to sort them by relevance to the query. The most common type of search is to find all documents containing given query terms and return them in order of their similarity to the query. Notions of query and similarity are very flexible and depend on the specific application. The simplest search considers query as a set of words and similarity as the frequency of query words in the document. (From postgresql documentation.)

Classes

djorm_pgfulltext.fields.VectorField

An tsvector index field which stores converted text into special format.

djorm_pgfulltext.models.SearchManager

Django manager that contains helper methods for search and re/genereate indexes.

How to use it

To use it, you will need to add a new field. Obviously, this is not mandatory, as it can specify which fields you want to search at the time of calling the search. Keep in mind that you should put the corresponding indices for the fields to be used.

from djorm_pgfulltext.models import SearchManager
from djorm_pgfulltext.fields import VectorField
from django.db import models

class Page(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()

    search_index = VectorField()

    objects = SearchManager(
        fields = ('name', 'description'),
        config = 'pg_catalog.english', # this is default
        search_field = 'search_index', # this is default
        auto_update_search_field = True
    )

The manager automatically injected update_search_field method to the model instance. Also, not to override the save method, you can pass the parameter auto_update_search_field = True, so the index field is updated automatically by calling the save method.

Usage examples:

  • The config parameter is optional and defaults to ‘pg_catalog.english’.

  • The fields parameter is optional. If a list of tuples, you can specify the ranking of each field, if it is None, it gets ‘D’ as the default.

  • It can also be a simple list of fields, and the ranking will be selected by default. If the field is empty, the index was applied to all fields CharField and TextField.

To search, use the search method of the manager. The current implementation, by default uses unaccent extension for ignore the accents. Also, the searches are case insensitive.

>>> Page.objects.search("documentation & about")
[<Page: Page: Home page>]
>>> Page.objects.search("about | documentation | django | home", raw=True)
[<Page: Page: Home page>, <Page: Page: About>, <Page: Page: Navigation>]

FTS extension by default uses plainto_tsquery instead of to_tosquery, for this reason the use of raw parameter.

General notes:

You must ensure you have installed the extension unaccent:

CREATE EXTENSION unaccent;
ALTER FUNCTION unaccent(text) IMMUTABLE;

You can install this extension on template1 database for make this extension automatically available for all new created databases.

Changelog

0.9.2

  • Django 1.7 lookups support.

0.9

  • Fix django 1.6 compatibility (transaction management).

Bitdeli badge

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djorm-ext-pgfulltext-0.9.2.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

djorm_ext_pgfulltext-0.9.3-py2.py3-none-any.whl (17.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file djorm-ext-pgfulltext-0.9.2.tar.gz.

File metadata

File hashes

Hashes for djorm-ext-pgfulltext-0.9.2.tar.gz
Algorithm Hash digest
SHA256 e3195eceedb9ce2c45c6a693eb9d456e775b2a59f586e9ca73a30973cb45ad8e
MD5 efc1a9bedc07da330b08b9313de01432
BLAKE2b-256 fbf81e1f99dcbe0bc91205644074e4d10074a46c32e41c3c91ed3d5539a4962a

See more details on using hashes here.

Provenance

File details

Details for the file djorm_ext_pgfulltext-0.9.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for djorm_ext_pgfulltext-0.9.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cdf5057defdb0b7a3d74d48091b3019a57e0ca199421c36fbcdeac81694ddb64
MD5 0833d30063c1c86b6c0ca41364695ea2
BLAKE2b-256 a8c991bd36f2b3f594d51339273d8c02879f39de185bafe68470953b2e93e741

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page