ElasticSearch:如何使用python删除索引

z6psavjg  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(5)|浏览(293)

请原谅我,如果这是非常基本的,但我有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

xzabzqsa

xzabzqsa1#

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

nhn9ugyo2#

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

ClassName._index.delete()

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

Post._index.load_mappings()
rjee0c15

rjee0c153#

由于在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文档)。

holgip5t

holgip5t4#

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

from elasticsearch_dsl import Index

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

yfwxisqw5#

如果您使用旧版本ES8+:

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

# Delete

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

相关问题