Coder Social home page Coder Social logo

django-fulltext-search's Introduction

Django Fulltext Search

https://badge.fury.io/py/django-fulltext-search.png https://travis-ci.org/confirm/django-fulltext-search.png?branch=master https://pypip.in/d/django-fulltext-search/badge.png

This repository provides a Python library for Django which supports native full-text search capabilities for MySQL and MariaDB .

Django already supports boolean full-text searches . However, with the integrated full-text search support you can only search one single column and IMHO there's currently no way to search over multiple columns.

Add fulltext index

Before you can run any full-text search you've to create at least one full-text index . Unfortunately Djangos' native migrations doesn't support full-text indexes and the manual says:

> Note this is only available in MySQL and requires direct manipulation of the database to add the full-text index.

However, you can easily create your own migration by starting with an empty one:

./manage.py makemigrations --empty customer

Open the created migration file and add a RunSQL command to the operations list:

operations = [
    migrations.RunSQL(
        ('CREATE FULLTEXT INDEX customer_fulltext_index ON customer_customer (first_name, last_name)',),
        ('DROP INDEX customer_fulltext_index on customer_customer',)
    )
]

Then migrate the database:

./manage.py migrate

Update your model

If you want to add full-text search capabilities to your existing model you've to create a new SearchManager() instance and define it as your models' objects attribute:

from django_fulltext_search import SearchManager

class Customer(models.Model):
    ''' Customer model. '''

    # Enable full-text search support for first_name and last_name fields.
    objects    = SearchManager(['first_name', 'last_name'])

    first_name = models.CharField(max_length=32)
    last_name  = models.CharField(max_length=32)
    # more fields...

As you can see, you can create the SearchManager() with a list of by default searchable fields. This means you don't have to bother about the field names later when you search your model. However, if you don't want to specify default fields you can also create the SearchManager() object without any arguments:

objects = SearchManager()

Search

The library currently supports boolean full-text seaches by default if you use one of the operators (+ - > < ( ) * ") in your search query. Please note that the at-operator (@) will not enable the boolean search mode, which means you can also search for mail addresses, as long as you don't include any other operator in your search query.

To search your model use the new search() method of the models' queryset:

Customer.objects.search('John*')

This only works if you've defined default fields in the constructor of the SearchManager(). If you haven't set them or you want to search alternative fields you have to define a list of fields:

Customer.objects.search('John*', ['first_name', 'last_name'])

The search method can also be called with keyword arguments:

  • query: The search query itself
  • fields: A list of fields
    • _optional_
    • _default are the fields defined in the SearchManager()
  • mode: Sets the search mode
    • _optional_
    • _default is AUTO which will enable BOOLEAN mode when a boolean operator is found in the fseaquery
    • _can be set to a valid search mode, e.g. BOOLEAN or NATURAL LANGUAGE

IMPORTANT: Please remember you've to create a full-text index for the defined fields before you can search them.

Related models

If you want to search a related models' field (e.g. models.ForeignKey) you can also use the __ syntax:

SearchManager(['customer__first_name', 'customer__last_name']

django-fulltext-search's People

Contributors

ad-m avatar domibarton avatar ppolewicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

django-fulltext-search's Issues

License

You might want to add a license, just to make sure it can be used properly. You can have a look here for some help =)

objects.search error

posts.objects.search('test',('gtitle','gdesc')).order_by('-id')

error
TypeError at /req_proc/reqdata
search() takes exactly 2 arguments (3 given)

Request Method: GET
1.10.7
TypeError
search() takes exactly 2 arguments (3 given)
./ndaps/views/req_proc.py in reqdata, line 59
/usr/bin/uwsgi-core
2.7.12
['.', '', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Tue, 10 Oct 2017 15:14:53 +0900

posts.objects.search('test')

error
TypeError at /req_proc/reqdata
get_field() got an unexpected keyword argument 'many_to_many'

Request Method: GET
1.10.7
TypeError
get_field() got an unexpected keyword argument 'many_to_many'
./ndaps/fulltext.py in search, line 73
/usr/bin/uwsgi-core
2.7.12
['.', '', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Tue, 10 Oct 2017 15:20:50 +0900

I got an error.

My Django version is 2.0.5 and python is 3.6, I got an error like this.
Traceback (most recent call last): File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner response = get_response(request) File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib64/python3.6/site-packages/django/views/generic/base.py", line 69, in view return self.dispatch(request, *args, **kwargs) File "/usr/local/lib64/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper return bound_func(*args, **kwargs) File "/data/opsplatform/public/views.py", line 16, in inner return func(reqeust, *args, **kwargs) File "/usr/local/lib64/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func return func.__get__(self, type(self))(*args2, **kwargs2) File "/usr/local/lib64/python3.6/site-packages/django/views/generic/base.py", line 89, in dispatch return handler(request, *args, **kwargs) File "/data/opsplatform/cmdb/views.py", line 50, in get AssetSInfo.objects.search('12312') File "/usr/local/lib/python3.6/site-packages/django_fulltext_search-0.2.0-py3.6.egg/django_fulltext_search.py", line 155, in search return self.get_query_set().search(query, **kwargs) File "/usr/local/lib/python3.6/site-packages/django_fulltext_search-0.2.0-py3.6.egg/django_fulltext_search.py", line 75, in search column = meta.get_field(field, many_to_many=False).column TypeError: get_field() got an unexpected keyword argument 'many_to_many'
My python code is this:
AssetSInfo.objects.search('12312')

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.