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

Elasticsearch教程
Elasticsearch系列教程
- Elasticsearch教程之一:介绍
- Elasticsearch教程之二:索引,分词及映射
- Elasticsearch教程之三:API
- Elasticsearch教程之四:AWS OpenSearch
- Elasticsearch教程之五:Opensearch Node.js客户端的使用
- Elasticsearch教程之六:Elasticsearch Python客户端的使用
- Elasticsearch教程之七:在OpenSearch中使用聚合实现Facet
安装
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
)