A Django Haystack backend that uses PostgreSQL's built-in full-text search. No external search service required.
- Python >= 3.12
- Django >= 5.0
- django-haystack >= 2.8.0
- PostgreSQL
pip install postgres-fts-backendAdd to INSTALLED_APPS:
INSTALLED_APPS = [
"django.contrib.postgres",
"haystack",
"postgres_fts_backend",
# ...
]Set a migration module so the generated search index migrations live in your project rather than inside the installed package:
MIGRATION_MODULES = {
"postgres_fts_backend": "myapp.search_migrations",
}Configure Haystack:
HAYSTACK_CONNECTIONS = {
"default": {
"ENGINE": "postgres_fts_backend.PostgresFTSEngine",
},
}To use a search configuration other than "english":
HAYSTACK_CONNECTIONS = {
"default": {
"ENGINE": "postgres_fts_backend.PostgresFTSEngine",
"SEARCH_CONFIG": "spanish",
},
}python manage.py build_postgres_schema
python manage.py migrate postgres_fts_backendRun these two commands again whenever you change a SearchIndex definition.
Fuzzy queries use PostgreSQL's trigram similarity matching (pg_trgm):
results = SearchQuerySet().filter(author__fuzzy="Janee")The similarity threshold is controlled by PostgreSQL's
pg_trgm.similarity_threshold setting (default 0.3). To adjust it:
ALTER DATABASE mydb SET pg_trgm.similarity_threshold = 0.5;PostgreSQL FTS doesn't provide any facilities for this. It could be done, but I just need to think more about it.