创建索引
from elasticsearch import Elasticsearch
es = Elasticsearch('192.168.149.96:9200')
mappings = {
"mappings": {
"properties": {
"perName": {
"type": "keyword"
},
"schoolName": {
"type": "keyword"
},
"perGrade": {
"type": "double"
},
"position": {
"type": "keyword"
},
"proMonth": {
"type": "date", # 可以接受如下类型的格式
"format": "yyyy-MM"
},
"detailedTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"projectName": {
"type": "keyword"
},
"targetOutput": {
"type": "double"
},
"actualOutput": {
"type": "double"
},
"yieldReachingRate": {
"type": "double"
},
"lateness": {
"type": "double"
},
"overtime": {
"type": "double"
},
"workingHours": {
"type": "double"
},
"groupRanking": {
"type": "double"
},
"qualityInspection": {
"type": "double"
},
"numberOfCustomer": {
"type": "double"
}
}
}
}
result = es.indices.create(index='new_source', body=mappings)
print(result)
删除索引
from elasticsearch import Elasticsearch
es = Elasticsearch('192.168.149.96:9200')
result = es.indices.delete('production_data')
print(result)
插入数据
from elasticsearch import Elasticsearch
es = Elasticsearch('192.168.149.96:9200')
action = {
'perName': '张三',
'schoolName': '清华',
'perGrade': 101.0,
'position': '职务',
'proMonth': '2020-03',
'detailedTime': '2020-03-09 00:00:00',
'projectName': '画框转写',
'targetOutput': 100.0,
'actualOutput': 90.0,
'yieldReachingRate': 0.9,
'lateness': 1.0,
'overtime': 1.0,
'workingHours': 8.0,
'groupRanking': 3.0,
'qualityInspection': 2.0,
'numberOfCustomer': 1.0
}
result = es.index(index="source_data", body=action)
print(result)
批量插入时 action 给成列表, 遍历即可
查询所有数据
from elasticsearch import Elasticsearch
es_con = Elasticsearch([{'host': '192.168.149.96', 'port': 9200}])
data_agg = es_con.search(
index='base_data',
size=1000,
body={
"query": {
"match_all": {}
},
}
)
print(data_agg, '666666666')
for data in data_agg.get('hits').get('hits'):
list_data = data.get('_source')
if '' in list(list_data.values()):
print(list_data)
id_ = data.get('_id')
# es_con.delete(index='base_data', id=id_)
else:
pass
# print(list_data)