Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedy-purwanto/0122e1c67f47119a13a8 to your computer and use it in GitHub Desktop.
Save dedy-purwanto/0122e1c67f47119a13a8 to your computer and use it in GitHub Desktop.
"""It's a hack for solr backend to apply django-style random ordering
with '?'. Change haystack/backends/solr_backend.py
"""
# .. Somewhere in the beginning
import random
# .. Somewhere in :638, replace content of for-loop into these:
if order_by.startswith('-'):
order_by_list.append('%s desc' % order_by[1:])
elif order_by.startswith('?'):
order_by_list.append('random_%04d desc' % random.randint(0, 9999))
else:
order_by_list.append('%s asc' % order_by)
"""In solr schema, make sure you have:
Inside <types>:
<fieldType name="random" class="solr.RandomSortField" />
Inside <fields>:
<dynamicField name="random*" type="random" indexed="true"
stored="false"/>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment