# es 查詢更新操作
# _*_ coding: utf-8 _*_
import time
import datetime
import pymysql
from elasticsearch import Elasticsearch
from urllib3.connectionpool import xrange
# class EsClient():
es_host = "192.168.8.190"
port = 9200
timeout = 15000
global index
global CLIENT
index = "content-2019.12.30"
CLIENT = Elasticsearch(hosts=es_host,port=port,timeout=timeout)
# 數據格式化
def get_page_data(result):
res = []
for hit in result['hits']['hits']:
# print(hit)
res.append({hit["_source"]["clientip"]: hit["_source"]["version"]})
print(res)
return res
# 數據的分頁操作
def load_all_data(scroll_id):
if scroll_id:
page = CLIENT.scroll(scroll_id=scroll_id, scroll='10m')
scroll_id = page['_scroll_id']
else:
page = CLIENT.search(index=index, scroll='10m', size=10000)
scroll_id = page['_scroll_id']
# print(page)
data = page["hits"]['hits']
total = page['hits']['total']['value']
print(len(data), total)
return {"data": data, "scroll_id": scroll_id, "length": len(data), "total": total}
# 數據的查詢操作 # 默認只給10000條數據
def query(body):
page = CLIENT.search(index=index, body=body)
data = page["hits"]['hits']
total = page['hits']['total']['value']
print(len(data), total)
return {"data": data, "length": len(data), "total": total}
#update:更新指定index、type、id所對應的文檔
#更新的主要點:
#1. 需要指定 id
#2. body={"doc": <xxxx>} , 這個doc是必須的
# es.update(index="my_index",doc_type="test_type",id=1,body={"doc":{"name":"python1","addr":"深圳1"}})
# 更新一條數據
def run_update_1():
print("開始修改")
body={"doc":{"site": "小武的測試站-+++++"}}
CLIENT.update(index='content-2019.12.30', id='y-O4nG8Bpdw5Z6bISieJ', body=body)
print("修改成功")
pass
# 批量更新, 跟新滿足條件的數據
def run_update_all():
print("開始修改")
query = {
"script": {"source": "ctx._source['site']='小武的測試站'" }, # 更新語句,將數據集合更改
'query': {'match': {'site': '生意地'}} # 查詢語句,將滿足條件的值進行篩選出來
}
CLIENT.update_by_query(index='content-2019.12.30', body=query)
print("修改成功")
pass
if __name__ == "__main__":
run_update_all()