当前位置:网站首页>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.基于镜像仓库一键安装containerd脚本
- C#,贝尔数(Bell Number)的计算方法与源程序
- Best practices of Apache APIs IX high availability configuration center based on tidb
- Date date calculation in shell script
- [section 5 if and for]
- Day (9) of picking up matlab
- Single architecture system re architecture
- Upgrade MySQL 5.1 to 5.610
- Why is IP direct connection prohibited in large-scale Internet
- Why disable foreign key constraints
猜你喜欢
幂等性的处理
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
Spark 算子之sortBy使用
Spark 算子之交集、并集、差集
The principle and common methods of multithreading and the difference between thread and runnable
捡起MATLAB的第(8)天
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
JVM - Chapter 2 - class loader subsystem
Function summary of drawing object arrangement in R language
Unity Shader学习
随机推荐
[split of recursive number] n points K, split of limited range
Leetcode-374 guess the size of the number
Spark 算子之coalesce与repartition
Why is IP direct connection prohibited in large-scale Internet
Config learning notes component
时序模型:门控循环单元网络(GRU)
Read the meaning of serial port and various level signals
Date date calculation in shell script
多级缓存使用
JVM - Chapter 2 - class loader subsystem
volatile的含义以及用法
MySQL - MySQL查询语句的执行过程
捡起MATLAB的第(7)天
一文掌握vscode远程gdb调试
js正則判斷域名或者IP的端口路徑是否正確
Merging of Shanzhai version [i]
Neodynamic Barcode Professional for WPF V11.0
GRBL学习(一)
Intersection, union and difference sets of spark operators
Vision of building interstellar computing network