Django CMS Haystack 2.0 Search Index

By | November 6, 2013

Currently django-cms-search doesn’t support haystack 2.0 yet, so here is my modified version to work on haystack 2.0 based on

import datetime
from haystack import indexes   
from cms.models.managers import PageManager
from cms.models.pagemodel import Page
from cms.models.pluginmodel import CMSPlugin


class PageIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)
    pub_date = indexes.DateTimeField(model_attr='publication_date', null=True)
    login_required = indexes.BooleanField(model_attr='login_required')
    url = indexes.CharField(model_attr='get_absolute_url')
    title = indexes.CharField(model_attr='get_title')

    def prepare(self, obj):    
        self.prepared_data = super(PageIndex, self).prepare(obj)
        plugins = CMSPlugin.objects.filter(placeholder__in=obj.placeholders.all())
        text = ''              
        for plugin in plugins: 
            instance, _ = plugin.get_plugin_instance()
            if hasattr(instance, 'search_fields'):
                text += ''.join(getattr(instance, field) for field in instance.search_fields)
        text += obj.get_meta_description() or u''
        text += obj.get_title() or u''
        text += obj.get_meta_keywords() if hasattr(obj, 'get_meta_keywords') and obj.get_meta_keywords() else u''
        self.prepared_data['text'] = text
        return self.prepared_data       

    def get_model(self):
        return Page

    def index_queryset(self, using=None):
        return Page.objects.published().filter(publisher_is_draft=False).distinct()

8 thoughts on “Django CMS Haystack 2.0 Search Index

  1. Nuno Diogo da Silva

    I’ve added your code snippet into my search_indexes.py. And despite being able to rebuild my index successfully (i saw the pages being index on the output of the command), it doesn’t seem to display the pages when i search for them.

    Context:

    I’m using the django cms i18n (bilingual);
    I haven’t installed django-cms-search (since i’m using this snippet).

    Regards.

    Reply
    1. james_lin Post author

      Hi you don’t need django-cms-search to use my snippet, I am not entirely sure if it’s due to multilingual, what error messages are you seeing?

      Reply
      1. Nuno Diogo da Silva

        Hi. I don’t see any. Due to the snippet, the rebuild_index command is able to index cms pages, but when i search for the pages, it doesn’t appear anything. Back when i post my question, i forgot to add the “page_text.txt” file. I thought it was because of that, but even now it doesn’t seem work.

        Reply
          1. Nuno Diogo da Silva

            I need to take more time analyzing my code. Although, that snippet you give me now helped a bit. but it’s not working fully. It works with an english search, but not with a portuguese one.

            Anyway, thanks for your time. When i figure it out, i shall write it here 😉

  2. Nuno Diogo da Silva

    I’m using this code snippet on my search_indexes.py and despite being able of indexing the cms pages when rebuilding the index, it’s not returning results when searching.

    Context points:

    – I haven’t installed django-cms-search app, since i’m using this;
    – It’s a bilingual project.

    Any suggestion?

    Regards in advanced.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.