forked from happyjared/python-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes.py
More file actions
160 lines (138 loc) · 4.26 KB
/
Copy pathes.py
File metadata and controls
160 lines (138 loc) · 4.26 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
import logging
from elasticsearch import Elasticsearch
log = logging.getLogger()
# @See https://github.com/elastic/elasticsearch/issues/21134
# _id is not configurable @See https://discuss.elastic.co/t/elsticsearch5-6-id-is-not-configurable/115496
class Es(object):
""" Example
doc_mapping = '''{
"properties": {
"positionId": {
"type": "keyword"
},
"cityId": {
"type": "integer"
},
"jobId": {
"type": "integer"
},
"location": {
"type": "geo_point"
},
"sourceFrom": {
"type": "integer"
},
"keyword":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"postJobTime": {
"type": "date"
}
}
}'''
"""
def __init__(self, host='localhost', port=9200, index=None, doc=None, mapping=None):
log.info('Es Init index: %s , document: %s , mapping %s', index, doc, mapping)
self.els = Elasticsearch(hosts="{0}:{1}".format(host, port))
ping_res = self.els.ping()
log.info('Es Ping : %s', ping_res)
self.doc = doc
self.index = index
if index and not self.els.indices.exists(index=index):
# 创建Index
create_index = self.els.indices.create(index=index)
log.info('Es create_index : %s', create_index)
if doc and not self.els.indices.exists_type(index=index, doc_type=doc):
# 创建Document with mapping
# @See: http://cwiki.apachecn.org/pages/viewpage.action?pageId=7372806
put_mapping = self.els.indices.put_mapping(index=index, doc_type=doc, body=mapping)
log.info('Es put_mapping : %s', put_mapping)
""" Example
json_data = {
'city_id': 2,
'location': {
'lat': 23.1686122210,
'lon': 113.4204483032
},
'source_from': 1,
'keyword': 'xxx'
}
"""
def put_data(self, data_body, index=None, doc=None, _id=None):
""" 添加数据到ES
:param data_body: 数据
:param index: 索引,可选
:param doc: 文档,可选
:param _id: 文档id, 可选
:return:
"""
return self.els.index(index=index if index else self.index,
doc_type=doc if doc else self.doc,
id=_id, body=data_body)
def search_id(self, _id, index=None, doc=None):
""" 根据id查找数据
:param _id: 指定id
:param index: 索引
:param doc: 文档
:return:
"""
return self.els.get(index=index if index else self.index,
doc_type=doc if doc else self.doc,
id=_id)
""" Example:
search_body = '''{
"query": {
"bool": {
"must": [
{
"match": {
"city_id": 2
}
},
{
"match": {
"source_from": 1
}
},
{
"match": {
"keyword": ""
}
}
],
"filter": {
"geo_distance": {
"distance": "1.9km",
"location": {
"lat": 23.1680598602,
"lon": 113.4022521973
}
}
}
}
}
}'''
"""
def search_body(self, body=None, index=None, doc=None):
""" 自定义DSL查询
:param body: DSL Body
:param index: 索引
:param doc: 文档
:return:
"""
if body is None:
body = {"query": {"match_all": {}}}
return self.els.search(index=index if index else self.index,
doc_type=doc if doc else self.doc,
body=body)
def remove_id(self, _id, index=None, doc=None):
""" 删除ES数据
:param _id: 文档id
:param index: 索引,可选
:param doc: 文档,可选
:return:
"""
return self.els.delete(index=index if index else self.index,
doc_type=doc if doc else self.doc, id=_id)