feincms / feincms3

feincms3 provides additional building blocks on top of django-content-editor and django-tree-queries which make building a page CMS (and also other types of CMS) simpler.

Home Page:https://feincms3.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to configure Haystack search?

fiee opened this issue · comments

While I’m a long time user of FeinCMS, now moving my first site to FeinCMS3, I never really used a search engine (except Google site search). Thus I’m a bit lost how to configure a Haystack index – how can I include all the text fields from page plugins?

„How to traverse a page with all its plugins“ might be interesting for other uses, too.

I didn’t read Haystack’s docs far enough. Seems like I can just use a template to render the whole page.

EDIT: Haystack doesn’t use the same context as a web request, it lacks many contents, including FeinCMS3’s regions.
Do you have any hints how to render without?

„How to traverse a page with all its plugins“ might be interesting for other uses, too.

Yes, this might be interesting. It was a bit complicated with FeinCMS 1.x (you had to use Page.content_type_for or iterate over Page._feincms_content_types). feincms3 makes this a bit more straightforward because you (have to) write all models yourself.

EDIT: Haystack doesn’t use the same context as a web request, it lacks many contents, including FeinCMS3’s regions.

I'm not sure but is there a reason why you cannot use Regions.from_item(page, renderer=renderer) and ´{% render_region %}for your Haystack templates as well? I'm not sure why you'd need a request for this. Or maybe theRequestFactory` from Django's test utils helps?

I finally got back to this, and I got it running.
I recognized I only need titles and the contents of the RichText plugins.

I.e. search_indexes.py is just the usual

from haystack import indexes
from . import models

class PageIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return models.Page

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(is_active=True)

And the template search/indexes/pages/page_text.txt looks like:

{{ object.title }}
{% for rt in object.pages_richtext_set.all %}
{{ rt.text|striptags }}
{% endfor %}
{% for tag in object.tags.all %}{{ tag.name }} {% endfor%}

(My cms app is called "pages", I used my own RichText plugin, since I wanted to customize the CKeditor like before, and I also use taggit tags.)

Looks good :)

Maybe you’d like to include such an example in the docs to help other noobs like me ;)

My first productive FeinCMS3 site: https://www.dreiviertelhaus.de
(my other sites still use the old FeinCMS)