springboot集成es
springboot集成es
·
Springboot集成es
1、引入依赖
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、增加es服务配置
# es服务地址
elasticsearch:
host: 127.0.0.1
# es服务端口
port: 9350
3、实体
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "device_log")
@Setting(shards = 3)
@Builder
public class DeviceLog implements Serializable {
@Id
private String id;
/**
* 设备ID
*/
@Field(type = FieldType.Keyword)
private String devId;
/**
* 设备名称
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String deviceName;
/**
* 设备sn码
*/
@Field(type = FieldType.Keyword)
private String deviceSn;
/**
* 设备所属产品名称
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String productName;
/**
* 设备所属产品ID
*/
@Field(type = FieldType.Keyword)
private String productUuid;
/**
* 产品功能点ID的标识符 如 功能点id为101的标识符为event
*/
@Field(type = FieldType.Keyword)
private String deviceDpId;
/**
* 日志等级定义:
* 提示-TIPS
* 次要-SECONDARY
* 重要-IMPORTANT
* 紧急-URGENT
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String logLevel;
/**
* 日志事件类型定义:
* 设备上线-DEVICE_ONLINE
* 设备下线-DEVICE_OFFLINE
* 数据上报-DEVICE_DATA_REPORT
* 事件上报-DEVICE_EVENT_REPORT
* 设备重置-DEVICE_RESET
* 设备激活-DEVICE_ACTIVATION
* 固件升级-DEVICE_FIRMWARE_UPGRADE
* 设备重启-DEVICE_RESTART_UPGRADE
* 设备心跳-DEVICE_HEARTBEAT
*
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String eventType;
/**
* 事件名称
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String eventName;
/**
* 事件详情
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String eventDetail;
/**
* 来源
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String source;
/**
* 来源详情
*/
@MultiField(
mainField = @Field(type = FieldType.Text),
otherFields = {
@InnerField(suffix = "keyword", type = FieldType.Keyword)
}
)
private String sourceDetail;
/**
* 记录时间
*/
@Field(type = FieldType.Date)
private Date recordTime;
/**
* 记录时间戳 9位(精确到秒)
*/
@Field(type = FieldType.Long)
private Long recordTimestamp;
4、实现service
service:
public interface DeviceLogService {
/**
* 保存日志
* @param deviceLog 日志信息
*/
void saveDeviceLog(DeviceLog deviceLog);
/**
* 保存日志与转换时间戳
* @param deviceLog
*/
void saveDeviceLogAndChangeDateType(DeviceLog deviceLog);
/**
* 通用设备日志查询
* @return 日志信息
*/
SearchHits<DeviceLog> commonDeviceLogQuery(DeviceLogQueryDTO deviceLogQueryDTO);
}
实现类:
@Service
@Slf4j
public class DeviceLogServiceImpl implements DeviceLogService {
@Autowired
private DeviceLogRepository deviceLogRepository;
@Autowired
private ElasticsearchOperations elasticsearchOperations;
@Override
public void saveDeviceLog(DeviceLog deviceLog) {
log.info("deviceLog::"+deviceLog.toString());
deviceLogRepository.save(deviceLog);
}
@Override
public void saveDeviceLogAndChangeDateType(DeviceLog deviceLog) {
if (deviceLog.getRecordTimestamp() == null){
deviceLog.setRecordTimestamp(System.currentTimeMillis()/1000);
}
deviceLog.setRecordTime(new Date(deviceLog.getRecordTimestamp()*1000));
deviceLogRepository.save(deviceLog);
}
@Override
public SearchHits<DeviceLog> commonDeviceLogQuery(DeviceLogQueryDTO deviceLogQueryDTO) {
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
checkParamAndDataInput(deviceLogQueryDTO, boolQueryBuilder,nativeSearchQueryBuilder);
Query query = nativeSearchQueryBuilder.withSorts(SortBuilders.fieldSort(ColumnUtil.getFieldName(DeviceLog::getRecordTime)).order(SortOrder.DESC)).build();
SearchHits<DeviceLog> deviceLogPage = elasticsearchOperations.search(query, DeviceLog.class);
return deviceLogPage;
}
/**
* 检查请求参数与填充查询条件数据
*/
private void checkParamAndDataInput(DeviceLogQueryDTO deviceLogQueryDTO, BoolQueryBuilder boolQueryBuilder, NativeSearchQueryBuilder nativeSearchQueryBuilder){
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getDevId())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getDevId),deviceLogQueryDTO.getDevId())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getDeviceDpId())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getDeviceDpId),deviceLogQueryDTO.getDeviceDpId())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getDeviceName())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getDeviceName)+ Constants.CHINESE_QUERY_KEYWORD_SUFFIX,deviceLogQueryDTO.getDeviceName())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getDeviceSn())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getDeviceSn),deviceLogQueryDTO.getDeviceSn())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getProductName())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getProductName)+ Constants.CHINESE_QUERY_KEYWORD_SUFFIX,deviceLogQueryDTO.getProductName())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getProductUuid())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getProductUuid),deviceLogQueryDTO.getProductUuid())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getLogLevel())){
String desc = CommonLogEnum.getDesc(deviceLogQueryDTO.getLogLevel(), CommonLogEnum.TIPS_LOG_LEVEL.getClassification());
if (desc != null){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getLogLevel)+ Constants.CHINESE_QUERY_KEYWORD_SUFFIX,desc)));
}
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getEventType())){
String desc = CommonLogEnum.getDesc(deviceLogQueryDTO.getEventType(), CommonLogEnum.DEVICE_ONLINE_EVENT_TYPE.getClassification());
if (desc != null){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getEventType)+ Constants.CHINESE_QUERY_KEYWORD_SUFFIX,desc)));
}
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getEventName())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getEventName)+ Constants.CHINESE_QUERY_KEYWORD_SUFFIX,deviceLogQueryDTO.getEventName())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getEventDetail())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getEventDetail)+ Constants.CHINESE_QUERY_KEYWORD_SUFFIX,deviceLogQueryDTO.getEventDetail())));
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getSource())){
String desc = CommonLogEnum.getDesc(deviceLogQueryDTO.getSource(), CommonLogEnum.DEVICE_LOG_SOURCE.getClassification());
if (desc != null){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getSource)+ Constants.CHINESE_QUERY_KEYWORD_SUFFIX,desc)));
}
}
if (StringUtils.isNotEmpty(deviceLogQueryDTO.getSourceDetail())){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.termQuery(ColumnUtil.getFieldName(DeviceLog::getSourceDetail),deviceLogQueryDTO.getSourceDetail())));
}
if (deviceLogQueryDTO.getStartTime() != null && deviceLogQueryDTO.getEndTime() != null){
nativeSearchQueryBuilder.withFilter(boolQueryBuilder.must(QueryBuilders.rangeQuery(ColumnUtil.getFieldName(DeviceLog::getRecordTimestamp))
.from(deviceLogQueryDTO.getStartTime()).to(deviceLogQueryDTO.getEndTime())));
}
if (deviceLogQueryDTO.getPageFlag()!=null && deviceLogQueryDTO.getPageFlag().equals(true)){
if (deviceLogQueryDTO.getPageNum() != null && deviceLogQueryDTO.getPageSize() !=null ){
nativeSearchQueryBuilder.withPageable(PageRequest.of(deviceLogQueryDTO.getPageNum().intValue(), deviceLogQueryDTO.getPageSize().intValue()));
}
}
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)