Go on.......
接下来我们来实现ElasticGraphObj类,上面已经得到包含有相关需求的素材list,那么该类的目的是根据得到的material list可以找到相应的设计graph
3. ElasticGraphObj类
- 3.1 __init__方法
def __init__(self,index_name,index_type,ip="127.0.0.1"):
'''
:param index_name: 索引名
:param index_type: 索引类型
:param ip: ip
'''
self.index_name = index_name
self.index_type = index_type
# 无用户名密码状态
self.esObj = Elasticsearch([ip])
# 用户名密码状态
# self.esObj = Elasticsearch([ip],http_auth=('elastic', 'password'),port=9200)
- 3.2 create_index方法
def create_index(self,index_name='stylepatternshow',index_type='stylepatternshow_type'):
'''
创建索引,创建索引名称为stylepatternshow,类型为stylepatternshow_type的索引
:param index_name:
:param index_type:
:return:
'''
# 创建索引
_index_mappings = {
"mappings": {
self.index_type: {
"properties": {
"id": {
"type": "long",
"index": "false"
},
"serial": {
"type": "text", # keyword不会进行分词,text会分词,默认按空格切分
"index": "false" # 不建索引
},
"taskType": {
"type": "long",
"index": "false"
},
"taskParam": {
"type": "text",
"index": True
},
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
if self.esObj.indices.exists(index = self.index_name) is not True:
res = self.esObj.indices.create(index = self.index_name,body = _index_mappings)
print('index is create->',res)
else:
print('index is already exist')
- 3.3 bulk_Index_Data方法
def bulk_Index_Data(self, jsonFile):
'''
用bulk将批量数据存储到es
:return:
'''
with open(jsonFile, 'r') as load_f:
load_json = json.load(load_f)
ACTIONS = []
i = 1
for line in load_json:
if line['status'] == 1 and (line['taskType'] == 500 or line['taskType'] == 501):
taskParam_dict = json.loads(line['taskParam'])
if 'main_groups' in taskParam_dict.keys():
layers_list = taskParam_dict['main_groups'][0]['layers']
serial_set = self.get_singel_serial_set(layers_list)
serial_str = ""
'''为了利用es本身的standard分析器,能够把空格切割,故意构造带空格material serial的字符串'''
for col in serial_set:
serial_str += col+' '
if serial_str == "":
continue
action = {
"_index": self.index_name,
"_type": self.index_type,
"_id": i, # _id 也可以默认生成,不赋值
"_source": {
"id": line['id'],
"serial": line['serial'],
"taskType":line['taskType'],
"taskParam