Skip to main content

Search Django models with Elasticsearch

Project description

Django ModelSearch

Django ModelSearch allows you index and search Django models using Elasticsearch.

This was built in to Wagtail CMS since 2014 and this package now allows it to be used in a standalone way on all other Django projects!

Installation

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

pip install django-modelsearch
# settings.py

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

Configure a backend in Django settings. For example, to configure Elasticsearch:

# settings.py

MODELSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'wagtail.search.backends.elasticsearch8',
        'URLS': ['https://localhost:9200'],
        'INDEX': 'wagtail',
        'TIMEOUT': 5,
        'OPTIONS': {},
        'INDEX_SETTINGS': {},
    }
}

Indexing content

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

from modelsearch import index

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()

    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 content

Searching is done using a new .search() method that is added to the querysets of indexed models.

You can use Django's .filter(), .exclude(), and .order_by() with the search method and these will be added as filters in the Elasticsearch query. Remember to index any fields you want to filter/order on with index.FilterField first!

For example:

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

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.0a1.tar.gz (75.0 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.0a1-py3-none-any.whl (90.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modelsearch-1.0a1.tar.gz
  • Upload date:
  • Size: 75.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for modelsearch-1.0a1.tar.gz
Algorithm Hash digest
SHA256 f9f6079eb63269a766fd514eb4338f08b951a73fcf099227d2af0d50af078417
MD5 74255909f2a0b46d15d1f0f2174ac796
BLAKE2b-256 eefc2ea05a3df2bef5dc379447642d1e0f1c0b83f94f52f3a58602df280954e3

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for modelsearch-1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 86f13ceefdddc798ea01b74cafe2d7b801158bc52703b704e608ff966a8aeb20
MD5 ab26b2fd1d1a4f3d110be8d4afec3613
BLAKE2b-256 061cad6ccfe7feb7d220a3c037e5dd44f7972612f1019dc939a9965eaf015ca4

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