当前位置:网站首页>ES常用查询、排序、聚合语句
ES常用查询、排序、聚合语句
2022-04-23 15:57:00 【aserendipper】
一、精确查询某个字段
1、两个查询条件是and的关系
查询text类型时,需要在字段后面加上.keyword,如果字段为keyword类型,则不需要加
{
"size": 20, //查询结果显示个数
"query": {
"bool": {
"must": [
{
"term": {
"description.keyword": { //字段名称
"value": "投资咨询" //字段值
}
}
},
{
"term": {
"tags_v2.id": {
"value": "tag-ec0dcc7af3"
}
}
}
]
}
}
}
查询nested类型字段时,需要加上子字段或者使用下面的查询语句
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "tags_v2", //字段名称
"query": {
"term": {
"tags_v2.id": {
"value": "tag-ec0dcc7af3"
}
}
}
}
}
]
}
}
}
2、两个查询条件是or的关系
{
"query": {
"bool": {
"should": [
{
"term": {
"app_name": {
"value": "知泸水"
}
}
},
{
"term": {
"app_type": {
"value": "应用"
}
}
}
]
}
}
}
3、多个字段多个逻辑关系
以下三个字段的关系是:bid_type and (address3 or address4)
{
"query": {
"bool": {
"must": [
{
"term": {
"bid_type": {
"value": "中标公告"
}
}
},
{
"bool": {
"should": [
{
"term": {
"address3": {
"value": "370100"
}
}
},
{
"term": {
"address4": {
"value": "370215"
}
}
}
]
}
}
]
}
}
}
二、查询不等于这个值的数据
{
"query": {
"bool": {
"must_not": [
{
"term": {
"address1": {
"value": "150"
}
}
}
]
}
}
}
三、模糊查询某个字段
1、查询以某个词语开头(description字段以投资咨询开头)
{
"query": {
"bool": {
"must": [
{
"prefix": {
"description.keyword": {
"value": "投资咨询",
"boost": 10
}
}
}
]
}
}
}
2、查询包含某个词语(description字段包含投资咨询)
{
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"description": {
"query": "投资咨询"
}
}
}
]
}
}
}
四、查询是否存在这个字段
1、查询存在这个字段的数据
{
"query": {
"exists": {
"field": "coop_industry1"
}
}
}
2、查询不存在这个字段的数据
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "logo"
}
}
]
}
}
}
五、复合查询
下列语句查询逻辑是cid等于1631880并且attention_num在[50,60]之间并且data_status为0或者1的数据
{
"query":{
"bool":{
"must":{
"term":{
"cid":"1631880"
}
},
"must_not":{
"range":{
"attention_num":{
"gte":60,
"lte":50
}
}
},
"should":[
{
"term":{
"data_status": 0
}
},
{
"term":{
"data_status": 1
}
}
]
}
}
}
六、排序
{
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"description": {
"query": "投资咨询"
}
}
}
]
}
},
"sort": [
{
"address2": {
"order": "desc" //降序
},
"address3": {
"order": "asc" //升序
}
}
]
}
七、聚合查询
1、对keywork,long等字段进行聚合
{
"size": 1,
"aggs": {
"capital_ratio": { //随便写
"terms": {
"field": "capital_ratio", //字段名称
"size": 10, //聚合结果显示个数
"order": {
"_term": "asc" //按照升序排列
}
}
}
}
}
2、对nested字段进行聚合
{
"aggs": {
"coop_industry1_aggs": { //随便写
"nested": {
"path": "coop_industry1" //字段名称
},
"aggs": {
"coop_industry1": { //随便写
"terms": {
"field": "coop_industry1.name", //字段名称
"size": 10 //聚合后结果显示个数
}
}
}
}
}
}
3、对时间类型聚合查询
{
"query": {
"bool": {
"must": [
{
"range": {
"found_date": {
"gte": 1533556800000,
"lte": 1533806520000
}
}
}
]
}
},
"size": 0,
"aggs": {
// 自己取的聚合名字,随便写
"found_date": {
// es提供的时间处理函数
"date_histogram": {
// 需要聚合分组的字段名称, 字段类型需要为date
"field": "found_date",
// 按什么时间段聚合, 这里是30分钟, 可用的interval在上面给出
"interval": "30m",
// 设置时区, 这样就相当于东八区的时间
"time_zone": "+08:00",
// 返回值格式化,HH大写,不然不能区分上午、下午
"format": "yyyy-MM-dd HH",
// 默认为0,桶中的数量大于min_doc_count
"min_doc_count": 0
}
}
}
}
版权声明
本文为[aserendipper]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39234967/article/details/117535233
边栏推荐
- s16. One click installation of containerd script based on image warehouse
- Basic greedy summary
- Named in pytoch_ parameters、named_ children、named_ Modules function
- 布隆过滤器在亿级流量电商系统的应用
- 【第5节 if和for】
- shell_2
- IronPDF for .NET 2022.4.5455
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- Compile, connect -- Notes
- 多级缓存使用
猜你喜欢
Spark 算子之sortBy使用
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
Day (9) of picking up matlab
The principle and common methods of multithreading and the difference between thread and runnable
mysql乐观锁解决并发冲突
Why disable foreign key constraints
Treatment of idempotency
Do we media make money now? After reading this article, you will understand
C, calculation method and source program of bell number
【现代电子装联期末复习要点】
随机推荐
Read the meaning of serial port and various level signals
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
时序模型:长短期记忆网络(LSTM)
Go language, condition, loop, function
Compile, connect -- Notes
捡起MATLAB的第(7)天
PS为图片添加纹理
捡起MATLAB的第(9)天
C language --- string + memory function
[self entertainment] construction notes week 2
多线程原理和常用方法以及Thread和Runnable的区别
Basic greedy summary
Timing model: gated cyclic unit network (Gru)
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
Day (9) of picking up matlab
Intersection, union and difference sets of spark operators
Why is IP direct connection prohibited in large-scale Internet
Spark 算子之sortBy使用
C, calculation method and source program of bell number
Fastjon2他来了,性能显著提升,还能再战十年