使用spel动态表达式设置es动态索引

本文介绍了如何使用Spring Expression Language (SpEL)动态表达式在Elasticsearch中创建索引名称,通过`@Document(indexName = “…")`注解结合`java.time.LocalDate.now()`生成当天日期作为索引名。同时,还提到了可以自定义类来进一步应用SpEL进行动态索引操作。需要注意的是,索引名称中不支持包括冒号在内的某些特殊字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import com.sangfor.taxation.common.monitor.ServiceMonitor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.elasticsearch.annotations.Document;
@EqualsAndHashCode(callSuper = true)
@Data
@Document(indexName = "service_monitor_#{T(java.time.LocalDate).now().toString()}")
public class ServiceMonitorEntity extends EsBaseEntity{

    /**
     * 应用id
     */
    private Integer appId;

    /**
     * 服务id
     */
    private Integer serviceId;

    /**
     * 服务名
     */
    private String serviceName;

    /**
     * 服务监控信息
     */
    private ServiceMonitor monitor; 
    
    /**
     * 服务新建连接最大值
     */
    private Long maxConnects;
    /**
     * 接入节点虚拟ip
     */
    private String inNodeVirtualIp;
    /**
     * 发布节点虚拟ip
     */
    private String appNodeVirtualIp;

}

@Document(indexName = “service_monitor_#{T(java.time.LocalDate).now().toString()}”)

可以看到有个大括号表达式,里卖弄可以设置函数表达式形成动态索引。

除此之外:es动态索引spel还可以自定义一个类去使用:

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: wkn
 * @date: 2021-06-17 16:46
 */
public final class DateString  {

    public static String now() {
        return new SimpleDateFormat("yyyy_MM_dd-HH-mm-ss").format(new Date());
    }

}
@EqualsAndHashCode(callSuper = true)
@Data
@Document(indexName = "service_monitor_#{T(com.example.myself.persistent.timec.DateString).now()}")
public class ServiceMonitorEntity extends EsBaseEntity{

此时需注意:
indexName有以下规范限制:

Name of the Elasticsearch index.
Lowercase only
 , *, ?, ", <, >, |, ` ` (space character), ,, #/li>
Cannot start with -, _, +
Cannot be . or ..
Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster)

除了以上特殊字符不支持,冒号 :也不支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值