Skip to main content

A library for indexing Django models with Elasicsearch, OpenSearch or database and searching them with the Django ORM.

Project description

Django ModelSearch

Build Status License Version Documentation

Django ModelSearch allows you to index Django models and search them using the ORM!

It supports PostgreSQL FTS, SQLite FTS5, Elasticsearch (7.x, 8.x, and 9.x), and OpenSearch (1.x, 2.x, and 3.x).

Features:

  • Index models in Elasticsearch and OpenSearch and query with the Django ORM
  • Reuse existing QuerySets for search, works with Django paginators and django-filter
  • Also supports PostgreSQL FTS and SQLite FTS5
  • Autocomplete
  • Faceting
  • Per-field boosting
  • Fuzzy Search
  • Phrase search
  • Structured queries

This has been built into Wagtail CMS since 2014 and extracted into a separate package in March 2025.

Installation

Install with PIP, then add to INSTALLED_APPS in your Django settings:

pip install modelsearch
# settings.py

INSTALLED_APPS = [
    ...
    "modelsearch
    ...
]

By default, Django ModelSearch will index into the database configured in DATABASES["default"] and use PostgreSQL FTS or SQLite FTS, if available.

You can change the indexing configuration, or add additional backends with the MODALSEARCH_BACKENDS setting. For example, to configure Elasticsearch:

# settings.py

MODELSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'modelsearch.backends.elasticsearch8',
        'URLS': ['https://localhost:9200'],
        'INDEX_PREFIX': 'modelsearch_',
        'TIMEOUT': 5,
        'OPTIONS': {},
        'INDEX_SETTINGS': {},
    }
}

Indexing

To index a model, add modelsearch.index.Indexed to the model class and define some search_fields:

from modelsearch import index
from modelsearch.queryset import SearchableQuerySetMixin

class BookQuerySet(models.QuerySet, SearchableQuerySetMixin):
    pass

class Book(index.Indexed, models.Model):
    title = models.CharField(max_length=255)
    genre = models.CharField(max_length=255, choices=GENRE_CHOICES)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    published_date = models.DateTimeField()

    objects = BookQuerySet.as_manager()

    search_fields = [
        index.SearchField('title', boost=10),
        index.AutocompleteField('title', boost=10),
        index.SearchField('get_genre_display'),

        index.FilterField('genre'),
        index.FilterField('author'),
        index.FilterField('published_date'),
    ]

Then run the rebuild_index management command to build the search index.

Searching

You can search models using the .search() QuerySet method (added by SearchableQuerySetMixin). For example:

>>> Book.objects.filter(author=roald_dahl).search("chocolate factory")
[<Book: Charlie and the chocolate factory>]

.search() can be used in conjunction with most other QuerySet Methods like .filter(), .exclude() or .order_by(). When using Elasticsearch, these are automatically converted to the same Elasticsearch Query, so any fields used here must be indexed with index.FilterField so they are added to the Elasticsearch index.

Autocomplete

To autocomplete a partial search query, use the .autocomplete() method. For example:

>>> Book.objects.filter(author=roald_dahl).search("choco")
[<Book: Charlie and the chocolate factory>]

Note that fields used in autocomplete need to also be indexed as an AutocompleteField as autocompletable fields need to be indexed differently.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modelsearch-1.0rc3.tar.gz (73.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modelsearch-1.0rc3-py3-none-any.whl (89.9 kB view details)

Uploaded Python 3

File details

Details for the file modelsearch-1.0rc3.tar.gz.

File metadata

  • Download URL: modelsearch-1.0rc3.tar.gz
  • Upload date:
  • Size: 73.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.3

File hashes

Hashes for modelsearch-1.0rc3.tar.gz
Algorithm Hash digest
SHA256 ef5c1838501376e6d163e971d4c7427dd6f6916d0f601972d92f5e3b634ce24c
MD5 2728d48643973b2b68688a4d7c592c26
BLAKE2b-256 653f6335e768b1b960179bea4f4b2c8f8c15e10190888925f28785b956a12224

See more details on using hashes here.

File details

Details for the file modelsearch-1.0rc3-py3-none-any.whl.

File metadata

File hashes

Hashes for modelsearch-1.0rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 d7adfa2d2cdec81045ad6c10c497d8bf266d986bb2a88c06d3b6d16061e6902a
MD5 5577b3b1657327b3f2beab2bae1775de
BLAKE2b-256 cd196cf11f0cb2f46d21e73eebe172e26fc6fcd0b4372236e8f6fb65d8205564

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page