Django Haystack:MultiValueField 上的范围搜索

发布于 2024-11-29 20:05:46 字数 819 浏览 2 评论 0原文

我想知道是否可以在多值字段上进行范围搜索。我有一个如下所示的模型:

Book
   Title = 'Awesome Book'
   Prices = [ Decimal('10.00'), Decimal('15.00'), Decimal('20.00') ]

我正在使用 MultiValueField 对价格字段进行索引,并且我希望能够执行以下操作:

sqs = SearchQueryResult()
sqs.filter(prices__gt=Decimal('10.00'), prices__lt=Decimal('20.00'))

这可能吗?还是我必须使用其他方法对多个值进行范围搜索?

更新:

我忘了提及 __gt 不起作用,我认为这是因为它将其索引为字符串列表。我发现以下内容 链接,他们谈论子类化 MultiValueField。我尝试过这个,但我无法让它给我一个小数列表。子类化的 MultiValueFiled 如下所示:

class MultiValueDecimalField(fields.MultiValueField):
    field_type = 'decimal'

I was wondering if it is possible to do a range search on a MultiValueField. I have a model that looks like the following:

Book
   Title = 'Awesome Book'
   Prices = [ Decimal('10.00'), Decimal('15.00'), Decimal('20.00') ]

I am indexing the prices field with a MultiValueField and I would like to be able to do the follow:

sqs = SearchQueryResult()
sqs.filter(prices__gt=Decimal('10.00'), prices__lt=Decimal('20.00'))

Is this possible or do I have to use something else to do a range search on multiple values?

Update:

I forgot to mention that the __gt doesn't work and I think it's because it's indexing it as a list of strings. I found the following link where they talk about subclassing MultiValueField. I tried this but I can't get it to give me a list of decimals. The subclassed MultiValueFiled looks like the following:

class MultiValueDecimalField(fields.MultiValueField):
    field_type = 'decimal'

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

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

发布评论

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

评论(3

浅浅 2024-12-06 20:05:46

解决这个问题的一种方法是执行以下操作:

sqs.filter(prices__in=['%.2f' % (x/100.00) for x in range(1000, 2000)])

它非常丑陋,但它有效。但仍对其他答案持开放态度。

One way to solve this problem is doing the following:

sqs.filter(prices__in=['%.2f' % (x/100.00) for x in range(1000, 2000)])

It's very ugly but it works. Still open to other answer though.

却一份温柔 2024-12-06 20:05:46

同样的事情:当我应用过滤器 __gte、__gt 等时,我注意到 SearchQuerySet 返回了不正确的数据。当我将字段类型更改为 FloatField 时,一切都开始正常工作。看起来像虫子,或者什么的

Same thing: when I applied filters __gte, __gt etc. I noticed that SearchQuerySet returns incorrect data. When I changed field type to FloatField everything started working right. looks like bug, or smth

是伱的 2024-12-06 20:05:46

您尝试过范围字段查找吗?

<代码>
SearchQuerySet().filter(view_count__range=[3, 5])

http://django-haystack.readthedocs.org /en/latest/searchqueryset_api.html#field-lookups

Have you tried the range field lookup?


SearchQuerySet().filter(view_count__range=[3, 5])

http://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#field-lookups

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