ES의 시간 유형 필드를 기준으로 날짜별 집계 방법

1. 수요

ES의 레코드는 특정 시간의 필드 형식만 가지며 요구사항은 날짜 및 기타 필드별 중첩 집계입니다.

2. 방법

1. ES와 함께 제공되는 date_histogram

2. 스크립트 스크립트를 사용합니다.

3. 구체적인 설명

1. date_histogram 방법 사용

(1) 매개변수

"interval"의 값은 "day"에 할당됩니다.

"형식"의 값은 "yyyy-MM-dd"에 할당됩니다.

(2) 결과

key_as_string: 날짜

key: key_as_string이 1970년 1월 1일 0:00 이후로 경과한 밀리초의 수이므로 이 값/1000/3600/24 ​​는 일 수를 얻기 위해 두 번째 방법에서 사용할 수 있습니다.

2. 스크립트 사용

(1) 코드

"script": {
	"source": '''
	Calendar c = Calendar.getInstance();
	c.set(1970, 1 - 1, 1, 0, 0, 0);##注意月份是1-1,Calendar类的month从0开始
	c.add(Calendar.DATE, (int)(doc['startTime'].value / 1000 / 3600 / 24));##加上经过的天数
	Date date = c.getTime();
	String format = new SimpleDateFormat('YYYY-MM-dd').format(date);
	return format + ',' + doc['content.RegistrationNo.keyword'].value + ',' + doc['content.RegistrationNoColor.keyword'].value + ',' + doc['eventTypeCode.keyword'].value '''
}

(2) 키바나 결과

3. 파이썬에서의 응용

def func(index):
    query_json={
        "size": 0,
         "aggs": {
          "result": {
            "terms": {
                "script": {
                    "source":'''
                        Calendar c = Calendar.getInstance();
                        c.set(1970,1-1,1,0,0,0);
                        c.add(Calendar.DATE,(int)(doc['startTime'].value/1000/3600/24));
                        Date date = c.getTime();
                        String format = new SimpleDateFormat('YYYY-MM-dd').format(date);
                        return format+','+doc['content.RegistrationNo.keyword'].value +','+ doc['content.RegistrationNoColor.keyword'].value +','+doc['eventTypeCode.keyword'].value'''
                },"size":1000000
            }
        }
        }
}
    query = ESconn.search(index=index, body=query_json, request_timeout=360)
    data=query['aggregations']['result']['buckets']
    return data

Supongo que te gusta

Origin blog.csdn.net/m0_49621298/article/details/125337175
Recomendado
Clasificación