There are 2 options to remove single object.
You can remove or update single object with remove_object
(Django Haystack Docs) or update_object
(Django Haystack Docs) are the methods of class SearchIndex
You can provide an instance object and which connection should be used.
SearchIndex.remove_object(self, instance, using=None, **kwargs)
Remove an object from the index. Attached to the class’s post-delete hook.
SearchIndex.update_object(self, instance, using=None, **kwargs)
Update the index for a single object. Attached to the class’s post-save hook.
If using
is provided, it specifies which connection should be used. >Default relies on the routers to decide which backend should be used.
Example:
from myapp.search_indexes import MyIndex
# Get the object you want to delete or update
instance = YourModel.objects.get(id=id)
# settings.HAYSTACK_CONNECTIONS / name of your index
using = "myindex_name"
# Remove object
MyIndex().remove_object(instance, using)
# Update object
MyIndex().update_object(instance, using)
You can remove single object through SearchBackend.remove()
Here is some example:
from haystack import connections as haystack_connections
# Get the object you want to delete or update
instance = YourModel.objects.get(id=id)
# Get all Names/keys of your indexes / settings.HAYSTACK_CONNECTIONS
backend_names = haystack_connections.connections_info.keys()
# Get key of connection for your object
using = backend_names[0]
# Get the backend
backend = haystack_connections[using].get_backend()
# To remove object
backend.remove(instance)