当前位置:网站首页>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
边栏推荐
- Spark 算子之distinct使用
- Interview questions of a blue team of Beijing Information Protection Network
- Filter usage of spark operator
- Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
- Leetcode-374 guess the size of the number
- Leetcode-396 rotation function
- Simple usage of dlopen / dlsym / dlclose
- Large factory technology implementation | industry solution series tutorials
- R语言中绘制ROC曲线方法二:pROC包
- Codejock Suite Pro v20.3.0
猜你喜欢

volatile的含义以及用法

【现代电子装联期末复习要点】

CAP定理

C language --- advanced pointer

MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问

MySQL Cluster Mode and application scenario
![[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention](/img/bf/2b4914276ec1083df697383fec8f22.png)
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention

Use bitnami PostgreSQL docker image to quickly set up stream replication clusters

C language self compiled string processing function - string segmentation, string filling, etc

多生成树MSTP的配置
随机推荐
Redis主从复制过程
Day (9) of picking up matlab
Intersection, union and difference sets of spark operators
单体架构系统重新架构
Basic greedy summary
Day (8) of picking up matlab
JS regular determines whether the port path of the domain name or IP is correct
Spark 算子之交集、并集、差集
MySQL - execution process of MySQL query statement
捡起MATLAB的第(2)天
PS add texture to picture
Filter usage of spark operator
CVPR 2022 quality paper sharing
Neodynamic Barcode Professional for WPF V11.0
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
Function summary of drawing object arrangement in R language
一文掌握vscode远程gdb调试
ESP32_Arduino
Configuration of multi spanning tree MSTP
Upgrade MySQL 5.1 to 5.67