繁体   English   中英

Elasticsearch:如何使用 python 删除索引

[英]Elasticsearch : How to delete an Index using python

如果这是非常基本的,请原谅我,但我有 Python 2.7 和 Elasticsearch 2.1.1,我只是想使用删除索引

es.delete(index='researchtest', doc_type='test')

但这给了我

return func(*args, params=params, **kwargs)
TypeError: delete() takes at least 4 arguments (4 given)

我也试过

es.delete_by_query(index='researchtest', doc_type='test',body='{"query":{"match_all":{}}}')

但我明白了

AttributeError: 'Elasticsearch' object has no attribute 'delete_by_query'

知道为什么吗? python 的 2.1.1 的 api 是否已更改?

https://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch.client.IndicesClient.delete

对于 ES 8+ 使用:

from elasticsearch import Elasticsearch
es = Elasticsearch()

es.options(ignore_status=[400,404]).indices.delete(index='test-index')

对于旧版本,请使用此表示法:

from elasticsearch import Elasticsearch
es = Elasticsearch()

es.indices.delete(index='test-index', ignore=[400, 404])

如果您有一个文档对象(模型)并且您正在使用 elasticsearch-dsl,特别是使用 Python-3.X,您可以直接调用模型的_index属性的delete方法。

ClassName._index.delete()

也正如文档中所述

_index属性也是 load_mappings 方法的所在地,该方法将从 elasticsearch 更新索引上的映射。 如果您使用动态映射并希望类知道这些字段(例如,如果您希望 Date 字段被正确(反)序列化),这将非常有用:

 Post._index.load_mappings()

由于在 API 方法中传递传输选项在 Elasticsearch python 客户端 8+ 中已弃用,因此指定应忽略的 HTTP 状态代码的方法(例如,在目标索引不存在的情况下防止错误)将使用Elasticsearch.options()现在:

from elasticsearch import Elasticsearch
es = Elasticsearch()

es.options(ignore_status=[400,404]).indices.delete(index='test-index')

(见文档)。

如果您使用的是 elasticsearch-dsl,请使用

from elasticsearch_dsl import Index

index = Index('test-index')
index.delete(ignore=[400, 404])

暂无
暂无

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

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