Elasticsearch:如何使用 python 删除索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35134162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Elasticsearch : How to delete an Index using python
提问by AbtPst
Forgive me if this is quite basic but I have Python 2.7 and Elasticsearch 2.1.1 and I am just trying to delete an index using
请原谅我,如果这是非常基本的,但我有 Python 2.7 和 Elasticsearch 2.1.1,我只是想使用删除索引
es.delete(index='researchtest', doc_type='test')
but this gives me
但这给了我
return func(*args, params=params, **kwargs)
TypeError: delete() takes at least 4 arguments (4 given)
I also tried
我也试过
es.delete_by_query(index='researchtest', doc_type='test',body='{"query":{"match_all":{}}}')
but I get
但我明白了
AttributeError: 'Elasticsearch' object has no attribute 'delete_by_query'
Any idea why? Has the api changed for 2.1.1 for python?
知道为什么吗?2.1.1 的python api 是否已更改?
采纳答案by ferdy
From the docs, use this notation:
从文档中,使用此符号:
from elasticsearch import Elasticsearch
es = Elasticsearch()
es.indices.delete(index='test-index', ignore=[400, 404])
回答by Kasramvd
If you have a document object (model) and you're using elasticsearch-dsl, specially with Python-3.X you can directly call the delete
method of your model's _index
attribute.
如果您有一个文档对象(模型)并且您正在使用 elasticsearch-dsl,特别是使用 Python-3.X,您可以直接调用delete
模型_index
属性的方法。
ClassName._index.delete()
Also as it's stated in documentation:
也正如文档中所述:
The
_index
attribute is also home to the load_mappings method which will update the mapping on the Index from elasticsearch. This is very useful if you use dynamic mappings and want the class to be aware of those fields (for example if you wish the Date fields to be properly (de)serialized):Post._index.load_mappings()
该
_index
属性也是 load_mappings 方法的所在地,该方法将从 elasticsearch 更新索引上的映射。如果您使用动态映射并希望类知道这些字段(例如,如果您希望日期字段正确(反)序列化),这将非常有用:Post._index.load_mappings()