Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add individual objects to django haystack?

I have a search index that I have created using Solr. I want to add individual django objects to the search index.

To remove objects from the solr database we use remove_object.

some = SomFooModel.objects.get(pk=1)
foo = FooIndex()
foo.remove_object(some) #This works

To add it, is there something like add_object or a work around here ?

What I want is.

foo.add_object(some). # there is no such thing 

This also does not work. It does not add the object to index.

foo.update_object(some)

I have tried reading the django-haystack documentation but there seems to be nothing that might help.

like image 360
Akash Deshpande Avatar asked Jun 05 '15 20:06

Akash Deshpande


1 Answers

I did not read the documentation well enough as a result I messed up on the QuerySet part.

foo.update_object(some)

The above does add the object to the index. Its just that I was not searching for it properly. I was searching for the object after removing it in the following way.

SearchQuerySet().filter(foo=some.foo)

This gave a empty query set always.

SearchQuerySet().models(SomFooModel).filter(foo = some.foo)

This gives the correct result.

Reference

like image 60
Akash Deshpande Avatar answered Nov 16 '22 05:11

Akash Deshpande