Giter Site home page Giter Site logo

poppingtonic / django-pg-fts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dvdmgl/django-pg-fts

0.0 3.0 2.0 82 KB

Implementation PostgeSQL for Full Text Search for Django >= 1.7

Home Page: http://django-pg-fts.readthedocs.org/

License: Other

Python 100.00%

django-pg-fts's Introduction

django-pg-fts

Implementation PostgeSQL for Full Text Search for django 1.7, taking advantage of new features Migrations and Custom Lookups.

Features:

  • FieldLookup's search, isearch, tsquery
  • Ranking support with normalization, and weights using annotations
  • Migrations classes to help create and remove index's, support for 'gin' or 'gist'
  • Migrations classes to help create and remove of trigger's
  • Multiple dictionaries support with trigger and FieldLookup's
  • Support for python 2.7, 3.3 and 3.4

With multiple dictionary support, index and trigger migrations

How it works

For a existing model:

class Article(models.Model):
    title = models.CharField(max_length=255)
    article = models.TextField()

You want to have full text search in fields title and article, and make give weight to title of 'A'.

Import TSVectorField:

from pg_fts.fields import TSVectorField

Add to existing TSVectorField to model and tell the fields that you want to index and the dictionary:

fts = TSVectorField(fields=(('title', 'A'), 'article'), dictionary='english')

Create migrations file:

python manage.py makemigrations article

Open the new created migration and import CreateFTSIndexOperation, CreateFTSTriggerOperation, UpdateVectorOperation:

from pg_fts.migrations import (CreateFTSIndexOperation, CreateFTSTriggerOperation,
                               UpdateVectorOperation)

And add to the end of operations:

# update vector for already existing data
UpdateVectorOperation(
    name='Article',
    field='fts',    
),
# create trigger for automatic insert and update vector
CreateFTSTriggerOperation(
    name='Article',
    field='fts'
),
# create gin index for vector
CreateFTSIndexOperation(
    name='Article',
    field='fts',
    index='gin'
)

Make the changes to your database:

python manage.py migrate article

Now you have a full text search performance with a trigger that checks any changes in the tracked fields and updates vector field.

You can search will match all words:

>>> Article.objects.filter(fts__search='waz up')

Will result in sql something like:

tsvector @@ to_tsquery('english', 'waz & up')

Or isearch will match some words:

>>> Article.objects.filter(fts__isearch='waz up')

Will result in sql something like:

tsvector @@ to_tsquery('english', 'waz | up')

But you can make a raw tsquery:

>>> Article.objects.filter(fts__tsquery='waz & !up')

Will result in sql something like:

tsvector @@ to_tsquery('english', 'waz & !up')

And also rank the results with normalization and order:

>>> from pg_fts.ranks import FTSRank >>> Article.objects.filter( rank=FTSRank(fts_search='waz up', normalization=[1,3])).order_by('-rank')

For multiple dictionaries and more advanced options, check the documentation.

Documentation

Documentation available in Read The Docs django-pg-fts.readthedocs.org

Installation

Clone from GitHub:

git clone git://github.com/dvdmgl/django-pg-fts.git django-pg-fts

You should run the tests:

python runtests.py

Or running tox for py27, py33, py34:

tox

Install using pip from github:

pip install git+https://github.com/dvdmgl/django-pg-fts

Or using setup.py:

python setup.py

django-pg-fts's People

Contributors

dvdmgl avatar askholme avatar

Watchers

James Cloos avatar Brian Muhia avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.