Pickels
Pickels

Reputation: 34680

Django Haystack: Range search on MultiValueField

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'

Upvotes: 2

Views: 1716

Answers (3)

hekevintran
hekevintran

Reputation: 23732

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

Upvotes: 0

ragzovskii
ragzovskii

Reputation: 11

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

Upvotes: 1

Pickels
Pickels

Reputation: 34680

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.

Upvotes: 3

Related Questions