Search code examples
pythondjangodjango-haystackwhoosh

Django Haystack Indexing More than one models


I am trying to implement Haystack search for my website with Whoosh back-end. I have been able to successfully setup the haystack app and I can search the model that I have registered, but when I create the search_indexes.py file for another app, I am having the following issue:

I have two models: Member and Events. I create a search_indexes.py for both of them and the corresponding /search/... _text.txt files in the template folder. Then I use ./manage.py rebuild_index

I get the following message:

Indexing 8 events  
Indexing 5 members

However, I am not able to see 13 indexed items:

 $> ./manage.py shell    
 $> from haystack.query import SearchQuerySet   
 $> sqs = SearchQuerySet().all()  
 $> print sqs.count()  
 $> 8

And these are the 8 events that were indexed. Consequently, from the website, I can only search the events, not the members. Deleting the search_indexes.py file from the 'Event' app folder and redoing everything indexes the 5 members correctly and they can be searched as usual. What could be the reason for this?

Update: I included the search_indexes.py files in others apps also to see whether they are indexed properly. I get the following message on rebuilding the index:

Indexing 8 events.  
Indexing 4 guests.     
Indexing 5 members.    
Indexing 8 sponsors.    

Now, it is indexing all the events and members but none of the guests and sponsor. I am able to search for events and members but not for the other two (using both the SearchQuery API and the website)

Update: Issue seems to have been resolved by changing the source of haystack.backends.whoosh_backend. Please see the answers


Solution

  • I've had the same problem the past couple of days (nice timing). I decided to start where you left off and see if I couldn't isolate the cause a bit better.

    The narrowed results are (at least partially) generated by a query of the models which are registered to the site (L298 and on). For my code, the query it generates is...

    django_ct:(barnaby.tag OR barnaby.userprofile)
    

    ...which gives a resultset with only barnaby.tag models. However, if I run...

    django_ct:(barnaby.tag OR barnaby.userprofile) (username:pfrazee OR name:Tag114)
    

    ...I end up getting results from both tag and userprofile. I can only assume that's a problem with Whoosh, but I can't say for sure. We should probably contact Haystack and/or Whoosh about it.

    At any rate, you can avoid this problem without altering haystack by setting this:

    HAYSTACK_LIMIT_TO_REGISTERED_MODELS = False