1、创建索引
命令如下
from elasticsearch import Elasticsearch es = Elasticsearch([{"host":"10.87.6.3","port":9200},]) s = "test" + "python" try: ret = es.indices.create(index=s) except Exception as e: print(e) else: print("创建成果") print(ret)
返回的结果如下,证明创建成果
{‘acknowledged‘: True, ‘shards_acknowledged‘: True, ‘index‘: ‘testpython‘}
返回的结果如下,证明创建失败
RequestError(400, ‘resource_already_exists_exception‘, ‘index [testpython/yEzYUZEiTjaZWxtN0RM_Uw] already exists‘)
2、删除索引
代码如下
try: ret = es.indices.delete(index=s) except Exception as e: print(e) else: print("删除成果") print(ret)
返回结果,证明删除成果
{‘acknowledged‘: True}
返回结果,证明删除失败
NotFoundError(404, ‘index_not_found_exception‘, ‘no such index‘)
原文地址:https://www.cnblogs.com/bainianminguo/p/10419848.html
时间: 2024-10-08 09:28:57