RealTimeSearchIndex and m2m_changed signal

269 views
Skip to first unread message

bwilliamson

unread,
Aug 5, 2010, 10:49:02 AM8/5/10
to django-haystack
Hi,
We are using the RealTimeSearchIndex and notice that it's indexing
everything but m2m fields. Are there any plans to support the new
m2m_changed signal to index m2m fields via the RealTimeSearchIndex?

Brian

Jeremy Bowers

unread,
Aug 5, 2010, 6:10:53 PM8/5/10
to django-haystack
We had this problem too, and hacked together a clumsy scheme to
reindex on a post_save signal. It's running the index command twice,
but it's a start.

+1 for native solution to this.

Daniel Lindsley

unread,
Aug 5, 2010, 10:11:13 PM8/5/10
to django-...@googlegroups.com
Brian,


Many to many is not planned, simply because search engines are not
relational, so primary keys don't mean much. It's far better to
denormalize related data onto the parent (usually in the ``text``
template) or to use custom ``prepare_FOO`` methods in conjunction with
a ``MultiValueField`` to grab the primary keys/slugs/names/whatever
makes sense for your related data. The archives of this mailing list
has many examples (search for "tags").

As for Jeremy's solution & the m2m_changed signal, the problem lies
in backward-compatibility, since Django 1.0 & 1.1 don't have it (both
of which Haystack supports). It'll be a consideration going forward
but I don't feel comfortable building it in at the moment. And given
that it's simply a subclass of ``RealTimeSearchIndex`` with a couple
extra lines of code, it should be doable for your needs.

Finally, adding an issue to GitHub would help me remember to
implement it in the future
(http://github.com/toastdriven/django-haystack/issues).


Daniel

> --
> You received this message because you are subscribed to the Google Groups "django-haystack" group.
> To post to this group, send email to django-...@googlegroups.com.
> To unsubscribe from this group, send email to django-haysta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-haystack?hl=en.
>
>

Danny Adair

unread,
Aug 5, 2010, 4:30:48 PM8/5/10
to django-...@googlegroups.com

Today I was going to ask the list about that same thing. :-)

Also using RealTimeSearchIndex, I've got calculated values hanging off
m2m and foreignkey relationships which need to be updated when the
other object changes. (for example an account balance which is
calculated from credit/debit entries having the account as their
foreignkey - need to update the balance when an entry is
added/removed/changed)

Can I hook into the signal and update manually?

Cheers,
Danny

Daniel Lindsley

unread,
Aug 10, 2010, 1:17:35 AM8/10/10
to django-...@googlegroups.com
Danny,


Hooking in to related models is usually as simple as overriding
``_setup_save``
(http://github.com/toastdriven/django-haystack/blob/master/haystack/indexes.py#L211-215)
in your ``SearchIndex`` subclass. Something like:

====
from django.db.models import signals
from haystack import indexes
from haystack import site
from myapp.models import MainModel, M2MModel


class MySearchIndex(indexes.RealTimeSearchIndex):
# ... Fields here...
# Then...

def _setup_save(self, model):
super(MySearchIndex, self)._setup_save(model)
signals.post_save.connect(self.update_object, sender=M2MModel)

====

Hope that helps.


Daniel

Danny Adair

unread,
Aug 11, 2010, 6:46:14 AM8/11/10
to django-...@googlegroups.com
Thanks Daniel,

On Tue, Aug 10, 2010 at 17:17, Daniel Lindsley <pola...@gmail.com> wrote:
>[...]


> class MySearchIndex(indexes.RealTimeSearchIndex):
>    # ... Fields here...
>    # Then...
>
>    def _setup_save(self, model):
>        super(MySearchIndex, self)._setup_save(model)
>        signals.post_save.connect(self.update_object, sender=M2MModel)

I've tried that and it seems as if the fields are attempted to be
looked up in the other instance (it's got a foreign key to the main
model, not m2m).

Django Version: 1.2.1 SVN-13303
Exception Type: SearchFieldError
Exception Value:

The model '<RelatedModel: MainModel #13786 Related Model>' does not
have a model_attr 'my_main_model_attr'.

Exception Location:
/home/danny/environments/my.env/lib/python2.6/site-packages/haystack/fields.py
in prepare, line 69
Python Executable: /home/danny/environments/my.env/bin/python2.6
Python Version: 2.6.5

I get this when saving the RelatedModel instance, when I expected
"my_main_model_attr" to be looked up for index update from the main
model?

Cheers,
Danny

Danny Adair

unread,
Aug 29, 2010, 12:45:32 AM8/29/10
to django-...@googlegroups.com
Just in case someone else gets bitten by this:
I was naming the search index fields the same as the model field they
were indexing.
myfield = CharField(model_attr='myfield')

However what wasn't so clever was to do the same with related lookups:
relatedfield__otherattr = CharField(model_attr='relatedfield__otherattr')

Don't use double underscores :-)

Cheers,
Danny

--
Kind regards,

Danny W. Adair
Director
Unfold Limited
New Zealand

Talk:       +64 - 9 - 9555 101
Fax:        +64 - 9 - 9555 111
Write:      danny...@unfold.co.nz
Browse:     www.unfold.co.nz
Visit/Post: 253 Paihia Road, RD 2, Kawakawa 0282, New Zealand

"We are what we repeatedly do. Excellence, then, is not an act but a habit."

==============================
Caution
The contents of this email and any attachments contain information
which is CONFIDENTIAL to the recipient. If you are not the intended
recipient, you must not read, use, distribute, copy or retain this
email or its attachments. If you have received this email in error,
please notify us immediately by return email or collect telephone call
and delete this email.  Thank you.  We do not accept any
responsibility for any changes made to this email or any attachment
after transmission from us.
==============================

Reply all
Reply to author
Forward
0 new messages