import datetime
import sys
import getopt
import hashlib
from elasticsearch import Elasticsearch """
初始化elasticsearch连接
"""
def init_es():
return Elasticsearch(["localhost:9200"]) """
查询数据, 支持分页
"""
def query_data(log_date, puid, data_rows, page_num):
es = init_es()
body = {
"query":{
"term":{
"puid":""
}
},
"sort":[{
"datatime":{
"order":"asc"
}
},{
"@timestamp":{
"order":"asc"
}
}],
"size":"",
"from":""
}
index_name = "test-"+log_date
body["query"]["term"]["puid"] = run_md5(str(puid))
body["size"] = data_rows
from_page = int(data_rows) * (int(page_num)-1)
body["from"] = from_page
data_array = es.search(index=index_name, doc_type='doc', body=body) print_data(data_array, data_rows) """
打印数据
"""
def print_data(data_array, data_rows): datas = data_array["hits"]["hits"]
#print datas
print "符合条件的数据总条数为:" + str(data_array['hits']['total'])
print "当前显示的数据如下:"
for data in datas:
result = data['_source']['message']
print result.strip(result.split("\t")[0]).strip("\t") """
md5加密
"""
def run_md5(puid): md = hashlib.md5()
md.update(puid.encode('utf-8'))
return md.hexdigest() """
处理逻辑调用查询
"""
def run(param):
puid = param['puid']
log_date = param['log_date'] if param['log_date'] else datetime.datetime.now().strftime('%Y-%m-%d')
data_rows = param['data_rows']
page_num = param['page_num'] query_data(log_date, puid, data_rows, page_num) def main(argv):
try:
opts, args = getopt.getopt(argv[1:], 'hp:d:r:n:', ['help', 'puid=', 'log_date=', 'data_rows=', 'page_num='])
except getopt.GetoptError as err:
print str(err)
sys.exit(2) if not opts:
print "The puid is a must !"
opts = [('-h', '')] VARS = {'puid': None, 'log_date': None, 'data_rows': 50, 'page_num': 1} for opt, value in opts: if opt in ('-h', '--help'):
print("")
print("Usage:python query_client_log.py -p puid [-r data_rows -d log_date [-n page_num]] | --puid=puid ....")
print("-p, --puid 用户id,此为必传参数")
print("-d, --log_date 数据日期,格式:yyyy-mm-dd")
print("-r, --data_rows 查询数据的条数,如为分页查询则为每页数据的条数,默认50条")
print("-n, --page_num 分页查询,当前查询的页号,默认不分页")
print("-h, --help 查看帮助并退出")
print("")
sys.exit() if opt in ('-p', '--puid'):
VARS['puid'] = value
elif opt in ('-d', '--log_date'):
VARS['log_date'] = value
elif opt in ('-r', '--data_rows'):
VARS['data_rows'] = value
elif opt in ('-n', '--page_num'):
VARS['page_num'] = value run(VARS) if __name__ == '__main__':
main(sys.argv)

