Django Haystack:MultiValueField 上的范围搜索
我想知道是否可以在多值字段上进行范围搜索。我有一个如下所示的模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决这个问题的一种方法是执行以下操作:
它非常丑陋,但它有效。但仍对其他答案持开放态度。
One way to solve this problem is doing the following:
It's very ugly but it works. Still open to other answer though.
同样的事情:当我应用过滤器 __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
您尝试过范围字段查找吗?
<代码>
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