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

记录一下日期查询问题

"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"
            }
          }
        }
      }
      
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图图小淘气_real

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值