Elasticsearch教程之六:Elasticsearch Python客户端的使用


Elasticsearch可以说是企业级搜索应用的最佳选择。相较于传统的关系型数据库,Elasticsearch更适合于存储海量数据并对其进行全文检索和数据分析(聚合)。下面介绍如何在Python中使用ES。

Elasticsearch教程
Elasticsearch教程

Elasticsearch系列教程

安装

bash
pip install elasticsearch

创建索引

Python
from elasticsearch import Elasticsearch
es = Elasticsearch()

# ignore 400 cause by IndexAlreadyExistsException when creating an index
es.indices.create(index='test-index', ignore=400)

# ignore 404 and 400
es.indices.delete(index='test-index', ignore=[400, 404])

读取数据

Python
from datetime import datetime
from elasticsearch import Elasticsearch

es = Elasticsearch()
resp = es.get(index="tutorial", id="HnOR9HkBkMWsw4mVbe5G")
print(resp)

添加数据

Python
from elasticsearch import Elasticsearch

es = Elasticsearch()
new_employee = {
    "name": "George Harding",
    "sex": "male",
    "age": 34,
    "years": 10
}
response = es.index(
    index = 'tutorial',
    body = new_employee
)

文章作者: 逻思
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明来源 逻思 !