python使用elasticsearch模块操作elasticsearch

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')

猜你喜欢

转载自www.cnblogs.com/bainianminguo/p/10419848.html
今日推荐