I'd like to sort the results of my django-haystack query by title.
from haystack.query import SearchQuerySet
for result in SearchQuerySet().all().order_by('result_title_sort'):
print result.result_title_sort
I keep getting this error however:
there are more terms than documents in field "result_title_sort", but it's impossible to sort on tokenized fields
This is my haystack field definition:
result_title_sort = CharField(indexed=True, model_attr='title')
How should I define the field, so I can sort on it?
Thank you Mark Chackerian, your solution does work for sorting. I however I still felt slightly uncomfortable modifying the output of the auto-generated schema.xml
. I found a solution by using Solr's <dynamicField> field types. The Django-Haystack docs aren't that clear on how to go about using dynamic fields, but basically if you just include a new key in the dict
returned by the SearchIndex
's prepare()
and a dynamicField
will be added to the document at index time.
Remove your existing attribute from the SearchIndex
#result_title_sort = CharField(indexed=True, model_attr='title')
def prepare(self, obj):
prepared_data['result_title_sort_s'] #notice the "_s"
The above will create a dynamic string field in the document called result_title_sort_s
by which you will be able to sort your results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With