ElasticSearch - Java项目自动创建索引

由于某些原因,我们更希望在启动项目时能够自动创建索引及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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Elasticsearch 的 Rollover 按月自动创建索引,可以使用 Elasticsearch 自带的 Curator 工具结合 Shell 脚本和 Cron 定时任务来实现。 以下是实现的大致步骤: 1. 编写 Shell 脚本,用于创建 Rollover 策略和索引模板。 2. 使用 Elasticsearch Curator 安装和配置 Curator 工具,并编写 Curator 配置文件。 3. 在 Shell 脚本中调用 Curator 执行 Rollover 操作。 4. 创建 Cron 定时任务,定期执行 Shell 脚本。 下面是一个简单的 Shell 脚本示例: ```sh #!/bin/bash # 设置 Elasticsearch 地址和端口号 ES_HOST=127.0.0.1 ES_PORT=9200 # 创建 Rollover 策略 curl -XPUT "${ES_HOST}:${ES_PORT}/_ilm/policy/my_policy" -d '{ "policy": { "phases": { "hot": { "actions": { "rollover": { "max_age": "30d", "max_size": "50gb" } } }, "delete": { "min_age": "90d", "actions": { "delete": {} } } } } }' # 创建索引模板 curl -XPUT "${ES_HOST}:${ES_PORT}/_template/my_template" -d '{ "index_patterns": ["my-index-*"], "settings": { "index": { "lifecycle": { "name": "my_policy" }, "number_of_shards": 1, "number_of_replicas": 0 } }, "mappings": { "properties": { "message": { "type": "text" }, "timestamp": { "type": "date" } } } }' # 执行 Rollover 操作 curator rollover --config /path/to/curator.yml /path/to/action_file.yml ``` 其中,`my_policy` 是 Rollover 策略的名称,`my_template` 是索引模板的名称,`my-index-*` 是索引名称的通配符,`/path/to/curator.yml` 和 `/path/to/action_file.yml` 是 Curator 的配置文件和操作文件。 使用 Cron 定时任务可以定期执行该脚本,例如: ```sh # 每月 1 日凌晨 1 点执行脚本 0 1 1 * * /path/to/script.sh ``` 这样就可以实现按月自动创建 Elasticsearch 索引了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值