Skip to main content

Django-sphinxsearch

SphinxSearch database backend for Django.

Build Status PyPI version

Installation and usage

  1. Install django-sphinxsearch package

    pip install django_sphinxsearch
  2. Configure Django settings

    INSTALLED_APPS += (
        'sphinxsearch',
    )
    
    SPHINX_DATABASE_NAME = 'sphinx'
    
    DATABASES[SPHINX_DATABASE_NAME] = {
        'ENGINE': 'sphinxsearch.backend.sphinx',
        'HOST': '127.0.0.1',
        'PORT': 9306,
    }
    
    DATABASE_ROUTERS = ['sphinxsearch.routers.SphinxRouter']
  3. Create index definitions in sphinx.conf

    index testapp_testmodel
    {
        type              = rt
        path              = /data/sphinx/testapp/testmodel/
    
        rt_field          = sphinx_field
        rt_attr_uint      = attr_uint
        rt_attr_bool      = attr_bool
        rt_attr_bigint    = attr_bigint
        rt_attr_float     = attr_float
        rt_attr_multi     = attr_multi
        rt_attr_multi_64  = attr_multi_64
        rt_attr_timestamp = attr_timestamp
        rt_attr_string    = attr_string
        rt_attr_json      = attr_json
    }
  4. Define Django model for index

    from datetime import datetime
    from django.db import models
    
    from jsonfield.fields import JSONField
    
    from sphinxsearch import models as spx_models
    
    class TestModel(spx_models.SphinxModel):
        # Note that NULL values are not allowed for sphinx rt-index.
    
        # Indexed text field. If no attribute with same name defined, can't be
        # retrieved from index.
        sphinx_field = spx_models.SphinxField(default='')
    
        # Numeric attributes
        attr_uint = models.IntegerField(default=0)
        attr_bigint = models.BigIntegerField(default=0)
        attr_float = models.FloatField(default=0.0)
        attr_timestamp = spx_models.SphinxDateTimeField(default=datetime.now)
        attr_bool = models.BooleanField(default=False)
    
        # String attributes
        attr_string = models.CharField(max_length=32, default='')
        attr_json = JSONField(default={})
    
        # Multi-value fields (sets of integer values)
        attr_multi = spx_models.SphinxMultiField(default=[])
        attr_multi_64 = spx_models.SphinxMulti64Field(default=[])
  5. Query index from your app

    # Numeric attributes filtering
    TestModel.objects.filter(attr_uint=0, attr_float__gte=10, attr_multi__in=[1, 2])
    
    # For sphinxsearch>=2.2.7, string attr filtering enabled
    TestModel.objects.filter(attr_string='some test')
    
    # Use mysql-fulltext-search filtering:
    
    TestModel.objects.filter(sphinx_field__search='find me')
    
    # Run match queries
    TestModel.objects.match(
        'find in all fields',
        sphinx_field='only in this field')
    
    # Insert and update documents to index
    
    obj = TestModel.objects.create(**values)
    obj.attr_uint = 1
    obj.save()
    
    TestModel.objects.filter(attr_bool=True).update(attr_uint=2)

Notes for production usage

  • Sphinxsearch engine has some issues with SQL-syntax support, and they vary from one version to another. I.e. float attributes are not comparable, string attributes were not comparible till v2.2.7.

  • Without limits sphinxsearch returns only 20 matched documents.

  • uint attributes accept -1 but return it as unsigned 32bit integer.

  • bigint accept 2**63 + 1 but return it as signed 64bit integer.

Metadata

Release files for django-sphinxsearch 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-sphinxsearch 0.2.0
File Size Uploaded
django_sphinxsearch-0.2.0.tar.gz 12.4 kB Details

Release files / django_sphinxsearch-0.2.0.tar.gz

Download URL django_sphinxsearch-0.2.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ebd7d405703d166bdeea18f17b3a5ef44a39b03efd24c35c1cddf2c04285d44b
BLAKE2b-256 checksum
How to use checksums
d65c76862408ccf3ba03ea1b7e334a27fbcfbf3e48dc5328a58051d23f43a562
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

4.2.0

2 release files

4.1.0

1 release file

4.0.0

1 release file

3.3.0

1 release file

3.2.0

1 release file

3.1.1

1 release file

3.1.0

1 release file

3.0.2

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.2.1

1 release file

2.2.0

1 release file

2.1.0

1 release file

2.0.0

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

This release

0.2.0 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page