ElasticSearch使用kibana控制台查询示例(时间范围查询)

news/2024/7/3 18:38:12

记录一下日期查询问题

"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"

# 创建索引
PUT my_date1
{
  "mappings": {
    "properties": {
      "publicDate": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
        // 不管publicDate是什么格式, 存储字段始终是字符串形式, 默认格式为第一种格式, 这里为yyyy-MM-ddHH:mm:ss
        // 同理, 如果yyyy-MM-dd在第一个, 那么格式化字符串形式就是yyyy-MM-dd
        "store": true
      }
    }
  }
}

ElasticSearch使用kibana控制台查询示例(带时间范围查询)

#查询transCode为OO06U001,并且根据@timestamp日期范围过滤,求出该交易码的最大、最小、平均耗时
GET transactionmonitor-2021.12.08/doc/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "transCode.keyword": "OO06U001"
                    }
                }
            ], 
            "filter": {
                "range": {
                    "@timestamp": {
                        "gte":"2021-12-08 00:18:00", 
                        "lte":"2021-12-08 00:20:00", 
                        "time_zone":"+08:00",
                        "format":"yyyy-MM-dd HH:mm:ss"
                    }
                }
            }
        }
    },
    "aggs": {
      "costTimeMax": {
        "max": {
          "field": "costTime"
        }
      },
      "costTimeMin":{
        "min": {
          "field": "costTime"
        }
      },
      "costTimeAvg":{
        "avg": {
          "field": "costTime"
        }
      }
    }
}



#查询出@timestamp该日期范围内的所有交易码的最大、最小、平均耗时,doc_count出现次数
##filter查询没有相关性得分
GET transactionmonitor-2021.12.08/doc/_search
{
    "query": {
        "bool": {
            "filter": {
                "range": {
                    "@timestamp": {
                        "gte":"2021-12-08 00:18:00", 
                        "lte":"2021-12-08 00:20:00", 
                        "time_zone":"+08:00",
                        "format":"yyyy-MM-dd HH:mm:ss"
                    }
                }
            }
        }
    },
    "aggs": {
      
      "transCodeTerms": {
        "terms": {
          "field": "transCode.keyword"
        },
        "aggs": {
          "costTimeMax": {
            "max": {
              "field": "costTime"
            }
          },
          "costTimeMin":{
            "min": {
              "field": "costTime"
            }
          },
          "costTimeAvg":{
            "avg": {
              "field": "costTime"
            }
          }
        }
      }
      
    }
}


#查询出交易码OO06U001和@timestamp该日期范围内的最大、最小、平均耗时,doc_count出现次数
#range查询有相关性得分
GET transactionmonitor-2021.12.08/doc/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "transCode": "OO06U001"
                    }
                }, 
                {
                    "range": {
                        "@timestamp": {
                            "gte": "2021-12-08 00:18:00.00", 
                            "lte": "2021-12-08 00:20:00.00", 
                            "time_zone": "+08:00", 
                            "format": "yyyy-MM-dd HH:mm:ss.SS"
                        }
                    }
                }
            ]
        }
    }
    ,
    "aggs": {
      
      "transCodeTerms": {
        "terms": {
          "field": "transCode.keyword"
        },
        "aggs": {
          "costTimeMax": {
            "max": {
              "field": "costTime"
            }
          },
          "costTimeMin":{
            "min": {
              "field": "costTime"
            }
          },
          "costTimeAvg":{
            "avg": {
              "field": "costTime"
            }
          }
        }
      }
      
    }
}



#查询出@timestamp该日期范围内的所有交易码的最大、最小、平均耗时,doc_count出现次数
#在根据出现次数正序排列(asc)
##filter查询没有相关性得分
GET transactionmonitor-2021.12.08/doc/_search
{
    "query": {
        "bool": {
            "filter": {
                "range": {
                    "@timestamp": {
                        "gte":"2021-12-08 00:18:00", 
                        "lte":"2021-12-08 00:20:00", 
                        "time_zone":"+08:00",
                        "format":"yyyy-MM-dd HH:mm:ss"
                    }
                }
            }
        }
    },
    "aggs": {
      
      "transCodeTerms": {
        "terms": {
          "field": "transCode.keyword",
          "order": {
            "_count": "asc"
          }
        },
        "aggs": {
          "costTimeMax": {
            "max": {
              "field": "costTime"
            }
          },
          "costTimeMin":{
            "min": {
              "field": "costTime"
            }
          },
          "costTimeAvg":{
            "avg": {
              "field": "costTime"
            }
          }
        }
      }
      
    }
}

补充聚合结果排序

