-
Notifications
You must be signed in to change notification settings - Fork 841
Expand file tree
/
Copy pathelasticsearch_demo.py
More file actions
351 lines (299 loc) · 10.1 KB
/
elasticsearch_demo.py
File metadata and controls
351 lines (299 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Elasticsearch 极简交互示例
包括数据写入和查询功能
"""
from elasticsearch import Elasticsearch
import json
from datetime import datetime
from typing import Dict, List, Any
class ElasticsearchDemo:
def __init__(self, host: str = "localhost", port: int = 9200):
"""
初始化 Elasticsearch 客户端
Args:
host: Elasticsearch 服务器地址
port: Elasticsearch 服务器端口
"""
# Elasticsearch 9.x 版本需要不同的配置
self.es = Elasticsearch(
[f"http://{host}:{port}"],
verify_certs=False,
ssl_show_warn=False,
request_timeout=30
)
self.index_name = "demo_index"
def check_connection(self) -> bool:
"""检查 Elasticsearch 连接状态"""
try:
if self.es.ping():
print("Elasticsearch 连接成功")
return True
else:
print("Elasticsearch 连接失败")
return False
except Exception as e:
print(f"连接错误: {e}")
return False
def create_index(self) -> bool:
"""创建索引"""
try:
if not self.es.indices.exists(index=self.index_name):
# 定义索引映射
mapping = {
"mappings": {
"properties": {
"title": {"type": "text"},
"content": {"type": "text"},
"author": {"type": "keyword"},
"tags": {"type": "keyword"},
"created_at": {"type": "date"},
"views": {"type": "integer"},
"is_published": {"type": "boolean"}
}
}
}
self.es.indices.create(index=self.index_name, **mapping)
print(f"索引 '{self.index_name}' 创建成功")
else:
print(f"索引 '{self.index_name}' 已存在")
return True
except Exception as e:
print(f"创建索引失败: {e}")
return False
def insert_document(self, doc: Dict[str, Any], doc_id: str = None) -> bool:
"""
插入单个文档
Args:
doc: 要插入的文档数据
doc_id: 文档ID,如果不提供则自动生成
Returns:
bool: 插入是否成功
"""
try:
# 添加时间戳
doc["created_at"] = datetime.now().isoformat()
if doc_id:
result = self.es.index(index=self.index_name, id=doc_id, document=doc)
else:
result = self.es.index(index=self.index_name, document=doc)
print(f"文档插入成功,ID: {result['_id']}")
return True
except Exception as e:
print(f"插入文档失败: {e}")
return False
def bulk_insert(self, docs: List[Dict[str, Any]]) -> bool:
"""
批量插入文档
Args:
docs: 要插入的文档列表
Returns:
bool: 批量插入是否成功
"""
try:
actions = []
for i, doc in enumerate(docs):
# 添加时间戳
doc["created_at"] = datetime.now().isoformat()
action = {
"index": {
"_index": self.index_name,
"_source": doc
}
}
actions.append(action)
# 执行批量插入
result = self.es.bulk(operations=actions)
if result["errors"]:
print("批量插入部分失败")
for item in result["items"]:
if "error" in item["index"]:
print(f"错误: {item['index']['error']}")
else:
print(f"批量插入成功,共 {len(docs)} 个文档")
return True
except Exception as e:
print(f"批量插入失败: {e}")
return False
def search_documents(self, query: Dict[str, Any], size: int = 10) -> List[Dict[str, Any]]:
"""
搜索文档
Args:
query: 搜索查询
size: 返回结果数量
Returns:
List[Dict]: 搜索结果列表
"""
try:
result = self.es.search(
index=self.index_name,
query=query["query"],
size=size
)
hits = result["hits"]["hits"]
print(f"找到 {result['hits']['total']['value']} 个结果")
documents = []
for hit in hits:
doc = hit["_source"]
doc["_id"] = hit["_id"]
doc["_score"] = hit["_score"]
documents.append(doc)
return documents
except Exception as e:
print(f"搜索失败: {e}")
return []
def get_document_by_id(self, doc_id: str) -> Dict[str, Any]:
"""
根据ID获取文档
Args:
doc_id: 文档ID
Returns:
Dict: 文档数据
"""
try:
result = self.es.get(index=self.index_name, id=doc_id)
doc = result["_source"]
doc["_id"] = result["_id"]
return doc
except Exception as e:
print(f"获取文档失败: {e}")
return {}
def delete_document(self, doc_id: str) -> bool:
"""
删除文档
Args:
doc_id: 文档ID
Returns:
bool: 删除是否成功
"""
try:
result = self.es.delete(index=self.index_name, id=doc_id)
print(f"文档 {doc_id} 删除成功")
return True
except Exception as e:
print(f"删除文档失败: {e}")
return False
def get_index_stats(self) -> Dict[str, Any]:
"""获取索引统计信息"""
try:
stats = self.es.indices.stats(index=self.index_name)
return stats["indices"][self.index_name]
except Exception as e:
print(f"获取统计信息失败: {e}")
return {}
def main():
"""主函数 - 演示 Elasticsearch 基本操作"""
print("Elasticsearch 极简交互示例")
print("=" * 50)
# 初始化客户端
es_demo = ElasticsearchDemo()
# 检查连接
if not es_demo.check_connection():
print("请确保 Elasticsearch 服务正在运行")
return
# 创建索引
es_demo.create_index()
# 示例数据
sample_docs = [
{
"title": "Python 编程入门",
"content": "Python 是一种简单易学的编程语言,适合初学者学习。",
"author": "张三",
"tags": ["Python", "编程", "入门"],
"views": 100,
"is_published": True
},
{
"title": "Elasticsearch 基础教程",
"content": "Elasticsearch 是一个分布式搜索和分析引擎。",
"author": "李四",
"tags": ["Elasticsearch", "搜索", "数据库"],
"views": 150,
"is_published": True
},
{
"title": "机器学习实战",
"content": "机器学习是人工智能的一个重要分支。",
"author": "王五",
"tags": ["机器学习", "AI", "算法"],
"views": 200,
"is_published": False
}
]
print("\n插入示例数据")
print("-" * 30)
# 插入单个文档
es_demo.insert_document(sample_docs[0], "doc_1")
# 批量插入文档
es_demo.bulk_insert(sample_docs[1:])
print("\n搜索示例")
print("-" * 30)
# 1. 全文搜索
print("1. 全文搜索 'Python':")
query = {
"query": {
"match": {
"content": "Python"
}
}
}
results = es_demo.search_documents(query)
for doc in results:
print(f" - {doc['title']} (作者: {doc['author']})")
# 2. 精确匹配
print("\n2. 精确匹配作者 '张三':")
query = {
"query": {
"term": {
"author": "张三"
}
}
}
results = es_demo.search_documents(query)
for doc in results:
print(f" - {doc['title']}")
# 3. 范围查询
print("\n3. 浏览量大于 120 的文章:")
query = {
"query": {
"range": {
"views": {
"gte": 120
}
}
}
}
results = es_demo.search_documents(query)
for doc in results:
print(f" - {doc['title']} (浏览量: {doc['views']})")
# 4. 布尔查询
print("\n4. 已发布且包含 '搜索' 标签的文章:")
query = {
"query": {
"bool": {
"must": [
{"term": {"is_published": True}},
{"term": {"tags": "搜索"}}
]
}
}
}
results = es_demo.search_documents(query)
for doc in results:
print(f" - {doc['title']} (标签: {doc['tags']})")
print("\n获取文档详情")
print("-" * 30)
# 根据ID获取文档
doc = es_demo.get_document_by_id("doc_1")
if doc:
print(f"文档详情: {json.dumps(doc, ensure_ascii=False, indent=2)}")
print("\n索引统计信息")
print("-" * 30)
stats = es_demo.get_index_stats()
if stats:
print(f"文档总数: {stats['total']['docs']['count']}")
print(f"索引大小: {stats['total']['store']['size_in_bytes']} 字节")
print("\n演示完成!")
if __name__ == "__main__":
main()