Unable to order Haystack / Whoosh results (and it is very slow)

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)), ) 
+4
source share
3 answers

I am doing almost the same thing, and ordering works quickly and correctly for me.

The only thing you do is significantly different:

 query = request.GET.get('q', '') results = SearchQuerySet().models(Entry).auto_query(query).order_by('population') 

Since you are specifying a query, I assume that you have created your own view. You do not need to customize the view. I have this implemented in my urls.py:

 from haystack.forms import ModelSearchForm from haystack.query import SearchQuerySet from haystack.views import SearchView, search_view_factory sqs = SearchQuerySet().models(MyModel).order_by('-weight') urlpatterns += patterns('', url(r'^search/$', search_view_factory( view_class=SearchView, template='search/search.html', searchqueryset=sqs, form_class=ModelSearchForm ), name='search'), ) 
+1
source

I found that I could not order the results using order_by. I was getting what seemed like a weird partial sort. In the end, I realized that the default ordering was by relevance. The order_by order that I used was apparently only sorted in each rank. This item is not shown in the Haystack documentation.

I think the lesson is probably that if you want your query for results to ignore relevance, you need to post your results before displaying them.

Probably a little off topic, but I was a bit surprised that your index field is CharField. Does this fit your model?

0
source

I know that I am three years late, but recently I ran into the same problem with the project that I was given.

I think the only problem is the indexed=False parameter that you pass to the population CharField.

I fixed my problem by deleting it.

0
source

All Articles