I use Haystack and Whoosh to search for a custom application with city data from the Geonames project.
I have only a small amount of imported Geonames city data (22,917 records). I would like to order the results in the city, and I had problems getting good results.
When I use order_by on my SearchQuerySet , the results are very slow. He also orders the "name" field correctly, but not the "population" field, so I think there is probably just something wrong.
Here's the search index:
class EntryIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) name = indexes.CharField(indexed=False, model_attr='ascii_name') population = indexes.CharField(indexed=False, model_attr='population') django_id = indexes.CharField(indexed=False, model_attr='id') def get_model(self): return Entry def index_queryset(self): return self.get_model().objects.all()
Here's the pattern:
{{ object.ascii_name }} {{ object.alternate_names }} {{ object.country.name }} {{ object.country.iso }} {{ object.admin1_division.ascii_name }} {{ object.admin1_division.name }} {{ object.admin1_division.code }} {{ object.admin2_division.ascii_name }} {{ object.admin2_division.name }}
Here is the appropriate view code:
query = request.GET.get('q', '') results = SearchQuerySet().models(Entry).auto_query(query).order_by('population')
When I answer order_by from the request, it returns in less than one second. Using it takes almost 10 seconds, and the results are not sorted by population. An order by name works, but it also takes ~ 10 seconds.
Note. I also tried with the built-in Haystack search, and it is very slow when I try to sort by population:
qs = SearchQuerySet().order_by('-population') urlpatterns = patterns('', ... url(r'^demo2/$', SearchView(searchqueryset=qs)), )