-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathctrl_elasticsearch.py
75 lines (60 loc) · 2.42 KB
/
ctrl_elasticsearch.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2020/5/27 19:55
# @Author : way
# @Site :
# @Describe: 操作 elasticsearch 官方文档 https://elasticsearch-py.readthedocs.io/en/master/
from elasticsearch import Elasticsearch, helpers
es = Elasticsearch(["127.0.0.1:9200"])
index_name = 'test'
type_name = 'human'
"""
print(es.ping()) # 测试es是否启动
print(es.info()) # 打印es信息
print(es.cluster.health()) # 打印集群健康信息
print(es.cluster.client.info()) # 当前节点信息
print(es.cat.indices()) # 索引信息 实例es的 cat 属性可以得到更多信息
"""
# 创建index 新增数据时,索引不存在时,也会被自动创建
response = es.indices.create(index=index_name, ignore=400) # 已存在则ignore
print(response)
data = {'name': '肥仔', 'age': 28}
# 新增数据(指定id, id已存在则报错)
response = es.create(index=index_name, doc_type=type_name, body=data, id=1)
print(response)
# 新增数据(自动生成id)
response = es.index(index=index_name, doc_type=type_name, body=data)
print(response)
# 新增/修改数据(id存在则更新,不存在则新增)
response = es.index(index=index_name, doc_type=type_name, body=data, id=1)
print(response)
# 查指定index、type、id数据
response = es.get(index=index_name, doc_type=type_name, id=1)
print(response['_source'])
# 条件查询
query = {
'query': {'match': {'age': 28}}
}
response = es.search(index=index_name, doc_type=type_name, body=query)
# response = es.search() # 也可以不加任何条件..
print(response['hits'])
# 删除指定id数据
response = es.delete(index=index_name, doc_type=type_name, id=1, ignore=404) # id不存在则ignore
print(response)
# 删除指定index、type下的所有数据
query_all = {'query': {'match_all': {}}} # 查询所有数据
response = es.delete_by_query(index=index_name, doc_type=type_name, body=query_all, ignore=409) # 查询为空则ignore
print(response)
# 删除index
response = es.indices.delete(index=index_name, ignore=404) # 索引不存在则ignore
print(response)
# 批量操作 写入
actions = []
for i in range(9):
action = {'_op_type': 'index', # 操作 index update create delete
'_index': index_name, # index
'_id': i, # 不指定id,则自动生成id
'_type': type_name, # type
'_source': {'name': '肥仔', 'age': i}}
actions.append(action)
helpers.bulk(es, actions=actions)