Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/BertrandBordage/django-postgrefts
Django-PostgreFTS — Adds PostgreSQL Full-Text Search to Django
https://github.com/BertrandBordage/django-postgrefts
Last synced: 3 months ago
JSON representation
Django-PostgreFTS — Adds PostgreSQL Full-Text Search to Django
- Host: GitHub
- URL: https://github.com/BertrandBordage/django-postgrefts
- Owner: BertrandBordage
- License: bsd-3-clause
- Created: 2015-04-24T15:30:15.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-30T10:53:17.000Z (almost 8 years ago)
- Last Synced: 2024-04-04T11:33:20.433Z (7 months ago)
- Language: Python
- Homepage:
- Size: 148 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
- starred-awesome - django-postgrefts - Django-PostgreFTS — Adds PostgreSQL Full-Text Search to Django (Python)
README
Django-PostgreFTS
=================**Please don’t use this project. It’s just a prototype**
Installation
------------- Run ``pip install https://github.com/BertrandBordage/django-postgrefts/archive/master.tar.gz``
- Add ``'postgrefts',`` to ``INSTALLED_APPS``Example usage with django CMS
-----------------------------In a `search_meta.py` file in an application registered in ``INSTALLED_APPS``,
write:.. code-block:: python
from cms.models import Title
from django.utils import translation
from postgre_fts.registry import search_registry, ModelSearchMeta@search_registry.add
class TitleSearchMeta(ModelSearchMeta):
model = Title
select_relateds = ('page',)
prefetch_relateds = ('page__placeholders__cmsplugin_set',)def get_queryset(self):
return self.model.objects.public().filter(
page__in_navigation=True,
page__level__gt=1,
language=translation.get_language())def get_title(self, obj):
return obj.titledef get_absolute_url(self, obj):
return obj.page.get_absolute_url()def get_boost(self, obj):
return 5.0 / (obj.page.level + 1)def get_image(self, obj):
from .models import CMSPageExtra
thumbnail = None
try:
thumbnail = obj.cmspageextra.thumbnail
except CMSPageExtra.DoesNotExist:
pass
if not thumbnail:
try:
thumbnail = obj.publisher_draft.cmspageextra.thumbnail
except CMSPageExtra.DoesNotExist:
pass
return thumbnailIn a `templates/cms/indexes/title_body.txt` in the same application:
.. code-block:: django
{% autoescape off %}
{{ object.meta_description|default_if_none:'' }}
{% for tag in object.tags.all %}
{{ tag }}
{% endfor %}{% for placeholder in object.page.placeholders.all %}
{% for plugin in placeholder.cmsplugin_set.all %}
{% with plugin_instance=plugin.get_plugin_instance.0 %}
{{ plugin_instance.body }}
{% endwith %}
{% endfor %}
{% endfor %}
{% endautoescape %}Then, you can rebuild index using `./manage.py update_index`.
Query examples
--------------.. code-block:: python
from postgrefts.models import Index
Index.objects.all() # All results
Index.objects.for_language() # All results for the current languages
Index.objects.for_language('fr') # All results in French
Index.objects.for_models(Title, Person) # All results for the given models
Index.objects.search('I <3 Django') # Simple search
Index.objects.autocomplete('Postg') # Returns all results starting with this
Index.objects.search('Marion', sort=True) # Same as search, but sorts results
Index.objects.autocomplete('Don Giov', sort=True)
Index.objects.search('Pur ti miro').highlight() # Highlight query in the results’ body# Full example
Index.objects.for_language().for_models(Language) \
.search('Python', sort=True).highlight()[:5]