How to update_index for haystack from a external script?

658 views Asked by At

I am using Django Haystack with ElasticSearch backend for my search page. I am using MongoDB as my Database.
Everything was working fine in my search page.

PROBLEM
My web-application uses an external script for changing a field in the backend database using pymongo
My database has 2 fields (Files , Analysis).
The third party script runs and changes the Analysis field to True or False.

After the script has been run , when I search for the filename , it is showing me the updated Analysis in the results.

But when I search for the Analysis Field , (say I search for True/False ) It is not listing out this currently updated Analysis, though it has been updated.

For example

Search : filename
Result : filename True

Search : True
Result : No results found

It is working only after I update_index

WHAT I TRIED
So I figured out that I have to update_index. But I don't know how to update from an external python script.
I tried running

os.system("python /myapp/manage.py update_index")

I get the error

Unknown command: 'update_index'

When I checked for the management command available from the external script , it is not listing the haystack commands.

os.system("python /myapp/manage.py")
Available subcommands:

[auth]
    #Things under [auth]

[contenttypes]
    #Things under [contenttypes]

[django]
    #Things under [django]

[sessions]
    #Things under [sessions]

[staticfiles]
    #Things under [staticfiles]

No haystack subcommands are being shown here , contrary to what I run in the terminal.

If I run on the terminal

#other subcommands
[haystack]
    build_solr_schema
    clear_index
    haystack_info
    rebuild_index
    update_index

So I expect the result
Search :True
Results : filename True

How Do I achieve this ?
How do I update_index from an external script?
Any Other IDEAS ?

3

There are 3 answers

3
valignatev On

This is how you execute management command from within your code:

from django.core.management import call_command

call_command('update_index', *args, **options)  # args and opions are optional.

Read more in django docs: https://docs.djangoproject.com/en/dev/ref/django-admin/#running-management-commands-from-your-code

0
Pankaj Sharma On

You can enable real time updating by adding this to settings.py:

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

It will as similar as ruinning update command, automatically trigger if any update in mapped indexes.

More details here:

http://django-haystack.readthedocs.io/en/v2.4.1/signal_processors.html#realtime-realtimesignalprocessor

Where the re-indexing is likely to take some time you should use a queue to prevent the request/response cycle being impeded, possible solutions such as celery are suggested here:

http://django-haystack.readthedocs.io/en/v2.4.1/other_apps.html#ref-other-apps

0
Ephmann On

The most possible case is that you are trying to call command not from the virtual environment, where django is. The answers below are right. But if you want to call command your way, you should run something like:

os.system("/path/to/your/venv/bin/python /myapp/manage.py")