首页 文章

Elasticsearch:如何使用python删除索引

提问于
浏览
33

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

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

2 回答

  • 61

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

    ClassName._index.delete()
    

    同样如stated in documentation

    _index属性也是load_mappings方法的主页,该方法将更新来自elasticsearch的索引上的映射 . 如果您使用动态映射并希望类知道这些字段(例如,如果您希望正确(de)序列化日期字段),这非常有用:Post._index.load_mappings()

  • 0

    从文档中,使用此表示法:

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

相关问题