Full-Text Search & Docker Deployment

PostgreSQL full-text search, exposed through django.contrib.postgres.search, lets you search and rank text by relevance far better than a simple substring match — and Docker lets you package that app, along with its database, into containers you can ship and run anywhere.

Learn Full-Text Search & Docker Deployment in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this final lesson you'll build relevance-ranked search with SearchVector, SearchQuery, and SearchRank, add fuzzy matching with TrigramSimilarity and a GIN index, then write a Dockerfile, run gunicorn, set production settings, and orchestrate Postgres with docker-compose.

A query like filter(title__icontains=term) only matches exact substrings and cannot rank results. PostgreSQL full-text search, in django.contrib.postgres.search , normalizes text into lexemes so searching for "running" also finds "run", and lets you order by relevance.

The three core tools are SearchVector (turn columns into a searchable document), SearchQuery (turn the user's terms into a query), and SearchRank (score how well each row matches).

Users misspell things. TrigramSimilarity compares three-character sequences, so a search for "djngo" still matches "django". It needs the Postgres pg_trgm extension. And to keep search fast on large tables, you add a GIN index on the search vector.

To ship the app, you package it in a Dockerfile , serve it with gunicorn , and turn on production settings: DEBUG = False , a real ALLOWED_HOSTS , secrets read from environment variables, and collectstatic to gather static files.

Finally, docker-compose runs the app container alongside a PostgreSQL container so your whole stack starts with one command.

Fill in the blank with the value DEBUG must have in production so error pages don't leak sensitive details.

❌ Static files (CSS/JS) are missing in production

With DEBUG = False , Django stops auto-serving static files, and you never collected them.

✅ Fix: run python manage.py collectstatic and serve STATIC_ROOT with your web server or WhiteNoise.

Your domain is not listed in ALLOWED_HOSTS , which Django enforces once DEBUG is False.

✅ Fix: set ALLOWED_HOSTS (often from an env var) to include your real domain or container host.

❌ function search_query / similarity does not exist

You used full-text or trigram search without enabling the required Postgres extension.

✅ Fix: enable it (e.g. the pg_trgm extension) via a migration with TrigramExtension before using TrigramSimilarity .

Combine the two halves of this lesson: rank search results by relevance, then run a production safety check before "deploying".

Lesson 27 complete — and you've finished the Django course!

You can build relevance-ranked search with SearchVector , SearchQuery , and SearchRank , add fuzzy matching with TrigramSimilarity and a GinIndex , then package your app in a Dockerfile , serve it with gunicorn , harden it with DEBUG=False and ALLOWED_HOSTS , and run it alongside PostgreSQL using docker-compose .

🏆 Congratulations — you've completed the Django course! From your first view to full-text search and a production Docker deployment, you now have the full toolkit to design, build, test, and ship real Django applications. Keep building, and revisit any lesson whenever you need a refresher.

Practice quiz

Which module provides Django's PostgreSQL full-text search tools?

  • django.contrib.postgres.search
  • django.core.search
  • django.contrib.search
  • django.db.fulltext

Answer: django.contrib.postgres.search. SearchVector, SearchQuery, SearchRank, and TrigramSimilarity all live in django.contrib.postgres.search.

What does SearchVector do in a full-text query?

  • It paginates results
  • It escapes SQL injection
  • It turns one or more text fields into a searchable document of normalized lexemes
  • It connects to the database

Answer: It turns one or more text fields into a searchable document of normalized lexemes. SearchVector processes the chosen text columns into a tsvector of normalized lexemes you can search against.

What is SearchQuery used for?

  • Creating database tables
  • Parsing the user's search terms into a query matched against a SearchVector
  • Caching query results
  • Defining a model field

Answer: Parsing the user's search terms into a query matched against a SearchVector. SearchQuery turns the search terms into a normalized query that is matched against the SearchVector.

What does SearchRank let you do?

  • Limit the number of results
  • Index the database
  • Encrypt the query
  • Order matches by how well they match, so the most relevant rise to the top

Answer: Order matches by how well they match, so the most relevant rise to the top. SearchRank scores each match's relevance so you can order results with the best matches first.

Which class enables fuzzy matching that tolerates typos and partial spellings?

  • TrigramSimilarity
  • SearchVector
  • SearchHeadline
  • SearchConfig

Answer: TrigramSimilarity. TrigramSimilarity compares three-character sequences, so it matches misspellings and partial words.

Which index type is recommended to make full-text search fast on large tables?

  • Hash index
  • GIN index
  • No index is needed
  • B-tree index

Answer: GIN index. A GIN index on the search vector dramatically speeds up full-text queries on large tables.

In production, what must DEBUG be set to?

  • 'production'
  • It does not matter
  • True
  • False

Answer: False. DEBUG must be False in production so error pages do not leak sensitive details about your code and settings.

What does the collectstatic command do before deployment?

  • Compiles Python to bytecode
  • Runs database migrations
  • Gathers all static files into STATIC_ROOT so a web server can serve them
  • Starts the development server

Answer: Gathers all static files into STATIC_ROOT so a web server can serve them. collectstatic copies all app and project static files into STATIC_ROOT for serving in production.

What is gunicorn's role in a typical Django deployment?

  • It is a caching backend
  • It is a production WSGI server that runs your Django app instead of runserver
  • It is a database engine
  • It is a template engine

Answer: It is a production WSGI server that runs your Django app instead of runserver. gunicorn is a production-grade WSGI server that serves your app, replacing the development runserver.

What is the purpose of docker-compose in this setup?

  • To define and run multiple containers together, such as the Django app and a PostgreSQL database
  • To minify JavaScript
  • To write database migrations
  • To replace gunicorn

Answer: To define and run multiple containers together, such as the Django app and a PostgreSQL database. docker-compose defines and orchestrates multiple services together, for example the web app and a Postgres database.