0

次のように、インデックスからデータを並べ替えることができる必要があります。

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

つまり、URLで「sort_by」の値を渡す必要があります。ドキュメントを検索しようとしましたが、例が見つかりません。

誰かがこれを達成する方法についての手がかりを与えることができますか?

Django Haystack 2.0とSearchView()を使用しています

手がかりはありますか?


更新:Haystack検索インデックス

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 に答える