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

2024-03-29 06:46:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Django Haystack和ElasticSearch后端作为我的搜索页面。 我正在使用MongoDB作为我的数据库。
我的搜索页面一切正常。你知道吗

问题
我的web应用程序使用一个外部脚本来使用pymongo更改后端数据库中的字段
我的数据库有2个字段(文件、分析)。
第三方脚本运行并将分析字段更改为True或False。

脚本运行后,当我搜索文件名时,结果中显示更新的分析。你知道吗

但是当我搜索分析字段时(比如我搜索真/假),它没有列出当前更新的分析,尽管它已经更新了。你知道吗

例如

搜索:文件名
结果:filename True

搜索:真
结果:未找到结果

它只有在我更新了索引后才起作用

我试过的
所以我想我必须更新索引。但我不知道如何从外部python脚本进行更新。
我试过跑步

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

我得到了错误

Unknown command: 'update_index'

当我检查外部脚本中可用的管理命令时,它没有列出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]

这里没有显示haystack子命令,这与我在终端中运行的相反。你知道吗

如果我在终端上运行

#other subcommands
[haystack]
    build_solr_schema
    clear_index
    haystack_info
    rebuild_index
    update_index

所以我期待结果
搜索:真
结果:filename True

我如何做到这一点?
如何从外部脚本更新索引?
还有其他想法吗?你知道吗


Tags: 命令脚本数据库trueindexos文件名update
2条回答

您可以通过将此添加到设置.py地址:

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

它将类似于销毁update命令,如果映射索引中有任何更新,它将自动触发。你知道吗

更多详情请参见:

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

如果重新索引可能需要一些时间,您应该使用队列来防止请求/响应周期受阻,这里建议使用芹菜等可能的解决方案:

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

以下是如何从代码中执行管理命令:

from django.core.management import call_command

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

在django文档中阅读更多:https://docs.djangoproject.com/en/dev/ref/django-admin/#running-management-commands-from-your-code

相关问题 更多 >