Code Monkey home page Code Monkey logo

django-postgrefts's Introduction

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:

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.title

    def 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 thumbnail

In a templates/cms/indexes/title_body.txt in the same application:

{% 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

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]

django-postgrefts's People

Contributors

bertrandbordage avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

wnp emencia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.