8

I'm using Haystack for search, and the resulting SearchQuerySet returned contains None elements:

>> SearchQuerySet().models(Question, Document, Idea)
>> [<SearchResult: idea.idea (pk=3875)>, None, None, None]

Running rebuild_index doesn't help. If I remove the .models() call from the first line, the problem goes away. I could just filter out None elements from the results, but I wanted to know if this is intended behaviour?

I am using Django 1.4, Whoosh, and Haystack 2.0.0-beta

zimkies
  • 1,067
  • 1
  • 9
  • 20
  • 1
    I haven't tried it yet, but [this guy](https://groups.google.com/forum/?fromgroups#!searchin/django-haystack/none/django-haystack/XSjIr8wx3lA/7f82x2QJ9cUJ) suggests using Solr instead of Whoosh. – simlmx May 10 '12 at 04:01
  • Update : now I've tried it and it works with Solr. – simlmx May 11 '12 at 15:30
  • everybody knows that elastic search or solr do and perform better - but for small to mid-sized sites, Whoosh is very ok IMHO and makes no trouble installing/setting up (only afterwards, haha). – benzkji Dec 15 '15 at 12:18
  • @zimkies are you using solr as well, now? – benzkji Jan 21 '16 at 08:54
  • @zimkies having an answer two years later, you could accept it after two more years, no? ;-) at least it is the only answer that does somehow resolve the problem, and does not suggest installing a different backend ;-) – benzkji Oct 17 '16 at 08:14

3 Answers3

5

I had this problem when haystack index had records without corresponding records in DB.

Sergey Fedoseev
  • 2,800
  • 2
  • 18
  • 16
  • 3
    Thanks, this isn't the problem though. It turns out the 'None' values correspond to User objects. Apparently the _models_ function removes hits that aren't of the right model type but leaves a None type instead of filtering them out – zimkies Aug 22 '12 at 20:06
  • Can't you say, is this problem only for 'whoosh' backend? Does using Solr engine solves it? – Daria Jul 23 '14 at 10:46
  • 1
    So there's no solution for Whoosh then? – awidgery Jul 31 '15 at 15:00
3

No, it is definitly not the intended behaviour, and as I can see, seems to be related to a design decision in Whoosh. And, as of December 2015, this still seems to be an issue, as can be seen here. Also, I can reproduce it with my setup (django 1.8.5, haystack 2.4.1, Whoosh 2.7.0) - that's why I came here.

Quick and dirty solution that worked for me: Define a new field on your index (type/model/tomato), set it the same for each model, and filter against that value:

.filter(type='my_modelname')

instead of

.models(MyModel).

I don't know (yet) how this scales, but seems to work ok.

benzkji
  • 1,718
  • 20
  • 41
1

Had the same problem using Whoosh, installed Elasticsearch and the None elements went away.

gitaarik
  • 42,736
  • 12
  • 98
  • 105