ES--script進行時間聚合(非使用date_histogram)

由於產品線上 日期時間使用的非 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
        }
      ]
    }
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章