从Haystack搜索结果中获取Django CMS插件信息

时间:2018-07-18 10:58:22

标签: django elasticsearch django-haystack django-cms

我的搜索结果页面也需要显示有关找到查询的插件的信息。我发现this question遇到了类似的问题,但我不仅需要内容,还需要了解有关插件的知识-即它叫什么,它在页面上的位置以及其他内容。基本上,我想引用查询所在的插件,但是我只能找到有关页面和标题的信息。我无法在SearchQuerySet对象上及其附近的任何地方找到它-在Haystack的文档中也没有找到它。有可能吗?

我正在使用的堆栈:Elasticsearch 2.4,django-haystack 2.8,aldryn-search 1.0(用于CMS索引)。

2 个答案:

答案 0 :(得分:0)

我最终为CMSPlugins编写了一个新索引。不知道我的代码使用了多少,但也许会帮助别人。

from django.conf import settings

from aldryn_search.helpers import get_plugin_index_data
from aldryn_search.utils import clean_join, get_index_base
from cms.models import CMSPlugin


class CMSPluginIndex(get_index_base()):
    haystack_use_for_indexing = True
    index_title = True

    object_actions = ('publish', 'unpublish')

    def get_model(self):
        return CMSPlugin

    def get_index_queryset(self, language):
        return CMSPlugin.objects.select_related(
            'placeholder'
        ).prefetch_related(
            'placeholder__page_set'
        ).filter(
            placeholder__page__publisher_is_draft=False,
            language=language
        ).exclude(
            plugin_type__in=settings.HAYSTACK_EXCLUDED_PLUGINS
        ).distinct()

    def get_search_data(self, obj, language, request):
        current_page = obj.placeholder.page
        text_bits = []

        plugin_text_content = self.get_plugin_search_text(obj, request)
        text_bits.append(plugin_text_content)

        page_meta_description = current_page.get_meta_description(fallback=False, language=language)

        if page_meta_description:
            text_bits.append(page_meta_description)

        page_meta_keywords = getattr(current_page, 'get_meta_keywords', None)

        if callable(page_meta_keywords):
            text_bits.append(page_meta_keywords())

        return clean_join(' ', text_bits)

    def get_plugin_search_text(self, base_plugin, request):
        plugin_content_bits = get_plugin_index_data(base_plugin, request)
        return clean_join(' ', plugin_content_bits)

    def prepare_pub_date(self, obj):
        return obj.placeholder.page.publication_date

    def prepare_login_required(self, obj):
        return obj.placeholder.page.login_required

    def get_url(self, obj):
        parent_obj = self.ancestors_queryset(obj).first()

        if not parent_obj:
            return obj.placeholder.page.get_absolute_url()

        return # however you get the URL in your project

    def get_page_title_obj(self, obj):
        return obj.placeholder.page.title_set.get(
            publisher_is_draft=False,
            language=obj.language
        )

    def ancestors_queryset(self, obj):
        return obj.get_ancestors().filter(
            plugin_type=# Some plugins that I wanted to find
        ).order_by(
            '-depth'
        )

    def get_title(self, obj):
        parent_obj = self.ancestors_queryset(obj).first()

        if not parent_obj:
            return self.get_page_title_obj(obj).title

        return # get title from parent obj if you want to

    def prepare_site_id(self, obj):
        return obj.placeholder.page.node.site_id

    def get_description(self, obj):
        return self.get_page_title_obj(obj).meta_description or None

答案 1 :(得分:0)

如果您使用aldryn-search,则只需在PLACEHOLDERS_SEARCH_LIST中定义所有要检查的占位符,因此将检查所有内部插件:

PLACEHOLDERS_SEARCH_LIST = {
    '*': {
        'include': ['content'],
        'exclude': [''],
    },
}