用whoose搜索haystack的django cmsplugin-blog搜索索引

时间:2021-08-11 18:54:26

I want to add a search function for a simple django cms homepage with cmsplugin-blog.

我想为一个简单的django cms主页添加一个搜索功能,这个主页有cmsplugin-blog。

But there are only search indexes for use with django-cms-facetsearch. But facetsearch needs solr, and I don`t want to run a solr server only for a few cms-pages and blogentries. I just want to use haystack with whoosh, because it is really simple to configurate.

但是只有搜索索引可以用于django-cm -facetsearch。但是facetsearch需要solr,我不想只运行几个cm页面和blogentries。我只是想使用haystack和whoosh,因为它非常容易配置。

Are there multilingual search indexes for cmsplugin-blog models? Or do I have to write them myself?

cmsplugin-blog模型是否有多语言搜索索引?还是我自己写?

Thank you for helping...

谢谢你的帮助…

1 个解决方案

#1


1  

I had the same problem for a setup using haystack, django-cms, cmsplugin-blog and some other apps as well.

在使用haystack、django-cms、cmsplugin-blog和其他一些应用程序时,我也遇到了同样的问题。

I just created a custom search index for cmsplugin-blog and haystack, inspired by the index used for regular CMS pages in django-cms-search. Take a look at it, it may help you to create your own.

我刚刚为cmsplugin-blog和haystack创建了一个自定义搜索索引,灵感来自于django- CMS搜索中用于常规CMS页面的索引。看看它,它可能会帮助你创造你自己的。

from haystack import indexes
from haystack import site
from cmsplugin_blog.models import Entry, EntryTitle
from cms.models.pluginmodel import CMSPlugin
from django.utils.encoding import force_unicode
import re

def _strip_tags(value):
    """
    Returns the given HTML with all tags stripped.

    This is a copy of django.utils.html.strip_tags, except that it adds some
    whitespace in between replaced tags to make sure words are not erroneously
    concatenated.
    """
    return re.sub(r'<[^>]*?>', ' ', force_unicode(value))

class BlogIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True)
    url = indexes.CharField(stored=True, indexed=False, model_attr='get_absolute_url')
    title = indexes.CharField(stored=True, indexed=False)
    pub_date = indexes.DateTimeField(model_attr='pub_date', null=True)

    def get_model(self):
        return Entry

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

    def prepare_title(self, obj):
        return EntryTitle.objects.filter(entry=obj)[0]

    def prepare_text(self, obj):
        title = EntryTitle.objects.filter(entry=obj)[0]
        placeholder_plugins = CMSPlugin.objects.filter(placeholder__in=obj.placeholders.all())
        text = force_unicode(title)
        plugins = list(placeholder_plugins)
        for base_plugin in plugins:
            instance, plugin_type = base_plugin.get_plugin_instance()
            if instance is None:
                # this is an empty plugin
                continue
            if hasattr(instance, 'search_fields'):
                text += u' '.join(force_unicode(_strip_tags(getattr(instance, field, ''))) for field in instance.search_fields)
            if getattr(instance, 'search_fulltext', False) or getattr(plugin_type, 'search_fulltext', False):
                text += _strip_tags(instance.render_plugin(context=RequestContext(request))) + u' '
        return text

site.register(Entry, BlogIndex)

I will consider putting a fork of cmsplugin-blog with a bulletproof version of this search index on github later. Feel free to use it wherever helpful.

稍后,我将考虑在github上放置一个cmsplugin-blog的分支,其中包含这个搜索索引的防弹版本。在任何有帮助的地方都可以随意使用。

#1


1  

I had the same problem for a setup using haystack, django-cms, cmsplugin-blog and some other apps as well.

在使用haystack、django-cms、cmsplugin-blog和其他一些应用程序时,我也遇到了同样的问题。

I just created a custom search index for cmsplugin-blog and haystack, inspired by the index used for regular CMS pages in django-cms-search. Take a look at it, it may help you to create your own.

我刚刚为cmsplugin-blog和haystack创建了一个自定义搜索索引,灵感来自于django- CMS搜索中用于常规CMS页面的索引。看看它,它可能会帮助你创造你自己的。

from haystack import indexes
from haystack import site
from cmsplugin_blog.models import Entry, EntryTitle
from cms.models.pluginmodel import CMSPlugin
from django.utils.encoding import force_unicode
import re

def _strip_tags(value):
    """
    Returns the given HTML with all tags stripped.

    This is a copy of django.utils.html.strip_tags, except that it adds some
    whitespace in between replaced tags to make sure words are not erroneously
    concatenated.
    """
    return re.sub(r'<[^>]*?>', ' ', force_unicode(value))

class BlogIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True)
    url = indexes.CharField(stored=True, indexed=False, model_attr='get_absolute_url')
    title = indexes.CharField(stored=True, indexed=False)
    pub_date = indexes.DateTimeField(model_attr='pub_date', null=True)

    def get_model(self):
        return Entry

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

    def prepare_title(self, obj):
        return EntryTitle.objects.filter(entry=obj)[0]

    def prepare_text(self, obj):
        title = EntryTitle.objects.filter(entry=obj)[0]
        placeholder_plugins = CMSPlugin.objects.filter(placeholder__in=obj.placeholders.all())
        text = force_unicode(title)
        plugins = list(placeholder_plugins)
        for base_plugin in plugins:
            instance, plugin_type = base_plugin.get_plugin_instance()
            if instance is None:
                # this is an empty plugin
                continue
            if hasattr(instance, 'search_fields'):
                text += u' '.join(force_unicode(_strip_tags(getattr(instance, field, ''))) for field in instance.search_fields)
            if getattr(instance, 'search_fulltext', False) or getattr(plugin_type, 'search_fulltext', False):
                text += _strip_tags(instance.render_plugin(context=RequestContext(request))) + u' '
        return text

site.register(Entry, BlogIndex)

I will consider putting a fork of cmsplugin-blog with a bulletproof version of this search index on github later. Feel free to use it wherever helpful.

稍后,我将考虑在github上放置一个cmsplugin-blog的分支,其中包含这个搜索索引的防弹版本。在任何有帮助的地方都可以随意使用。