Q:有两个人掉到陷阱里了,死的人叫死人,活人叫什么?

es索引模版配置不当导致的aggs聚合查询字段显示错误的问题

Elasticsearch | 作者 Max | 发布于2016年03月18日 | | 阅读数:8328

今天在es中对http日志的状态码status进行aggs搜索出现字段内容显示不正常的问题,记录过程:

http日志的情况:
1、http日志从logstash写入es时,状态码配置为status,其内容为 200 ,302 ,400 ,404等。
2、使用kibana对该日志的索引进行查询,在discover页面中显示的status内容跟logstash的内容一致,是正常的。

出现问题的场景:
(我这里使用的是kibana的sense插件进行的查询,如果直接使用curl python-ES也是一样的)
查询该索引:
POST http-2016.03.18/_search
{
  "fields": ["status"],
          "query":{
            "bool":{
              "must": [
                {
                  "range" : {
                    "@timestamp" : {"gte" : "now-5m"}
                  }
                }
              ]
            }
          },
          "_source": "false",
          "size": 0,
          "aggs": {
            "status_type": {
              "terms":{"field":"status"}
            }
          }
}

查询返回的结果中aggregations部分的内容:
"aggregations" : {
    "status_type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : -56,
        "doc_count" : 376341
      }, {
        "key" : 46,
        "doc_count" : 51439
      }, {
        "key" : 45,
        "doc_count" : 5543
      }, {
        "key" : 48,
        "doc_count" : 1669
      }, {
        "key" : -108,
        "doc_count" : 1068
      }, {
        "key" : -50,
        "doc_count" : 11
      }, {
        "key" : -109,
        "doc_count" : 8
      }, {
        "key" : -112,
        "doc_count" : 4
      } 

寻找原因:
起先先去掉了查询的aggs部分,单独查询query的内容:
POST http-2016.03.18/_search
{
  "fields": ["status"],
          "query":{
            "bool":{
              "must": [
                {
                  "range" : {
                    "@timestamp" : {"gte" : "now-5m"}
                  }
                }
              ]
            }
          }
}

返回的结果中,hits显示的status字段内容是正常的:
"hits": {
    "total": 1242104,
    "max_score": 1,
    "hits": [
      {
        "_index": "http-2016.03.18",
        "_type": "log",
        "_id": "AVOI3EiwidwPAhB1e7gQ",
        "_score": 1,
        "fields": {
          "status": [
            "200"
          ]
        }
      }
    ......

然后查询了http索引的索引信息和模版配置:
GET /http-2016.03.18/
GET /_template/http
发现其中http的status的属性type类型的内容是byte :
        "properties": {
          "@timestamp": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },
        ......
        ......
          "status": {
            "type": "byte"
          },
        ......
        ......

原因:
在aggs查询中发现了status字段显示错误的情况,status的type类型在es模版中定义成了byte类型,当status的值超过127后将出现溢出的情况,因此修改为short后,恢复了正常。
(对于http的状态码status,其type类型使用short已经足够了,如果使用integer,long或默认的string类型也是可以的,这里影响的是存储空间占用的大小。)
 
 

[尊重社区原创,转载请保留或注明出处]
本文地址:http://elasticsearch.cn/article/60


0 个评论

要回复文章请先登录注册