config为ES环境配置
#elasticsearch环境配置
ES_HOST = "IP"
ES_PORT = 9200
- 新增记录bulk
import time
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import config
es = None
def init():
global es
es = Elasticsearch([config.ES_HOST], port=config.ES_PORT)
def addRecord():
print("<======start addRecord===========>")
actions = []
#要插入的数据
action = {
"_index": "properties_demo-01_2020-08",
"_type": "_doc",
"_id": "dtORKXQBYFLC1S8JD5l8",
"_score": 5.7014313,
"_source": {
"formatValue": "59255",
"productId": "DEMO-01",
"propertyName": "属性2",
"property": "prop2",
"numberValue": 59255,
"deviceId": "DEMO-01-0110",
"value": "59255",
"timestamp": 1598425197323
}
}
actions.append(action)
str = helpers.bulk(es,actions)
print(str)
if __name__ == '__main__':
init()
addRecord()
- 删除记录(delete)
# coding=utf-8
import config
from elasticsearch import Elasticsearch
es = None
def init():
global es
es = Elasticsearch([config.ES_HOST], port=config.ES_PORT)
def esDel():
print("<====start del=====>")
#删除所需三要素
indexname='properties_demo-01_2020-08'
doctype='_doc'
id='dtORKXQBYFLC1S8JD5l8'
res = es.delete(index=indexname,doc_type=doctype,id=id)
print(res)
print("<====end del=====>")
if __name__ == '__main__':
init()
esDel()
- 查询记录(包含aggs聚合)
query下size限制hits中返回记录数
aggs下size默认为10,若超过10需手动设置
# coding=utf-8
import config
from elasticsearch import Elasticsearch
es = None
def init():
global es
es = Elasticsearch([config.ES_HOST], port=config.ES_PORT)
def esSearch():
index = 'properties_' + '*'
deviceid = "DEMO-01-0110"
body = {
"query": {
"bool": {
"must": [
{
"term": {"deviceId": deviceid}
}
# ,{
# "term": {"property": 'prop2'}
# }
]
# ,
# "must_not":[
# {"term": {"property": 'stat'}}
# ]
,
# 时间范围过滤
"filter": {
"range": {"timestamp": {"gt": 1598425197000, "lte": 1598425297000}}
}
}
}
,
"size":0,#query返回的hits长度,这里不需要查询明细内容设置为0
"aggs":{
#自定义name,解析buckets时需用到
"property_agg":{
"terms":{
"field":"property",
"size":20, #默认只返回10个桶,若超过10个需手动设置size
"order":{
"min_property":"desc"
}
},
"aggs": {
"min_property": {
"min": {
"field": "numberValue"
}
}
,
"max_property": {
"max": {
"field": "numberValue"
}
},
"avg_property": {
"avg": {
"field": "numberValue"
}
},
"sum_property": {
"sum": {
"field": "numberValue"
}
}
}
}
}
# ,"sort": {"timestamp": {"order": "desc"}}#,
# "collapse": {"field": "property"}
}
# filter_path = ['hits.hits._id', 'hits.hits._source']
result = es.search(index=index, body=body)
print(result['hits'])
print(result['aggregations'])
print(result['aggregations']['property_agg'])
print(result['aggregations']['property_agg']['buckets'])
buckets = result['aggregations']['property_agg']['buckets']
for item in buckets:
print("property:" ,item['key']," min_property:",item['min_property']['value']," avg_property",item['avg_property']['value']," max_property:",item['max_property']['value']," sum_property:",item['sum_property']['value'])
if __name__ == '__main__':
init()
esSearch()
- stats聚合查询
stats包含max、min、avg、count、sum
# coding=utf-8
import config
from elasticsearch import Elasticsearch
es = None
def init():
global es
es = Elasticsearch([config.ES_HOST], port=config.ES_PORT)
def esSearch():
index = 'properties_' + '*'
deviceid = "DEMO-01-0110"
body = {
"query": {
"bool": {
"must": [
{
"term": {"deviceId": deviceid}
}
# ,{
# "term": {"property": 'prop2'}
# }
]
# ,
# "must_not":[
# {"term": {"property": 'stat'}}
# ]
,
# 时间范围过滤
"filter": {
"range": {"timestamp": {"gt": 1598425197000, "lte": 1598425297000}}
}
}
}
,
"size":0,#query返回的hits长度,这里不需要查询明细内容设置为0
"aggs":{
#自定义name,解析buckets时需用到
"property_agg":{
"terms":{
"field":"property",
"size":20 #默认只返回10个桶,若超过10个需手动设置size
},
"aggs": {
"stats_property": {
"stats": {#stats包含max、min、avg、count、sum
"field": "numberValue"
}
}
}
}
}
# ,"sort": {"timestamp": {"order": "desc"}}#,
# "collapse": {"field": "property"}
}
# filter_path = ['hits.hits._id', 'hits.hits._source']
result = es.search(index=index, body=body)
print(result['hits'])
print(result['aggregations'])
print(result['aggregations']['property_agg'])
print(result['aggregations']['property_agg']['buckets'])
buckets = result['aggregations']['property_agg']['buckets']
#解析buckets
for item in buckets:
print("property:" ,item['key']," min:",item['stats_property']['min']," avg",item['stats_property']['avg']," max:",
item['stats_property']['max']," sum:",item['stats_property']['sum']," count:",item['stats_property']['count'])
if __name__ == '__main__':
init()
esSearch()