python查询elasticsearch(Query DSL) 实例的更多相关文章

  1. Elasticsearch Query DSL 整理总结(一)—— Query DSL 概要,MatchAllQuery,全文查询简述

    目录 引言 概要 Query and filter context Match All Query 全文查询 Full text queries 小结 参考文档 引言 虽然之前做过 elasticse ...

  2. Elasticsearch Query DSL 整理总结(二)—— 要搞懂 Match Query,看这篇就够了

    目录 引言 构建示例 match operator 参数 analyzer lenient 参数 Fuzziness fuzzniess 参数 什么是模糊搜索? Levenshtein Edit Di ...

  3. Elasticsearch Query DSL

    Elasticsearch Query DSL By:授客 QQ:1033553122 1. match_all 1 2. match 2 3. match_phrase 5 4. match_phr ...

  4. Elasticsearch Query DSL(查询语言)

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  5. python 查询 elasticsearch 常用方法(Query DSL)

    1. 建立连接 from elasticsearch import Elasticsearch es = Elasticsearch(["localhost:9200"]) 2. ...

  6. Elasticsearch Query DSL查询入门

    本篇为学习DSL时做的笔记,适合ES新手,大佬请略过~ Query DSL又叫查询表达式,是一种非常灵活又富有表现力的查询语言,采用JSON接口的方式实现丰富的查询,并使你的查询语句更灵活.更精确.更 ...

  7. Elasticsearch Query DSL 整理总结(三)—— Match Phrase Query 和 Match Phrase Prefix Query

    目录 引言 Match Phase Query slop 参数 analyzer 参数 zero terms query Match Phrase 前缀查询 max_expansions 小结 参考文 ...

  8. Elasticsearch Query DSL备忘(1)(Constant score query和Bool Query)

    Query DSL (Domain Specific Language),基于json的查询方式 1.Constant score query,常量分值查询,目的就是返回指定的score,一般都结合f ...

  9. Elasticsearch Query DSL 语言介绍

    目录 0. 引言 1. 组合查询 2. 全文搜索 2.1 Match 2.2 Match Phase 2.3 Multi Match 2.4 Query String 2.5 Simple Query ...

随机推荐

  1. VoIP应用在Ubuntu 14.04下编译FFmpeg libX264及PJSIP

    PJSIP是一个开源的SIP协议栈.它支持多种SIP的扩展功能,可说算是最目前流行的SIP协议栈之一了.  它实现了SIP.SDP.RTP.STUN.TURN和ICE.PJSIP作为基于SIP的一个多 ...

  2. Swift 的 Currying 特性 | SwiftCafe 咖啡时间

    Currying 也是 Swift 的众多先进特性之一,用一句话说就是将接受多个参数的函数,转变成每次之接受一个参数的调用序列. 上面一句话说得可能大家感觉不是那么清楚,那么没关系,咱们举一个例子来说 ...

  3. AngularJS 计时器

    <div ng-controller="MyController"> <!--显示$scope.clock的now属性--> <h1>hello ...

  4. C++杂记:运行时类型识别(RTTI)与动态类型转换原理

    运行时类型识别(RTTI)的引入有三个作用: 配合typeid操作符的实现: 实现异常处理中catch的匹配过程: 实现动态类型转换dynamic_cast. 1. typeid操作符的实现 1.1. ...

  5. API HOOK介绍 【转】

    什么是“跨进程 API Hook”? 众所周知Windows应用程序的各种系统功能是通过调用API函数来实现.API Hook就是给系统的API附加上一段小程序,它能监视甚至控制应用程序对API函数的 ...

  6. [Erlang-0015][Lager] Erlang日志框架Lager简析

    项目地址:https://github.com/basho/lager (欢迎任何形式的转载,但请务必注明出处:http://www.cnblogs.com/liangjingyang)

  7. delphi7 xml通用解析转换为stringgrid

    对于有n多记录的xml,可以填充到stringgrid中 其中 vkeynode 为 xml中 重复节点 function CommonAnalyzeXml(vxml,vkeynode: string ...

  8. .NET程序员如何快入门Spring Boot

    本篇文章将教你作为一个.NET程序员如何快入门Spring Boot.你不需要用Eclipse,也不需要用IDEA.已经习惯了VS,其他的IDE-- 但不得不说VS Code很厉害,一用就喜欢.微软给 ...

  9. CSS样式规范

    一般团队都有对CSS样式的规范,因为只有写的规范些,维护层本低,易懂.我们开发并不一次性的,往往都是要迭代的,如果这次随便写,下次迭代的时候将付出高昂的代价.而团队的规范一般都大同小异,往往都包含一下 ...

  10. LVS-DR模式部署流程

    情景一 一.环境介绍 1)RIP.VIP.DIP为同一物理网络 2)LVS Hostname:lvs eth0:DIP-192.168.3.31 eth0:0:VIP-192.168.3.10 3)R ...