django-full-text-search provides a simple, database-agnostic full text search for Django models. By extending Django's model manager functionality, searching and indexing of documents can be added to any Django model. django-full-text-search also provides pluggable stemming implementations and includes (and defaults to) an implementation of the Porter stemming algorithm.
Once installed, searching documents is as easy as:
- Registering a manager for the model you would like to make searchable:
from ftsearch.managers import SearchableManager
- class MyModel(models.Model):
...
objects = SearchableManager()
2. Define a get_text_only()
method on the model. This method should return
a single string containing all text to be searched:
class MyModel(models.Model): name = models.CharField() body = models.CharField() ... objects = SearchableManager() def get_text_only(self): return ' '.join(self.name, self.body)
3. Index the documents when they are created or modified by using the manager methods. This only needs to be done once:
MyModel.objects.add_to_index(my_model_instance)
Now search the indexed documents:
MyModel.objects.search('my search terms')
$ sudo python setup.py install
- Add the
ftsearch
app toINSTALLED_APPS
in yoursettings.py
By default django-full-text-search comes with pretty reasonable defaults and doesn't require any additional configuration. However, if you would like to override some defaults, you can use the following configuration options:
SEARCH_WEIGHTS
- A list of 2-value tuples which specify the weights to be used w hen ranking results. if you would like to add application-specific weighting rules, you can do so by overridingSEARCH_WEIGHTS
. By default it is defined as:settings.SEARCH_WEIGHTS = ( (1.0, ftsearch.weights.frequency_score), (1.0, ftsearch.weights.location_score), (1.0, ftsearch.wieghts.distance_score), )
SEARCH_STEMMER
- A custom stemming implementation. It must be a class which has astem()
method.SEARCH_WORD_SPLIT_REGEX
- The regex that is used to split text. By default it isre.compile(r'\W*')
.
django-full-text-search is mostly suitable for small datasets. If you need to index and search millions of documents, it may not be for you.