#查询出@timestamp该日期范围内的所有交易码的最大、最小、平均耗时,doc_count出现次数
#在根据聚合结果costTimeMin(最小耗时)正序(asc)排列
#filter查询没有相关性得分
GET transactionmonitor-2021.12.08/doc/_search
{
    "query": {
        "bool": {
          "must": [
            {"match": {
                "isSuccess.keyword": "Y"
              }
            }
          ], 
            "filter": {
                "range": {
                    "@timestamp": {
                        "gte":"2021-12-08 00:18:00", 
                        "lte":"2021-12-08 00:20:00", 
                        "time_zone":"+08:00",
                        "format":"yyyy-MM-dd HH:mm:ss"
                    }
                }
            }
        }
    },
    "aggs": {
      
      "transCodeTerms": {
        "terms": {
          "field": "transCode.keyword",
          "order": {
            "costTimeMin": "asc"
          }
        },
        "aggs": {
          "costTimeMax": {
            "max": {
              "field": "costTime"
            }
          },
          "costTimeMin":{
            "min": {
              "field": "costTime"
            }
          },
          "costTimeAvg":{
            "avg": {
              "field": "costTime"
            }
          }
        }
      }
      
    }
}
#使用自带 key(分类的字段) 和 count(匹配的次数) 字段排序
#BucketOrder.key(asc)  BucketOrder.count(asc)   BucketOrder.aggregation(orderField, asc)
GET transactionmonitor-2021.12.08/doc/_search
{
    "query": {
        "bool": {
          "must": [
            {"match": {
                "isSuccess.keyword": "Y"
              }
            },
            {
              "match": {
                "transCode.keyword": "DEDE02Q002"
              }
            }
          ], 
            "filter": {
                "range": {
                    "@timestamp": {
                        "gte":1638893880000, 
                        "lte":1638894000000
                    }
                }
            }
        }
    },
    "aggs": {
      
      "transCodeGrp": {
        "terms": {
          "field": "@timestamp",
          "order": {
            "_key": "asc",
            "_count": "asc"
          }
        },
        "aggs": {
          "costTimeAvg":{
            "avg": {
              "field": "costTime"
            }
          }
        }
      }
      
    }
}


http://www.niftyadmin.cn/n/4054447.html

相关文章

SpringBoot整合Quartz 实现分布式定时任务调度

一、Quartz 集群架构 Quartz 是 Java 领域最著名的开源任务调度工具。 在上篇文章中,我们详细的介绍了 Quartz 的单体应用实践,如果只在单体环境中应用,Quartz 未必是最好的选择,例如Spring Scheduled一样也可以实现任务调度&am…

ElasticSearch6.X版本自动添加时间戳

需求:根据时间提取es数据 解决:为es的记录添加时间戳 1、方法 配置时间戳 pipeline PUT _ingest/pipeline/my_timestamp_pipeline {"description": "Adds a field to a document with the time of ingestion","processors&q…

ElasticsearchTemplate替换为ElasticsearchRestTemplate(Transport Client替换为RestHighLevelClient)

在Elasticsearch 8.0的版本中也将完全移除TransportClient,其次有些es有些账号不能通过 9300端口连接 所以项目中打算由RestHighLevelClient 替换掉 Transport Client 版本 这里给一张springboot官方的版本推荐 这里我的springboot版本为 2.1.6.RELEASE Elasticse…

ruby编码说明

程序编码一般分几种情况: 1、源码文件编码 2、接收外部内容的编码 3、运行环境编码 4、操作系统编码 首先源码文件的编码,可以通过在ruby文件的头部添加一行申明即可,这样所有在源码里面出现的字符都保存为指定的编码: # -*- codi…

left join注意事项

一、【问题描述】 客户表(CLT.CSM_BASEINFO)左连接证件信息表(CLT.CSM_CERTINFO),通过客户名称查询客户信息。 1)证件信息表的字段筛选条件,放到on后面,查询结果正常 SELECT clt…

JDK8给Tomcat配置https

第一步:生成秘钥库 采用JDK自带的keytool工具生成秘钥 别名 xieh1234 存储路径 D:\cas\keystore\ keytool -genkey -v -alias xieh1234 -keyalg RSA -keystore D:\cas\keystore\xieh1234.keystore -alias 表示证书的别名,一个keystore文件中可以存放多…

笔记本插拔电源黑屏一下

亲爱的朋友你是否有这样的痛苦呢?当你的笔记本插上电源,屏幕要黑一下;当你拔掉电源时屏幕还是要黑一下。下面我就以Thinkpad E550电脑,win10系统为例和大家分享一下我的解决办法。 之所以会出现如上现状,是因为电脑插上电源和拔掉电源的刷屏率设置不相同。在桌面单击"右击…

编程游戏网站

http://www.imooc.com/article/14793转载于:https://blog.51cto.com/11283249/1950174