3

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 Answers3

1

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

valignatev
  • 6,020
  • 8
  • 37
  • 61
  • Thanks for answering, I am not able to call rebuild_index or update_index , I am getting a migration error, though I don't get that when running from terminal.I tried doing models.get by even it threw the same error ** File "C:\Python36\lib\site-packages\djongo\sql2mongo\query.py", line 287, in _align_results ret.append(doc[selected.column]) KeyError: 'Types' ... djongo.sql2mongo.MigrationError: Types** Types is a field which tells the type of file , its a foreign key field – No Reveal Name Jun 29 '19 at 09:53
  • It's hard to read error messages from the comment. Maybe you can update your question with the code you're executing and the error you're getting in form of formatted traceback. – valignatev Jun 29 '19 at 10:27
  • Hey.. I just found out the error. I tried using Models in the external script and it threw me the same error which it threw when I did update_index. So I found that update_index must also do the same model.save method. So in my forms.py when I search for a field and return the result I just do model.save method. Thanks for your answer it led me into this – No Reveal Name Jun 29 '19 at 15:46
0

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

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50
0

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")
Ephmann
  • 1
  • 1