如何使用Python删除Elasticsearch索引?

cig3rfwq  于 6个月前  发布在  ElasticSearch
关注(0)|答案(5)|浏览(60)

我有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

ukdjmx9f

ukdjmx9f1#

对于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])

bvjxkvbb

bvjxkvbb2#

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

ClassName._index.delete()

字符串
正如文档中所述:
_index属性也是load_mappings方法的所在地,该方法将从elasticsearch更新Index上的Map。如果您使用动态Map并希望类知道这些字段(例如,如果您希望Date字段正确(反)序列化),则这非常有用:

Post._index.load_mappings()

pdtvr36n

pdtvr36n3#

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

from elasticsearch import Elasticsearch
es = Elasticsearch()

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

字符串
(see文档)。

fxnxkyjh

fxnxkyjh4#

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

from elasticsearch_dsl import Index

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

字符串

wbrvyc0a

wbrvyc0a5#

如果您使用的是旧版本的ES 8+:

from elasticsearch import Elasticsearch
es = Elasticsearch(http://localhost:9200)

# Delete
es.indices.delete(index='name_index', ignore=[400, 404])

字符串

相关问题