简体   繁体   English

如何从外部脚本更新 haystack 的索引?

[英]How to update_index for haystack from a external script?

I am using Django Haystack with ElasticSearch backend for my search page.我在搜索页面中使用带有 ElasticSearch 后端的 Django Haystack。 I am using MongoDB as my Database.我使用 MongoDB 作为我的数据库。
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我的 Web 应用程序使用外部脚本使用 pymongo 更改后端数据库中的字段
My database has 2 fields (Files , Analysis).我的数据库有 2 个字段(文件、分析)。
The third party script runs and changes the Analysis field to True or False.第三方脚本运行并将分析字段更改为 True 或 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.但是当我搜索 Analysis Field 时,(比如我搜索 True/False )它没有列出这个当前更新的 Analysis ,尽管它已经更新了。

For example例如

Search : filename搜索:文件名
Result : filename True结果:文件名真

Search : True搜索 : 真
Result : No results found结果:未找到结果

It is working only after I update_index它仅在我 update_index 后工作

WHAT I TRIED我的尝试
So I figured out that I have to update_index.所以我发现我必须更新_index。 But I don't know how to update from an external python script.但我不知道如何从外部 python 脚本更新。
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.当我检查外部脚本中可用的管理命令时,它没有列出 haystack 命令。

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.这里没有显示 haystack 子命令,这与我在终端中运行的相反。

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搜索 :True
Results : filename True结果:文件名真

How Do I achieve this ?我如何实现这一目标?
How do I update_index from an external script?如何从外部脚本 update_index?
Any Other IDEAS ?任何其他想法?

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在 django 文档中阅读更多内容: https : //docs.djangoproject.com/en/dev/ref/django-admin/#running-management-commands-from-your-code

You can enable real time updating by adding this to settings.py:您可以通过将其添加到 settings.py 来启用实时更新:

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

It will as similar as ruinning update command, automatically trigger if any update in mapped indexes.它将类似于破坏update命令,如果映射索引中有任何更新,则自动触发。

More details here:更多细节在这里:

http://django-haystack.readthedocs.io/en/v2.4.1/signal_processors.html#realtime-realtimesignalprocessor 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:如果重新索引可能需要一些时间,您应该使用队列来防止请求/响应周期受到阻碍,这里建议使用 celery 等可能的解决方案:

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

The most possible case is that you are trying to call command not from the virtual environment, where django is.最可能的情况是您试图调用命令而不是从 django 所在的虚拟环境中调用。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM