es查询与聚合
"""
官方文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/aggregations.html
官方文档:https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html
参考:https://blog.csdn.net/hanyuyang19940104/article/details/81668880中的bug解决方案
可参考:https://blog.csdn.net/junfeng666/article/details/78251788
可参考: https://linux.ctolib.com/elasticsearch-dsl-py.html
"""
# metric的方法有sum、avg、max、min, value_count等等
import time
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q, A
from elasticsearch.helpers import bulk
import requests
import json
es = Elasticsearch(['localhost'], port=9200)
dict_1 = {"name": "test", "ac": "bob", "address": {"city":"shanghai"}}
dict_2 = [
{"name":'bob', "age":100, "ac":"sssssss"},
{"name":'marry', "age":110, "ac":"i am marry"},
{"name":'lili', "age":155, "ac":"helloworld"},
]
def get_data_by_id():
return es.get(index="bank", doc_type="account", id='qwe')
def query_data():
res = es.search(index="bank", doc_type="account")
return res
def index_data():
return es.index(index="bank", doc_type="account", body=dict_1)
def bulk_data(data=None):
if not data:
data = dict_2
actions = []
# '_op_type':'index',#操作 index update create delete
for i in data:
action = {
'_op_type': 'index', # 操作 index update create delete
# '_index': "bank",
'_index': "cars",
"_type": "transactions",
# "_type": "account",
"_source": i
}
actions.append(action)
success, _ = bulk(es, actions=actions, raise_on_error=True)
return success
def Q_func():
# 官方文档:https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html
# q = Q("multi_match", query="bob", fields=["name", 'ac'])
s = Search(using=es, index="bank")
# Q("match", title='python') & Q("match", title='django')
s.query = Q('bool', must=[Q('match', name='bob'), Q('match', ac='bob')]) # name=bob且ac=bob
# s.query = Q('bool', must=[Q('match', name='bob')])
res_3 = s.query().execute()
print(res_3)
print(len(res_3))
# <Response: [<Hit(bank/account/a_AJWGYB6B4UEZt2YIRu): {'name': 'marry', 'age': 10, 'ac': 'i am marry'}>
def q_search():
# .source(["address"])可以指定返回字段
s = Search(using=es, index="bank")
# s = s.filter('term', category__keyword='Python')
s = s.query('match', address__city='shanghai') # 查二级数据
# data为dict_1 = {"name": "test", "ac": "bob", "address": {"city":"shanghai"}}
res = s.execute()
print(res)
# 聚合:
def A_func():
s = Search(using=es, index="bank")
# a = A('terms', field='name')
# s.aggs.bucket("term_name", "terms", field='name')
# res =a.metric('clicks_per_category', 'sum', field='clicks') \
# .bucket('tags_per_category', 'terms', field='tags')
s.aggs.bucket('sum_age', 'match', field='name') \
.metric("max_age", "sum", script="doc['downFlux'].value+doc['upFlux'].value")
# .metric("max_age", "sum", field='age')
# s.aggs.bucket('sum_age', 'terms', field='name') # 参数为group_name, 方法, 栏
# s.aggs.metric('max_age', 'max', field='age')
# s.aggs.bucket('per_name', 'terms', field='name') \
# .metric('max_age', 'max', field='age')
res = s.execute()
for i in res:
print(i)
print(len(res))
# a = {'terms': {'field': 'name'}}
# {
# 'terms': {'field': 'category'},
# 'aggs': {
# 'clicks_per_category': {'sum': {'field': 'clicks'}},
# 'tags_per_category': {'terms': {'field': 'tags'}}
# }
# }
# index_data()
# q_search()
# A_func()
# print(bulk_data())
def curl_es():
data = [
{"price": 10000, "color": "red", "make": "honda", "sold": "2014-10-28"},
{"price": 20000, "color": "red", "make": "honda", "sold": "2014-11-05"},
{"price": 30000, "color": "green", "make": "ford", "sold": "2014-05-18"},
{"price": 15000, "color": "blue", "make": "toyota", "sold": "2014-07-02"},
{"price": 12000, "color": "green", "make": "toyota", "sold": "2014-08-19"},
{"price": 20000, "color": "red", "make": "honda", "sold": "2014-11-05"},
{"price": 80000, "color": "red", "make": "bmw", "sold": "2014-01-01"},
{"price": 25000, "color": "blue", "make": "ford", "sold": "2014-02-12"},
]
body = {
"size": 0,
"aggs": {
"popular_colors": {
"terms": {
"field": "color.keyword"
}
}
}
}
res = es.search(index="cars", doc_type="transactions", body=body)
print(res)
# for key, i in res:
# print(key, i)
def agg_es():
#
# s = Search(using=es, index="cars", doc_type='transactions').extra(size=0) ### 注意这里size=0可加快查询速度
s = Search(using=es, index="cars", doc_type='transactions')
# metric的方法有sum、avg、max、min, value_count等等
# bucket的size参数只返回1个bucket桶
# 加上size=1000返回的数据不会只有10条
s.aggs.bucket('test', 'terms', field='color.keyword',size=1000).metric("sum_test", 'count', field='make.keyword')
# metric("max_age", "sum", script="doc['downFlux'].value+doc['upFlux'].value")
print(s.to_dict(),'\n')
res = s.execute()
print(res)
print(res.aggregations)
print(res.to_dict())
'''
{'_index': 'cars', '_type': 'transactions', '_id': 'fPDTW2YB6B4UEZt2CYQ_', '_score': 1.0,
'_source': {'price': 20000, 'color': 'red', 'make': 'honda', 'sold': '2014-11-05'}}]}, 'aggregations': {
'test': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 0,
'buckets': [{'key': 'red', 'doc_count': 4, 'sum_test': {'value': 130000.0}},
{'key': 'blue', 'doc_count': 2, 'sum_test': {'value': 40000.0}},
{'key': 'green', 'doc_count': 2, 'sum_test': {'value': 42000.0}}]}}}
'''
if __name__ == "__main__":
agg_es()
# doc_count:查询出的记录条数,与聚合后的buckets的list 长度不同
es查询与聚合的更多相关文章
- es查询,聚合、平均值、值范围、cardinality去重查询
原文:https://blog.csdn.net/sxf_123456/article/details/78195829 普通查询 GET ana-apk/_search { "query& ...
- ElasticSearch 学习记录之ES查询添加排序字段和使用missing或existing字段查询
ES添加排序 在默认的情况下,ES 是根据文档的得分score来进行文档额排序的.但是自己可以根据自己的针对一些字段进行排序.就像下面的查询脚本一样.下面的这个查询是根据productid这个值进行排 ...
- Elasticsearch(8) --- 聚合查询(Metric聚合)
Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...
- Elasticsearch(9) --- 聚合查询(Bucket聚合)
Elasticsearch(9) --- 聚合查询(Bucket聚合) 上一篇讲了Elasticsearch聚合查询中的Metric聚合:Elasticsearch(8) --- 聚合查询(Metri ...
- Es查询工具使用
Kibana按照索引过滤数据 1.创建索引模式 2.查询索引中的数据 Es查询不返回数据 创建索引的时候指定mapping mappings={ "mappings": { &qu ...
- ES查询语句
记录常用的es 查询 聚合 GET _cat / indices GET / p_ext_develop / _mapping / g GET / p_ext_develop / _analyze { ...
- ElasticSearch的高级复杂查询:非聚合查询和聚合查询
一.非聚合复杂查询(这儿展示了非聚合复杂查询的常用流程) 查询条件QueryBuilder的构建方法 1.1 精确查询(必须完全匹配上,相当于SQL语句中的“=”) ① 单个匹配 termQuery ...
- java查询elasticsearch聚合
java查es多分组聚合: SearchRequestBuilder requestBuilderOfLastMonth = transportClient.prepareSearch(TYPE_NA ...
- Elasticsearch使用系列-基本查询和聚合查询+sql插件
Elasticsearch使用系列-ES简介和环境搭建 Elasticsearch使用系列-ES增删查改基本操作+ik分词 Elasticsearch使用系列-基本查询和聚合查询+sql插件 Elas ...
随机推荐
- Python-学习-小例子练习
网上了点小例子,练习一下下,都是特别简单的.而且这些代码也都是找的网上的代码,目的是在于练习一下Python和熟悉下Python的编码风格等等 学习一门语言,最快的方法就是把它用在世界的开发中,这样才 ...
- QC的使用学习(三)
一.需求转换测试 1.自动转换方法: (1)将最底层的子需求转换成设计步骤:即将最底层的子要求转换成测试用例的步骤. (2)将最底层的子要求转换成测试:即将最底层的要求转换成单个测试用例(建议使用) ...
- 【转】Linux学习(1)-常用快捷键、文件管理和查询
原文链接:http://www.cnblogs.com/zhaopei/p/7397402.html 有话要说 为什么要用Linux?要用Linux的原因太多,想说说不完啊. 如果你说用Linux只是 ...
- m个苹果放在n个盘子中有多少种结果
题目 m个苹果放在n个盘子中有多少种结果,前置条件: 允许存在空盘 重复的摆放结果忽略不计 根据题意,也就是有3种情况,的确完全重复的摆放方式是没多大意义的 思路 这题可以用枚举的描述方式进行尾递归求 ...
- 机器视觉必知-GenICam相机通用接口标准
机器视觉必知-GenICam相机通用接口标准 GenICam(相机通用接口): 一种通用软件接口 通用相机接口标准 目前机器视觉行业所使用的相机几乎均以相同方式来进行配置,即:---通过在注册表中的读 ...
- PAT——乙级1006:换个格式输出整数&乙级1021:个位数统计&乙级1031:查验身份证
1006 换个格式输出整数 (15 point(s)) 让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 ...
- NO6——KMP
int next[N]; char str1[M],str2[N]; //str1 长,str2 短 //len1,len2,对应str1,str2的长 void get_next(int len2) ...
- 使用github同步网站
今天刚刚完成了自己的一个小项目,想把他上传到服务器上,想到到我使用的Visual Stdio Code具有git功能,于是想到使用github作为代码仓库来同步代码. 大体步骤分为这几步:创建远程代码 ...
- 【linux】亲测成功_CentOS7.2/rhel7.2 忘记root密码及重置root密码的方法?
本文转自:https://www.jb51.net/article/146320.htm CentOS 7 root密码的重置方式和CentOS 6完全不一样,以进入单用户模式修改root密码为例. ...
- winspool.drv
public partial class Form1 : Form{ [System.Runtime.InteropServices.DllImportAttribute("winspool ...