【问题标题】:django cmsplugin-blog search indexes for haystack with whoosedjango cmsplugin-blog 用 whoose 搜索 haystack 的索引
【发布时间】:2012-05-30 10:05:12
【问题描述】:

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

但只有搜索索引可用于 django-cms-facetsearch。但是 facetsearch 需要 solr,我不想只为几个 cms-pages 和 blogentries 运行 solr 服务器。我只想使用 haystack 和 whoosh,因为它的配置非常简单。

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

感谢您的帮助...

【问题讨论】:

    标签: python django search django-haystack django-cms


    【解决方案1】:

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

    我刚刚为 cmsplugin-blog 和 haystack 创建了一个自定义搜索索引,灵感来自 django-cms-search 中用于常规 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)
    

    稍后我会考虑在 github 上放置一个带有此搜索索引的防弹版本的 cmsplugin-blog 的分支。在有帮助的地方随意使用它。

    【讨论】:

      猜你喜欢
      • 2011-05-20
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 2015-09-21
      • 2011-03-10
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多