我有一个模型( django-oscar产品):
class Product(AbstractProduct):
# ...
price_display = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
# ...按干草堆索引:
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
# ...
price = indexes.DecimalField(null=True)
# ...
def prepare_price(self, obj):
return obj.price_display or Decimal('0.0')Haystack由elasticsearch支持
pip freeze | grep elastic
elasticsearch==1.6.0
pyelasticsearch==1.4
pip freeze | grep hays
django-haystack==2.1.0现在这个查询集:
# ...
qs = qs.filter(text__contains=q)
qs = qs.order_by('price')按如下方式返回(正确找到的)价格排序的对象:
120 130 24 300 ... 9我不知道哪里出了问题。
发布于 2015-06-19 18:23:28
Haystack (至少在我使用的版本中)向elastic发送一个十进制字段,例如:“价格”:{“类型”:“字符串”,“商店”:true,"term_vector":"with_positions_offsets",“分析器”:“雪球”},
所以它“看起来”不像一个字符串,它是在排序一个字符串...
我已经将我的代码改为使用float,这不是一个问题,我们在这一点上不再谈论金钱。
price = indexes.FloatField(null=True)
def prepare_price(self, obj):
if obj.price_display:
return float(str(obj.price_display))
return float('0.0')现在它将字段发送为:"price":{"type":"float","index":"analyzed","store":true},
点菜很管用,耶!
https://stackoverflow.com/questions/30896229
复制相似问题