Skip to main content

ParadeDB integration for Django

Project description

logo

Bring the full power of ParadeDB into Django.

Build sophisticated, composable search queries effortlessly, and leverage advanced indexing, search, and ranking at blazing speed — all inside PostgreSQL.

📘 See the Documentation

Quick Example

Create a Django model and attach a BM25 index using ParadeDB:

from django.db import models
from django.conf import settings
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.fields.ranges import (
    IntegerRangeField,
    DateRangeField,
)
from paradedb.indexes import Bm25Index, IndexFieldConfig, JSONFieldIndexConfig

class Article(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
    )

    created = models.DateTimeField(auto_now_add=True, blank=True)
    rank = models.IntegerField(default=0)
    published = models.BooleanField(default=True)

    price_range = IntegerRangeField(null=True, blank=True, default=None)
    date_range = DateRangeField(null=True, blank=True, default=None)

    metadata = models.JSONField(default=dict, blank=True)
    tags = ArrayField(
        models.TextField(max_length=255),
        default=list,
        blank=True,
    )

    # ParadeDB primary key (optional)
    paradedb_key_field = "id"

    def __str__(self):
        return self.title

    class Meta:
        indexes = [
            Bm25Index(
                "id",
                "title",
                "description",
                "rank",
                "published",
                "price_range",
                "date_range",
                "created",
                "user",
                "metadata",
                name="article_bm25_idx",
                fields_config=IndexFieldConfig(
                    json_fields=[
                        JSONFieldIndexConfig(
                            "metadata",
                            fast=True,
                        )
                    ]
                ),
            ),
        ]

The Bm25Index enables fast, relevance-ranked full-text search.


Basic Full-Text Search

from paradedb.expressions import Search
from .models import Article

results = Article.objects.filter(
    Search("title", "django search")
)

for article in results:
    print(article.title)
  • Inspect query planner to verify:
results.explain()

JSON SubScript Field Search

Assume stored JSON:

{
  "category": "backend",
  "framework": "django",
  "reading_time": 2
}

Using Django Lookup (__json_op)

Lookup Pattern

field__json_key__operator__json_op=value
Part Meaning
metadata JSONField
framework JSON key
match, phrase, term, match_conjunction ParadeDB operator
json_op Enables JSON expression
"django" search value

Term

results = Article.objects.filter(
    metadata__framework__term__json_op="django"
)

Using Expression API (JsonOp)

from paradedb.expressions import JsonOp

MATCH

results = Article.objects.filter(
    JsonOp("metadata", "framework", "match", value="dj")
)

Combine with Django Filters

from django.db import models

results = Article.objects.filter(
    JsonOp("metadata", "framwork", "match", value="django") &
    models.Q(rank=2) &
    models.Q(published=True)
)

Using V2 API with normal fields and Json, Array fields Querying

operator lookups - match_v2, phrase_v2, match_conjunction, term_v2

Term

from django.db import models
from paradedb.cast import ValueCast

# query using normal fields
results = Article.objects.filter(models.Q(title__term_v2='djangp'))

# query using v2 value using cast with tokenizer
results = Article.objects.filter(models.Q(title__term_v2=ValueCast('django', 'pdb.ngram(2,2)')))

# query jsonfield
results = Article.objects.filter(models.Q(metadata__framework__term_v2='django')) # value can be passed as valuecast also

# query array field
results = Article.objects.filter(tags__term_v2='django')

Mixing ParadeDB Search + ORM

results = Article.objects.filter(
    Search("title", "django") &
    models.Q(rank__gte=5) &
    models.Q(published=True)
)

Aggregation

from paradedb.aggregates import Count, Avg

article_aggregated_count = Article.objects.aggregate(count=Count('id', filter=models.Q(id__all=True)))
print(article_aggregated_count)

avg_read_time_data = Article.objects.aggregate(avg_reading_time=Avg('metadata.reading_time', filter=models.Q(id__all=True)))
print(avg_read_time_data)

🎉 You're All Set!

Your Django app is now:

✅ ParadeDB powered ✅ BM25 indexed ✅ JSON searchable

  • To learn more, see the full documentation

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

paradedb_django-1.3.1.tar.gz (8.9 MB view details)

Uploaded Source

Built Distribution

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

paradedb_django-1.3.1-py3-none-any.whl (48.7 kB view details)

Uploaded Python 3

File details

Details for the file paradedb_django-1.3.1.tar.gz.

File metadata

  • Download URL: paradedb_django-1.3.1.tar.gz
  • Upload date:
  • Size: 8.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for paradedb_django-1.3.1.tar.gz
Algorithm Hash digest
SHA256 0bc0f6b6aae93bfd90999f4cef2bc18c09cd5d363ff59880322b66fb68a656ba
MD5 b6dd75fcdd5a8a53eecdffd834aafd89
BLAKE2b-256 b9947da100a91c5828ae554865659e22d1377a5f311ae65b2742ea3f672760b3

See more details on using hashes here.

File details

Details for the file paradedb_django-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for paradedb_django-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d7b44064244f8b5906f2bb210f101c8b1e1172d40be80b0320cb328fe8ab47dd
MD5 9bc5d4b0e607eea12d0740a1c2d070b2
BLAKE2b-256 2d1f6c4f4cea49114c107025c2b1b3ffcd74abb061b4ebb388eab375c95f1527

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