由于某些原因,我们更希望在启动项目时能够自动创建索引及mapping,这样就不用再到各个环境中创建索引了
实践
一、createIndex属性
spring data elasticsearch包的@Document
注解中包含了一个createIndex
属性,默认为true,也就是默认开启了自动创建索引的,想要关闭也可通过设置为false来实现。
二、Bean
@Data
@Document(indexName = "XXXXXXX")
public class EsData implements Serializable {
private static final long serialVersionUID = 8245360035812215473L;
@Id
@Field( store = true, type = FieldType.Keyword)
private String id;
/**
* 次数
*/
@Field( store = true, type = FieldType.Long)
private Long count;
/**
* 覆盖率
*/
@Field( store = true, type = FieldType.Double)
private Double coverage;
/**
* 创建时间
*/
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
@Field(type = FieldType.Date,format = DateFormat.date_time,pattern = "yyyy-MM-dd HH:mm:ss",store = true)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 时间标记
*/
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
@Field(type = FieldType.Date,format = DateFormat.date_time,pattern = "yyyy-MM-dd HH:mm:ss", store = true)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date flagTime;
/**
* 时间类型
*/
@Field( store = true, type = FieldType.Keyword)
private String timeUnit;
}
二、接口
注意:不添加该接口,创建的mapping与定义的mapping不同
@Repository
public interface EsDataRepository extends ElasticsearchRepository<ScreenMonitorData,String> {
}
三、启动项目
启动项目后可以去kibana查看索引的mapping是否符合预期
GET /index_name/_mapping