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. 【C++智能指针 auto_ptr】

    <More Effective C++>ITEM M9他提到auto_ptr.说是当异常产生的时候.怎么释放为对象分配的堆内存,避免反复编写内存释放语句. PS:这里书里面提到函数退出问题 ...

  2. 在Style中将EventTrigger与Trigger同时使用

    原文:在Style中将EventTrigger与Trigger同时使用 现在在看WPF关于Trigger的有关实例,然后找到一篇不错的文章,特此转载,收藏一下!!   一般情况下,使用Style时,可 ...

  3. 【转载】Docker 安装 Nginx 并个性化挂载配置文件 nginx.conf

    首先,系统(3.8以上内核的linux)中安装好 Docker 这个运用程序.由于网络原因,我们下载一个Docker官方的镜像需要很长的时间,甚至下载失败.为此,阿里云容器镜像服务提供了官方的镜像站点 ...

  4. 图形化界面安装oracle报错Could not execute auto check for display colors using command /usr/bin/xdpyinfo. Check if the DISPLAY variable is set.

    问题描述: 在Linux + oracle 安装时,采有root 帐号登录x-windows 界面,然后 $su oracle 登录录安装Oracle 报以下错误: >>> Coul ...

  5. style文件的指定

    新建资源文件   写资源文件 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese ...

  6. 通过.NET客户端调用Web API(C#)

    3.2 Calling a Web API From a .NET Client (C#) 3.2 通过.NET客户端调用Web API(C#) 本文引自:http://www.asp.net/web ...

  7. 读BeautifulSoup官方文档之与bs有关的对象和属性(1)

    自从10号又是5天没更, 是, 我再一次断更... 原因是朋友在搞python, 老问我问题, 我python也是很久没碰了, 于是为了解决他的问题, 我只能重新开始研究python, 为了快速找回感 ...

  8. jquery 包裹标签

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. RelativeSource 简述

    原文:RelativeSource 简述 RelativeSource实现标记扩展,以描述绑定源相对于绑定目标的位置. <Binding> <Binding.RelativeSour ...

  10. Linux学习之“exit函数”

    先看下"_exit"和"exit": exit被调用时,将冲刷所有的标准I/O流,关闭所有打开的文件描述符.在传统实现中,同时也将关闭标准I/O流.即表示标准输 ...