Search in Django ORM: From Basic Text Lookups to PostgreSQL Full‑Text Search

This article explores the different search techniques available in Django ORM—from simple text lookups using contains and icontains, to advanced database‑specific search features such as unaccent and trigram similarity in PostgreSQL. It also explains when traditional string matching becomes insufficient and how document‑based search engines or PostgreSQL’s built‑in full‑text search can be used for large‑scale and multilingual search needs.

Search, contains, icontainsunaccent, trigram_similar, full text searchSearchVector, PostgreSQL, Django ORM

~2 min read • Updated Mar 10, 2026

1. Introduction

Searching is one of the most common tasks in web applications. It can range from simple filtering to complex, multilingual, weighted search with ranking and highlighting. Django provides several tools to support different levels of search complexity.

2. Basic Textual Queries

The simplest form of search uses text lookups:


Author.objects.filter(name__contains="Terry")

This approach is fragile because it requires the user to know an exact substring. A slightly better option is:


name__icontains

But even this is limited, especially for multilingual or accented text.

3. Advanced Database Comparison Functions

If you use PostgreSQL, Django provides powerful search helpers through django.contrib.postgres.

Unaccented search


Author.objects.filter(name__unaccent__icontains="Helen")

This matches accented variations like “Hélène”.

Trigram similarity search

Useful for approximate matching:


Author.objects.filter(name__unaccent__lower__trigram_similar="Hélène")

Trigram search compares sequences of three letters. It works well for short strings but may miss longer names due to lower similarity scores.

4. Document‑Based Search

When dealing with large text fields, simple string matching becomes ineffective. Document‑based search engines analyze text semantically and offer features like:

  • Ignoring stop words (“a”, “the”, “and”)
  • Stemming (“pony” → “ponies”)
  • Weighted fields (e.g., title more important than body)
  • Multilingual support

Popular engines include:

  • Elasticsearch
  • Solr

To use them with Django models, you must index your data as documents and map search results back to database IDs.

5. PostgreSQL Full‑Text Search

PostgreSQL includes a built‑in full‑text search engine. While not as advanced as Elasticsearch, it integrates seamlessly with relational queries.

Simple full‑text search:


Entry.objects.filter(body_text__search="cheese")

Searching across multiple fields and related models:


Entry.objects.annotate(
    search=SearchVector("blog__tagline", "body_text"),
).filter(search="cheese")

This allows combining relational filtering with full‑text search in a single query.

6. Choosing the Right Search Method

MethodBest For
contains / icontainsSimple, small‑scale searches
unaccent / trigramNames, short strings, approximate matching
PostgreSQL full‑text searchMedium‑sized text, fast and integrated search
Elasticsearch / SolrLarge documents, advanced ranking, multilingual search

Conclusion

Django offers a wide range of search tools—from basic substring matching to advanced full‑text search. The right choice depends on your dataset, language requirements, and performance needs. PostgreSQL provides powerful built‑in search capabilities, while external engines like Elasticsearch are ideal for large‑scale, feature‑rich search applications.

Written & researched by Dr. Shahin Siami