Elasticsearch常用查询语句

前言

 Elasticsearch 作为一个文档型数据库,和mysql等关系型数据库一样,同样有相关的查询语法和常用的查询语句

1.查询所有数据

GET /索引名/_search
{
  "query": {
    "match_all": {}
  }
}

2.根据关键字查询

GET /索引名/_search
{
  "query":{
      "bool":{
        "must": [
          {"term":
              {"ruleTraceId":145020602594435072}
          }
        ]
  }
}
}

其中 ruleTraceId 的字段类型为keyWord。

3.创建索引

PUT 索引名称/
{
  "settings":{
    "number_of_shards":3,
    "number_of_replicas":0
  },
  
  "mappings": {
    "doc":{
    "properties":{
        "ruleTraceId":{
           "type":"keyword"
        },
        "message":{
           "type":"text"
        },
        "invokeId":{
          "type":"keyword"
        },
        "marker":{
          "type": "keyword"
        },
        "line_number":{
          "type":"integer"
        },
        "class":{
          "type":"keyword"
        },
        "method":{
          "type":"keyword"
        },
        "exception":{
          "type":"nested",
           "properties":{
              "exception_message":{
                "type":"text"
              },
              "exception_class":{
                "type":"text"
              },
              "stacktrace":{
                "type":"text"
              }
           }
        },
        "timestamp":{
          "type": "date",
          "format":"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        },
        "version":{
          "type":"keyword"
        },
        "level":{
          "type":"keyword"
        }
    }
  }
}
}

其中number_of_shards 代表索引分片数,number_of_replicas 代表副本个数(注:如果只有一个节点,则副本数为0)。

4. 按照时间区间查询并排序

get /qlanglog/_search   
   {
                "query":{
                "bool":{
                "must": [ 
                          {"match": 
                              {"ruleTraceId":147543126306791424}
                          },
                          
                                                                            {
                                                  "range": {
                    "timestamp": {
                      "gte": "20/10/2021",
                      "lte": "2022",
                      "format": "dd/MM/yyyy||yyyy"
                    }
                  }
                             }
         ]
                  }
                },
      "sort":[{
              "timestamp":"asc"
              },
              "_id":"asc"
              ]
                }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值