python搜索es

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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值