elastic search

[spring data elasticsearch] document indexName 에 date pattern 넣기

kimxavi 2022. 9. 8. 23:09
반응형

상황

@Document 어노테이션 indexName 에 datePattern 넣는 방법

해결

@Document(indexName = "test-index-#{@elasticsearchIndexPattern.getToday()}", createIndex = false)
public class TestLog {
    @Id
    private String id;

    @Field(type= FieldType.Text)
    private String title;

    @Field(type= FieldType.Ip)
    private String ip;

    @Field(type= FieldType.Keyword)
    private String data;
}

위 코드에 @다음 부분에 아래 component 와 함수 호출을 넣어주면 된다.

@Component
public class ElasticsearchIndexPattern {
    public String getToday() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        return dateFormat.format(date);
    }
}

 

추가 의문

getToday() 함수는 언제언제 호출 될까?

-> ES operation 이 작동할 때마다 호출이 돼서 날짜가 바로바로 반영이 된다.

 

https://stackoverflow.com/questions/24333327/rolling-index-dynamic-index-name-in-spring-data-elasticsearch

반응형