Django Haystack - A SearchIndex Based on Multiple Models

梦想的初衷 提交于 2019-12-11 05:27:32

问题


I have some problems by trying to create my SearchIndex with Haystack on Django and I don't know what to do.

Here are my two models:

# Meta: stores meta data about tutorials (category, title)
class Meta(models.Model):
    """
    Database [tutorial.meta]
    """
    mta_title = models.CharField(max_length=TUTORIAL_TITLE_MAX)
    mta_views = models.PositiveIntegerField(default=0)


# Contents: stores the tutorial text content
class Contents(models.Model):
    """
    Database [tutorial.contents]
    """
    tut_id = IdField()
    cnt_body = BBCodeTextField()

Now I want to base my SearchIndex on the 3 following fields: mta_title, mta_views and cnt_body. Here are my current SearchIndex:

from haystack import indexes
from tutorial.models import Meta as TutorialMeta
from account.models import Profile as UserProfile


class TutorialMetaIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='mta_title')
    views = indexes.CharField(model_attr='mta_views')
    # Haystack reserves the content field names for internal use
    cnt_body = indexes.CharField()

    def get_model(self):
        return TutorialMeta

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

    def prepare_cnt_body(self, obj):
        ????

I've seen on this question, that the answer was to create a prepare_cnt_body. But I don't know what I should return.

Thank you everyone.


回答1:


Thank you Sectio Aurea,

But here is my solution with a simple prepare function:

class TutorialIndex(indexes.SearchIndex, indexes.Indexable):
    """
    Index the tutorials
    """
    text = indexes.CharField(document=True, use_template=True)
    tut_id = indexes.IntegerField(model_attr='tut_id')
    cnt_body = indexes.CharField(model_attr='cnt_body')
    mta_title = indexes.CharField()
    mta_views = indexes.CharField()

    def get_model(self):
        """
        Return the current model
        """
        return TutorialContents

    def get_updated_field(self):
        """
        Return the update date tracking field
        """
        return "cnt_date"

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

    def prepare(self, object):
        """
        Prepare the search data
        """
        self.prepared_data = super(TutorialIndex, self).prepare(object)

        # Retrieve the tutorial metas and return the prepared data
        meta = get_tutorial_meta(id=object.tut_id)
        self.prepared_data['mta_title'] = meta.mta_title
        self.prepared_data['mta_views'] = meta.mta_views

        return self.prepared_data



回答2:


No need for 'prepare'. Simply use the template you've referred to in your 'text' field. In your app 'myapp', create the file templates/search/indexes/myapp/tutorialmeta_text.txt. In this file create the following entries using standard Django template language model references, something like:

{{object.mta_title}}
{{object.mta_views}}
{{object.contents.cnt_body}}

Then you'll need to rebuild your index with the new template (./manage.py rebuild_index). This will index each of the three referred fields against each object. With this method, you can also omit the 'title', 'views' and 'cnt_body' fields from your SearchIndex class, as well as the 'prepare' method.



来源:https://stackoverflow.com/questions/18926878/django-haystack-a-searchindex-based-on-multiple-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!