连接数据库
def connect_es():
"""连接es"""
username = "elastic"
password = "elastic"
es = Elasticsearch(hosts="http://192.168.5.125:9200", http_auth=(username, password))
return es
必须先连接数据库,才可进行下一步操作
查询数据库
# 搜索文档
def search_es():
index='pos_bill',
body={
"query": {
"bool": {
"must": [
{
"term": {
"shopEntityId": {
"value": "1FGIHNM3QRPBTT725C889LSKTH001574"
}
}
}
]
}
}
}
es=connect_es()
res = es.search(index=index, body=body)
print(res['hits']['hits'][0]['_source'])
删除数据
def delete_es():
index = 'pos_bill'
es = connect_es()
data={
"query": {
"term": {
"_id": "77FCA5C399FA9E74F048AD8A131D8BE5"
}
}
}
es.delete(index=index,id='CHdkHYsBNYUhKaSss1uK',routing="1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK")
插入数据
def insert_es():
index="pos_bill"
data={
"id": "1FGIHNM3QRPBTT725C889LSKTH001574202309150001",
"billDate": "20230915",
"billSerialNumber": "1FGIHNM3QRPBTT725C889LSKTH001574202309150001",
"billType": "601",
"source": "2",
"cTimeStamp": "20230915143000",
"createTime": "20230915143000",
"exactBillType": "60101",
"receivableAmount": 200.0,
"saleTime": "20230915143000",
"shopEntityId": "1FGIHNM3QRPBTT725C889LSKTH001574",
"shopEntityName": "果果商户",
"shopId": "1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK",
"shopName": "郭氏机构mall04",
"terminalNumber": "BBBB920AAEAC",
"thirdPartyOrderNo": "D09090D902",
"totalFee": 200.0,
"totalNum": 1.0
}
es = connect_es()
es.index(index='pos_bill', body=data,routing="1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK")
更新数据
def update_es():
index="pos_bill"
data={
"doc":{
"totalFee": 9999.0,
"totalNum": 99.0
}
}
es = connect_es()
es.update(index='pos_bill', body=data,routing="1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK",id="CXdtHYsBNYUhKaSs-1uH")
if __name__ == '__main__':
update_es()