django-haystack 排序 - 我该如何处理这个问题?

发布于 2024-09-04 08:24:53 字数 344 浏览 2 评论 0原文

我正在使用 django-haystack 作为我网站上的搜索页面。我基本上完成了,但对订购不太满意,也不太确定 haystack 如何决定如何订购所有东西。

我知道我可以使用 order_by 覆盖 SearchQuerySet,但这完全覆盖了它。假设我想强制搜索按库存 (BooleanField) 排序,以便库存产品显示在顶部,但然后像平常一样执行其他所有操作。我该怎么做?

我尝试执行 order_by('-in_stock', 'content') 图形 content 是它默认使用的内容,但如果我让它自行排序,它会产生非常不同的结果。

感谢您对此事的任何意见!

I'm using django-haystack for a search page on my site. I'm basically done, but not quite happy with the ordering and not quite sure how haystack decides how to order everything.

I know I can over-ride the SearchQuerySet by using order_by but that over-rides it entirely. Let's say I want to force the search to order by in stock (BooleanField), so that the products that are in stock show up on top, but then do everything else as it normally would. How do I do that?

I tried doing order_by('-in_stock', 'content') figure content was what it used by default but it produces very different results from if I just leave it to do its own ordering.

Thanks for any input on this matter!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-09-11 08:24:53

您的 search_indexes.py 中必须有一个带有 in_stock: 的索引,

class YourModel(indexes.SearchIndex):
    in_stock = indexes.BooleanField(model_attr='model_in_stock')

并且在您的 urls: 中:

sqs = SearchQuerySet().models(YourModel).order_by('-in_stock', 'score') # score is a field of haystack

这样,您首先显示结果(如果有库存),然后按分数显示!

You must have a index in your search_indexes.py with in_stock:

class YourModel(indexes.SearchIndex):
    in_stock = indexes.BooleanField(model_attr='model_in_stock')

and in your urls:

sqs = SearchQuerySet().models(YourModel).order_by('-in_stock', 'score') # score is a field of haystack

In this way, you show the results first if they are in stock and then by score!

吐个泡泡 2024-09-11 08:24:53

要对 CharField 进行排序,请使其可存储,但不可索引。

sorted_name = indexes.CharField(indexed=False, stored=True)

如果您需要使其可排序和可索引,请在 SearchIndex 中使用两个不同的字段。

To sort on a CharField, make it storable, but not indexable.

sorted_name = indexes.CharField(indexed=False, stored=True)

If you need to have it sortable and indexable, use two different fields in the SearchIndex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文