0

我需要能够像这样对索引中的数据进行排序:

http://mywebapp.com/search/?q=something&sort_by=points-desc

换句话说,我需要在 URL 中传递“sort_by”的值。我试图搜索文档,但找不到任何示例。

有人可以就如何实现这一目标提供线索吗?

我正在使用 Django Haystack 2.0 和 SearchView()

有什么线索吗?


更新:干草堆搜索索引

class LinkViewIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    section = indexes.CharField(model_attr='section')
    section_id = indexes.CharField(model_attr='section_id')
    item_author_username = indexes.CharField(model_attr='item_author_username')
    title = indexes.CharField(model_attr='title')
    url = indexes.CharField(model_attr='url')
    url_image = indexes.CharField(model_attr='url_image')
    comments = indexes.CharField(model_attr='comments', null=True)
    comments_count = indexes.CharField(model_attr='comments_count')
    points = indexes.CharField(model_attr='points')
    date_inserted = indexes.DateTimeField(model_attr='date_inserted')

    def get_model(self):
    return LinkView

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


class TextViewIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    section = indexes.CharField(model_attr='section')
    section_id = indexes.CharField(model_attr='section_id')
    item_author_username = indexes.CharField(model_attr='item_author_username')
    title = indexes.CharField(model_attr='title')
    texto = indexes.CharField(model_attr='texto')
    comments = indexes.CharField(model_attr='comments', null=True)
    comments_count = indexes.CharField(model_attr='comments_count')
    points = indexes.CharField(model_attr='points')
    date_inserted = indexes.DateTimeField(model_attr='date_inserted')

    def get_model(self):
    return TextView

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


class OffTopicLinkViewIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    section = indexes.CharField(model_attr='section')
    section_id = indexes.CharField(model_attr='section_id')
    item_author_username = indexes.CharField(model_attr='item_author_username')
    title = indexes.CharField(model_attr='title')
    url = indexes.CharField(model_attr='url')
    url_image = indexes.CharField(model_attr='url_image')
    comments = indexes.CharField(model_attr='comments', null=True)
    comments_count = indexes.CharField(model_attr='comments_count')
    points = indexes.CharField(model_attr='points')
    date_inserted = indexes.DateTimeField(model_attr='date_inserted')

    def get_model(self):
    return OffTopicLinkView

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


class OffTopicTextViewIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    section = indexes.CharField(model_attr='section')
    section_id = indexes.CharField(model_attr='section_id')
    item_author_username = indexes.CharField(model_attr='item_author_username')
    title = indexes.CharField(model_attr='title')
    texto = indexes.CharField(model_attr='texto')
    comments = indexes.CharField(model_attr='comments', null=True)
    comments_count = indexes.CharField(model_attr='comments_count')
    points = indexes.CharField(model_attr='points')
    date_inserted = indexes.DateTimeField(model_attr='date_inserted')

    def get_model(self):
    return OffTopicTextView

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


class BusinessLinkViewIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    section = indexes.CharField(model_attr='section')
    section_id = indexes.CharField(model_attr='section_id')
    item_author_username = indexes.CharField(model_attr='item_author_username')
    title = indexes.CharField(model_attr='title')
    country = indexes.CharField(model_attr='country')
    localidades = indexes.CharField(model_attr='localidades')
    url = indexes.CharField(model_attr='url')
    url_image = indexes.CharField(model_attr='url_image')
    comments = indexes.CharField(model_attr='comments', null=True)
    comments_count = indexes.CharField(model_attr='comments_count')
    points = indexes.CharField(model_attr='points')
    date_inserted = indexes.DateTimeField(model_attr='date_inserted')

    def get_model(self):
    return BusinessLinkView

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


class BicycleAdViewIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    section = indexes.CharField(model_attr='section')
    section_id = indexes.CharField(model_attr='section_id')
    item_author_username = indexes.CharField(model_attr='item_author_username')
    title = indexes.CharField(model_attr='title')
    country = indexes.CharField(model_attr='country')
    city = indexes.CharField(model_attr='city')
    n_item_kind = indexes.CharField(model_attr='n_item_kind')
    n_bicycle_ad_condition = indexes.CharField(model_attr='n_bicycle_ad_condition')
    bicyclead_link_url = indexes.CharField(model_attr='bicyclead_link_url', null=True)
    bicyclead_link_url_image = indexes.CharField(model_attr='bicyclead_link_url_image', null=True)
    bicyclead_insite_image = indexes.CharField(model_attr='bicyclead_insite_image', null=True)    
    texto = indexes.CharField(model_attr='texto', null=True)
    telephone = indexes.CharField(model_attr='telephone', null=True)
    price = indexes.CharField(model_attr='price', null=True)
    n_bicycle_ad_payment_type = indexes.CharField(model_attr='n_bicycle_ad_payment_type', null=True)
    propriedades = indexes.CharField(model_attr='propriedades', null=True)
    comments = indexes.CharField(model_attr='comments', null=True)
    comments_count = indexes.CharField(model_attr='comments_count')
    points = indexes.CharField(model_attr='points')
    date_inserted = indexes.DateTimeField(model_attr='date_inserted')

    def get_model(self):
    return BicycleAdView

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

问题应该是。如何使用以下方式传递 GET 参数以按“points”或“date_inserted”排序:

http://mywebapp.com/search/?q=something&sort_by=points-desc
4

1 回答 1

1

覆盖 haystack 的 get_queryset 方法并将排序字段添加到索引模型中,例如:

class LinkListView(ListView):
    context_object_name = “link”

    def get_queryset(self):
        sort = self.request.GET.get(‘sort_by’)
        if sort:
            return Link.published.filter().order_by('{0}'.format(sort))
        else:
            return Link.published.filter().order_by('-date_inserted')
于 2013-03-18T12:14:13.350 回答