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
# coding=utf-8
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan, bulk
from django.conf import settings
from pytz import timezone
def es_index_adapt(index_prefix, doc_type, rw=None):
"""get the adapted index name
"""
assert rw in [None, 'read', 'write']
index = '-'.join((index_prefix, doc_type))
if rw:
index = '-'.join((index, rw))
return index
def tzlc(dt, truncate_to_sec=True):
if dt is None:
return None
if truncate_to_sec:
dt = dt.replace(microsecond=0)
return timezone(settings.TIME_ZONE).localize(dt)
def to_epoch(dt):
return (dt - datetime(year=1970, month=1, day=1, tzinfo=timezone('UTC'))).total_seconds()
def reindex(es, old_index='', new_index='', chunk_size=500):
docs = scan(client=es, scroll='1m', index=old_index)
def _change_op(docs):
for d in docs:
d['_op_type'] = 'create'
d['_index'] = new_index
yield d
return bulk(client=es, actions=_change_op(docs), chunk_size=chunk_size)
def create_index(es, index_prefix, doc_type):
cl = es.indices
index = es_index_adapt(
index_prefix=index_prefix,
doc_type=doc_type
)
if not cl.exists(index=index):
cl.create(index=index)
return True
else:
return False
def delete_index(es, index_prefix, doc_type):
cl = es.indices
index = es_index_adapt(
index_prefix=index_prefix,
doc_type=doc_type
)
if cl.exists(index=index):
cl.delete(index=index)
return True
else:
return False
def load_mapping(doc_type):
import os.path
import re
import json
mapping_file_path = os.path.join(
os.path.dirname(__file__),
'..', 'mapping', '%s.json' % (doc_type,))
mapping = ''
with open(mapping_file_path, 'r') as f:
for line in f:
# 去掉注释
mapping += re.sub(r'//.*$', '', line)
mapping = json.loads(mapping)
return mapping
def put_mapping(es, index_prefix, doc_type, mapping, delete=False):
cl = es.indices
index = es_index_adapt(
index_prefix=index_prefix,
doc_type=doc_type,
rw='write'
)
# delete mapping first it type exists
if cl.exists_type(index=index, doc_type=doc_type) and delete:
cl.delete_mapping(index=index, doc_type=doc_type)
# there is no "index_analyzer" option in ver 2.x
# but only "analyzer" and "search_analyzer"
def _mapping_opt_modify(o):
if 'analyzer' in o and 'search_analyzer' in o:
o['index_analyzer'] = o['analyzer']
del o['analyzer']
for v in o.values():
if isinstance(v, dict):
_mapping_opt_modify(v)
return o
# mapping body needs to be modified
# as there is a difference between ver 1.x and 2.x
def _mapping_modify(es, doc_type, mp):
es_info = es.info()
es_ver = es_info['version']['number']
from distutils.version import LooseVersion
if LooseVersion(es_ver) < LooseVersion('2.0.0'):
return {
doc_type: _mapping_opt_modify(mp)
}
else:
return mp
import copy
mapping_copy = copy.deepcopy(mapping)
mapping_copy = _mapping_modify(es, doc_type, mapping_copy)
return cl.put_mapping(index=index, doc_type=doc_type, body=mapping_copy)
def alias_shift(es, alias, old_index, new_index):
cl = es.indices
if cl.exists(index=old_index) and cl.exists(index=new_index):
q = {
'actions': [
{'remove': {'index': old_index, 'alias': alias}},
{'add': {'index': new_index, 'alias': alias}},
]
}
cl.update_aliases(body=q)
def init_alias(es, index_prefix, doc_type):
cl = es.indices
index = es_index_adapt(
index_prefix=index_prefix,
doc_type=doc_type
)
index_read = es_index_adapt(
index_prefix=index_prefix,
doc_type=doc_type,
rw='read',
)
index_write = es_index_adapt(
index_prefix=index_prefix,
doc_type=doc_type,
rw='write',
)
content = {
'actions': [
{'add': {'index': index, 'alias': index_read}},
{'add': {'index': index, 'alias': index_write}},
]
}
cl.update_aliases(body=content)
def area_tag_id_filter(prefix_list, value):
return {
'bool': {
'should': [
{'term': {prefix + field: value}}
for prefix in prefix_list
for field in ['city_tag_id',
'city_province_tag_id',
'city_province_country_tag_id',
]
]
}
}
__es_instance = None
def get_es_instance():
global __es_instance
if __es_instance is not None:
return __es_instance
init_args = {
'sniff_on_start': False,
'sniff_on_connection_fail': False,
}
new_hosts = settings.ES7_HOSTS
__es_instance = Elasticsearch(hosts=new_hosts, http_auth=(settings.HTTP_AUTH_NAME, settings.HTTP_AUTH_PWD), **init_args)
return __es_instance