https://www.jianshu.com/p/46372a2df051
#-*- coding: utf-8 -*-
# Author:Fan,Bai
# Mail: baifan08@meituan.com
#本文件用来在es中根据规则搜索符合条件的历史路径
import json
from elasticsearch import Elasticsearch,Urllib3HttpConnection
# 建立es客户端连接
es = Elasticsearch(
hosts=[
# {"host": "10.4.235.42"}
# {"host": "yf-meishi-es-scp01.yf.sankuai.com"}
# IP 如何获取参考上节:获取访问集群IP地址
{"host": "10..8.5"},
{"host": "10.5.59"},
],
http_auth=('corm', 'CDB691E'),
connection_class = Urllib3HttpConnection,
port=8080,
sniff_on_start=True,
sniff_on_connection_fail=True,
sniffer_timeout=600
)
# 索引一个文档
def index_test():
doc = {
"coop": {
"contractNum": "xcs1234567890",
"bdId": 2022448,
"partnerId": 394042
},
"product": {
"dealId": 123456,
"coopId": "16fbd051-3b14-4388-b06d-a628a928eae0",
"productId": 654321,
"preDealId": "scpuuid1-2079-aa60-97aa-f14d4541d8f8",
"productType": 1,
"productSource": 2,
"bizAccountId": 20074390,
"sellChannel": 1,
"cityIds": [
8000
],
"poiIds": [
795013
],
"createTime": 1466670131,
"modifyTime": 1466670131
}
}
return es.index(index="scp.groupbuy", doc_type="deals", id=123456, body=doc)
# 根据文档ID查询
def get_test():
return es.get(index="scp.groupbuy", id=str(123456), ignore=404)
# 根据多个文档ID查询
def multi_get_test():
p_body = {
"ids": ['1288480', '1408973']
}
return es.mget(index="scp.discount", body=p_body)
# 复合查询
def search_test():
qbody='''{"query": {
"range":{"route.diffLen": { "gte" : 500 }}
}
,
"from": 0,
"size": 10000,
"sort": [],
"profile": false
}'''
# return es.search(index="rider_trace", doc_type="deals", body=q_body)
return es.search(index='rider_trace',scroll='1m',body=qbody)
# 更新索引
def update_test():
doc = {
"doc": {
"coop": {
"cityId": 123
}
}
}
return es.update(index="scp.groupbuy", doc_type="deals", id=123456, body=doc)
def discount_test():
q_body = {
"_source": [
"product.dealId"
],
"size": 10000,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"missing": {
"field": "coop.cityId"
}
}
]
}
}
}
},
}
return es.search(index="scp.discount", doc_type="deals", body=q_body)
if __name__ == '__main__':
# res1 = index_test()
# print res1
# res3 = multi_get_test()
# print res3
# print '>>>'
# print res3['docs']
# print '>>>'
# print res3['docs'][0]['_source']
#
# res4 = search_test()
# print res4
# res5 = update_test()
# print res5
res = search_test()
print res['hits']['total']
# for ai in res['hits']['hits']:
# print(ai.keys())
print(len(res['hits']['hits']))
with open("more500ways.json", "w") as f:
f.write(json.dumps(res, indent=4))