一、定义ES索引
java
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain = true)
@Document(indexName = "charge_pile_log" + Constants.ES_TIME_DYNAMIC_INDEX) //(索引名称动态,前面固定,后面月份)
public class ChargePileLogESDomain {
@Id
private String id;
@Field(type = FieldType.Integer)
private Integer businessType;
}
public static final String ES_TIME_DYNAMIC_INDEX = "-#{ T(com.energy.cloud.log.service.util.DateUtil).getUTCMonth() }";
二、定义Repository
java
import com.energy.cloud.log.service.domain.ChargePileLogESDomain;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ChargePileLogRepository extends ElasticsearchRepository<ChargePileLogESDomain, String> {
}
三、存储数据
java
@Resource
private ChargePileLogRepository chargePileLogRepository;
List<ChargePileLogESDomain> saveList = new ArrayList<>();
chargePileLogRepository.saveAll(saveList);
四、查询数据(索引名称带通配符)
java
SearchRequest request = new SearchRequest(charge_pile_log*);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
request.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
五、常量类和工具类
java
public static final String ES_TIME_DYNAMIC_INDEX = "-#{ T(com.energy.cloud.log.service.util.DateUtil).getUTCMonth() }";
public class DateUtil {
public static String getUTCMonth(){
//设置日期格式为yyyy-mm
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM");
//获取拾取为UTC的时间
DateTime now = DateTime.now(DateTimeZone.UTC);
return fmt.print(now);
}
}