如何解决 django HayStack, elasticsearch update_index 错误?

how to solve django HayStack, elasticsearch update_index error?


我使用 django web 框架集成了 elasticsearch 和 haystack。现在我想删除不存在的 使用 call_command('update_index --remove') 函数来自 Elasticsearch 索引的数据。

我的问题是当我 运行 update_index --remove 我产生以下错误:

Indexing 6 notes
[ERROR/MainProcess] Error updating core using default 
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/haystack/management/commands/update_index.py", 
line 230, in handle
self.update_backend(label, using)
File "/usr/local/lib/python3.6/dist-packages/haystack/management/commands/update_index.py", 
line 308, in update_backend
index_total = SearchQuerySet(using=backend.connection_alias).models(model).count()
File "/usr/local/lib/python3.6/dist-packages/haystack/query.py", line 522, in count
return len(self)
File "/usr/local/lib/python3.6/dist-packages/haystack/query.py", line 86, in __len__
self._result_count = self.query.get_count()
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/__init__.py", line 619, in 
get_count
self.run()
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/elasticsearch_backend.py", 
line 951, in run
results = self.backend.search(final_query, **search_kwargs)
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/__init__.py", line 33, in 
wrapper
return func(obj, query_string, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/elasticsearch_backend.py", 
line 524, in search
_source=True)
File "/usr/local/lib/python3.6/dist-packages/elasticsearch/client/utils.py", line 84, in 
_wrapped
return func(*args, params=params, **kwargs)
TypeError: search() got an unexpected keyword argument 'doc_type'
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 381, 
in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 375, 
in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 316, in 
run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 353, in 
execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/dist-packages/haystack/management/commands/update_index.py", 
line 230, in handle
self.update_backend(label, using)
File "/usr/local/lib/python3.6/dist-packages/haystack/management/commands/update_index.py", 
line 308, in update_backend
index_total = SearchQuerySet(using=backend.connection_alias).models(model).count()
File "/usr/local/lib/python3.6/dist-packages/haystack/query.py", line 522, in count
return len(self)
File "/usr/local/lib/python3.6/dist-packages/haystack/query.py", line 86, in __len__
self._result_count = self.query.get_count()
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/__init__.py", line 619, in 
get_count
self.run()
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/elasticsearch_backend.py", 
line 951, in run
results = self.backend.search(final_query, **search_kwargs)
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/__init__.py", line 33, in 
wrapper
return func(obj, query_string, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/haystack/backends/elasticsearch_backend.py", 
line 524, in search
_source=True)
File "/usr/local/lib/python3.6/dist-packages/elasticsearch/client/utils.py", line 84, in 
_wrapped
return func(*args, params=params, **kwargs)
TypeError: search() got an unexpected keyword argument 'doc_type'

我正在使用:
django-haystack==2.8.1
elasticsearch==7.1.0
elasticsearch-7.4.0 => 服务器

注:
不推荐

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

这个方法不适合我。我没有使用 django 进行数据插入。仅用于过滤和删除目的。

update_index 同样的问题 --age=24

我认为问题在于您使用的是 Elasticsearch 7,并且 Haystack only supports versions up to 5:

Haystack currently only supports Elasticsearch 1.x and 2.x. Elasticsearch 5.x is not supported yet, if you would like to help, please see #1383.

我看到两个选项:

  • 保留 Haystack 并使用 Elasticsearch 5 服务器,
  • 抛弃 Haystack 并使用“vanilla”Elasticsearch python client 并手动执行查询(也可以执行纯 HTTP 请求)。

您看到的错误:

TypeError: search() got an unexpected keyword argument 'doc_type'

与 Elasticsearch 7 中删除了文档类型有关。