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, MySQL FTS, MariaDB FTS, Elasticsearch (7.x, 8.x, and 9.x), and OpenSearch (2.x and 3.x).

Features:

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, MySQL FTS, MariaDB FTS or SQLite FTS, if available.

If you are using PostgreSQL, you must additionally add django.contrib.postgres to your INSTALLED_APPS setting.

You can change the indexing configuration, or add additional backends with the MODELSEARCH_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


# This mixin adds a .search() method to the models QuerySet
class SongQuerySet(SearchableQuerySetMixin, models.QuerySet):
    pass


# Create a model that inherits from Indexed
class Song(index.Indexed, models.Model):
    name = models.TextField()
    lyrics = models.TextField()
    release_date = models.DateField()
    artist = models.ForeignKey(Artist, related_name='songs')

    objects = SongQuerySet.as_manager()

    # Define a list of fields to index
    search_fields = [
        # Index text fields for full-text search
        # Boost the important fields
        index.SearchField('name', boost=2.0),
        index.SearchField('lyrics'),

        # Index fields that for filtering
        # These get inserted into Elasticsearch for fast filtering
        index.FilterField('release_date'),
        index.FilterField('artist'),

        # Pull in content from related models too
        index.RelatedFields('artist', [
           index.SearchField('name'),
        ]),
    ]

Then run the django-admin rebuild_modelsearch_index to create the indexes, mappings and insert the data. Signals are then used to keep the index in sync with the database.

Searching

Search by calling .search() on the QuerySet!

Song.objects.search("Flying Whales")

Searches also work when reversing ForeignKeys:

opeth.songs.search("Harvest")

You can use Django's .filter(), .exclude() and .order_by() with search too:

Song.objects.filter(release_date__year__lt=1971).search("Iron Man")

The filters are rewitten into the Elasticsearch query to make it run fast with a lot of data.

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.2.1.tar.gz (91.3 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.2.1-py3-none-any.whl (111.5 kB view details)

Uploaded Python 3

File details

Details for the file modelsearch-1.2.1.tar.gz.

File metadata

  • Download URL: modelsearch-1.2.1.tar.gz
  • Upload date:
  • Size: 91.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for modelsearch-1.2.1.tar.gz
Algorithm Hash digest
SHA256 8732cb84f424a592ea9fa44763650285af5ecf6475aa742125e7e368083d6505
MD5 0e810e245a371ebd420c96e88e1b9bfc
BLAKE2b-256 deffab9bea5ee016a2b78a1e4e9aafdaafaef1677a3887aee1f0a51013c6b9fd

See more details on using hashes here.

File details

Details for the file modelsearch-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: modelsearch-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 111.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for modelsearch-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6576cb66162b8b1f80806116c2530b4b6409c50a55727735e3175556409c0355
MD5 96465d3d20d2889d50a3b8dde35dec32
BLAKE2b-256 c4db23e8b874e1b68d2f76e127733a325468a0739cb204d0c9d7bad471ef8c57

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