ES--script进行时间聚合(非使用date_histogram)

版权声明:商业用途请联系博主,非商业转载请标明出处。 https://blog.csdn.net/qq_15807167/article/details/85269179

由于产品线上 日期时间使用的非 ES的date数据结构,导致无法使用date_histogram进行时间聚合的统计。因此这里使用了script来达到聚合的效果。

介绍

本次使用了3亿量的数据进行测试,效果还是蛮差的。这里先将一下使用方式。

  • lang代表使用的脚本方式
  • params需要脚本传递的参数
  • inline脚本字符串 (新版本请使用source)
GET _search
{
  "query": {
    "match_phrase": {
      "csdq": "江苏省"
    }
  }
  , "aggs": {
    "date": {
      "terms": {
        "script": {
          "lang": "painless"
          , "params": {
            "filedName": "csrq",
            "type":"1"
          }
          , "inline": """
        String type = params.type;
        String filedName = params.filedName;
        int year = Integer.parseInt(doc[filedName].value.substring(0, 4));
        int month = Integer.parseInt(doc[filedName].value.substring(5, 7));
        int day = Integer.parseInt(doc[filedName].value.substring(8, 10));
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        String tagName = "";
        if (type.equals("1")) {
            return c.get(Calendar.DAY_OF_WEEK);
        } else if (type.equals("2")) {
            return c.get(Calendar.DAY_OF_MONTH);
        } else if (type.equals("3")) {
            return c.get(Calendar.MONTH);
        }
        return tagName;
          """
        }
      }
    }
  }
}

查询结果

#! Deprecation: Deprecated field [inline] used, expected [source] instead
{
  "took" : 11369,
  "timed_out" : false,
  "_shards" : {
    "total" : 194,
    "successful" : 194,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 13977621,
    "max_score" : 3.7853308,
    "hits" : [
      {
        "_index" : "test_person",
        "_type" : "person",
        "_id" : "YgYl6WcB4n2gTYRcBZf4",
        "_score" : 3.7853308,
        "_source" : {
      }
    ]
  },
  "aggregations" : {
    "date" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "6",
          "doc_count" : 1998324
        },
        {
          "key" : "7",
          "doc_count" : 1998120
        },
        {
          "key" : "1",
          "doc_count" : 1997858
        },
        {
          "key" : "3",
          "doc_count" : 1997501
        },
        {
          "key" : "2",
          "doc_count" : 1997068
        },
        {
          "key" : "4",
          "doc_count" : 1995166
        },
        {
          "key" : "5",
          "doc_count" : 1993584
        }
      ]
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_15807167/article/details/85269179