Django-sphinxsearch
SphinxSearch database backend for Django.
- Not a django_sphinx_db fork
Django>=2.0,<3.1supported- Tested against Django-2.2 (LTS) and Django-3.0
Installation and usage
-
Install django-sphinxsearch package
pip install django_sphinxsearch
-
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, 'OPTIONS': { 'use_unicode': False # if non-ascii letters looks bad in results } } DATABASE_ROUTERS = ['sphinxsearch.routers.SphinxRouter']
-
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 } -
Define Django model for index
from datetime import datetime from django.db import models from sphinxsearch import models as spx_models class FieldMixin(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. class Meta: abstract = True # Indexed text field. If no attribute with same name defined, can't be # retrieved from index. sphinx_field = spx_models.SphinxField(default='') other_field = spx_models.SphinxField(default='') # Numeric attributes attr_uint = spx_models.SphinxIntegerField(default=0, db_column='attr_uint_') attr_bigint = spx_models.SphinxBigIntegerField(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 = spx_models.JSONField(default={}) # Multi-value fields (sets of integer values) attr_multi = spx_models.SphinxMultiField(default=[]) attr_multi_64 = spx_models.SphinxMulti64Field(default=[]) class TestModel(FieldMixin, spx_models.SphinxModel): pass
-
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)
Run tests
docker-compose up django
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 comparable 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.
- Use SphinxIntegerField and SphinxBigIntegerField instead of IntegerField and
BigIntegerField from django.db.models, because IN is an expression in
SQL (
value IN column), but a function (IN(value, column)) in sphinxsearch. - Since 3.0.1 multi64 field incorrectly parses values greater than 2**31 and is completely unusable for bigint values.
- In 3.1.1 you can create rt index with same string field and attr from config but you cant clone this index, so you attr-based filtering does not work.
Metadata
Release files for django-sphinxsearch 4.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_sphinxsearch-4.2.0.tar.gz | 15.8 kB | Without provenance | Details for django_sphinxsearch-4.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_sphinxsearch-4.2.0-py3-none-any.whl | Python 3 | none | any | Details for django_sphinxsearch-4.2.0-py3-none-any.whl |
Total release size: 34.0 kB
Release files / django_sphinxsearch-4.2.0.tar.gz
| Download URL | django_sphinxsearch-4.2.0.tar.gz |
|---|---|
| Size | 15.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d7f57919555f2c63ed4833aa99ae88c9dd5acf236aa4c1188b59afb8619540d5
|
|
BLAKE2b-256 checksum How to use checksums |
5d08abf7a6279f0e8b0b4ff2a6a2b1924215009e77bc6787b64423f5cd34b2b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
|
Release files / django_sphinxsearch-4.2.0-py3-none-any.whl
| Download URL | django_sphinxsearch-4.2.0-py3-none-any.whl |
|---|---|
| Size | 18.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d969655d3acfa5d102b9765701f53cb78e74d48e35a5e8cf374fa163da355e21
|
|
BLAKE2b-256 checksum How to use checksums |
5ddcaf7a1100c3b3d6be08492446c25bda29bd461b48f952af55d2b51f7f6a99
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
|
Security for django-sphinxsearch 4.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.