Searching across multiple models with Haystack 2

690 views
Skip to first unread message

Gustavo Narea

unread,
Nov 12, 2014, 12:32:52 PM11/12/14
to django-...@googlegroups.com
Hello, everybody.

I'm trying to migrate an old search interface to Haystack, but there's one thing I cannot figure out how to do: How can I search across multiple models? I know the documentation covers this, but I'm pretty sure it's incomplete. Or else I'm missing something terribly obvious.

I can only provide a single search form and the search must be done across two models (User and Article). I was hoping I could just normalize both models to a single search index, because the fields are the same (creation date, title and description), but Haystack doesn't support this. So I had to create a base class with the fields and one subclass for each model.

The problem with this is that it forces me to use multiple Solr cores. And somehow I have to bind each Haystack index to its corresponding Solr core, which I tried to solve by creating a custom route class, but failed because I could not work out what to do in MyRoute.for_read() when the following is used: "SearchQuerySet.models(User, Article)" 

Any advice?

Thanks in advance!

Gustavo Narea

unread,
Nov 13, 2014, 6:41:48 AM11/13/14
to django-...@googlegroups.com
Hello, everybody.

I just realized that it'd be impossible to search across cores because Solr  doesn't support it.

I suppose the other option would be to somehow get Haystack to map 2 models to a single index, which is what I wanted to do in the first place.

Does this mean that I cannot use Haystack?

Cheers.

 - Gustavo.

Chris Wilson

unread,
Nov 13, 2014, 7:08:50 AM11/13/14
to django-...@googlegroups.com
Hi Gustavo,

On Thu, 13 Nov 2014, Gustavo Narea wrote:

> I just realized that it'd be impossible to search across cores because
> Solr  doesn't support it.
>
> I suppose the other option would be to somehow get Haystack to map 2
> models to a single index, which is what I wanted to do in the first
> place.
>
> Does this mean that I cannot use Haystack?

No, it's not required that you specify a model to search for, or combine
the two different models into one. Just use a SearchForm instead of a
ModelSearchForm, and it will return all results of any type. If you want
all results from User and Article and nothing else, look at the
implementation of ModelSearchForm and construct your query the same way,
with an OR on the contenttype.

Cheers, Chris.
--
Aptivate | http://www.aptivate.org | Phone: +44 1223 967 838
25 Signet Court, Swanns Road, Cambridge, CB5 8LA, UK

Aptivate is a not-for-profit company registered in England and Wales
with company number 04980791.

Gustavo Narea

unread,
Nov 13, 2014, 9:31:53 AM11/13/14
to django-...@googlegroups.com
Hi, Chris.

Thank you so much for your reply.

I think I understand how to search with either of the two options you mention. However, how can I then index the documents? Based on my basic knowledge about Haystack, I'd have to create an index class, which requires exactly one model.

Cheers.

Chris Wilson

unread,
Nov 13, 2014, 9:41:51 AM11/13/14
to django-...@googlegroups.com
On Thu, 13 Nov 2014, Gustavo Narea wrote:

> I think I understand how to search with either of the two options you
> mention. However, how can I then index the documents? Based on my basic
> knowledge about Haystack, I'd have to create an index class, which
> requires exactly one model.

Make two SearchIndexes, one for each model.

Gustavo Narea

unread,
Nov 13, 2014, 11:21:28 AM11/13/14
to django-...@googlegroups.com
Thanks, Chris! That worked

--
You received this message because you are subscribed to a topic in the Google Groups "django-haystack" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-haystack/b85dAaEKyb0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-haystack+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Gustavo Narea.
Software Developer.
Reply all
Reply to author
Forward
0 new messages