SpringBoot 整合ES

限于喜欢 提交于 2020-11-19 11:50:46

1, pom文件

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

 

2,实体类 ES 存储的实体类

package com.jtl.alarm.repository.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.jtl.alarm.dto.ESAlarmDataDTO;
import com.jtl.device.service.dto.RealTimeDto;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.*;

import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: 20200601
 * @Package: com.jtl.alarm.repository.entity
 * @ClassName: ESAlarmAndDeviceDetailEntity
 * @Author: Administrator
 * @Description: ${description}
 * @Date: 2020/9/23 0023 11:11
 * @Version: 1.0
 */
@Data
@Document(indexName = "alarm_info", type = "alarm_info")
//@Mapping(mappingPath = "/mapping/dewaisi_info.json")
@Setting(settingPath = "/setting/alarm_info.json")
public class ESAlarmAndDeviceDetailEntity implements Serializable {

    /**
     * 警情id
     */
    @Id
    private Long id;

    /**
     * 报警时间   时间格式注意
     */
    @Field(format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis", store = false, type = FieldType.Date)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Field(format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis", store = false, type = FieldType.Date)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;
    /**
     * 设备名称
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart_pinyin", searchAnalyzer = "ik_smart_pinyin")
    private String deviceName;

    @Field(type = FieldType.Keyword, store = false)
    private String deviceCode;
    /**
     * 所属系统
     */
    @Field(type = FieldType.Keyword, store = false)
    private String system;
    /**
     * 设备类型
     */
    @Field(type = FieldType.Keyword, store = false)
    private String deviceType;

    @Field(type = FieldType.Keyword, store = false)
    private String deviceTypeId;
    /**
     * 设备型号
     */
    @Field(type = FieldType.Keyword, store = false)
    private String deviceModel;
    /**
     * 设备型号 id
     */
    @Field(type = FieldType.Keyword, store = false)
    private String deviceModelId;
    /**
     * 所属建筑
     */
    @Field(type = FieldType.Keyword, store = false)
    private String building;


    @Field(type = FieldType.Long, store = false)
    private Long buildingId;
    /**
     * 楼层
     */
    @Field(type = FieldType.Keyword, store = false)
    private String floor;
    /**
     * 安装位置
     */
    @Field(type = FieldType.Keyword, store = false)
    private String location;
    /**
     * 设备状态
     */
    @Field(type = FieldType.Keyword, store = false)
    private String alarmType;
    /**
     * 报警类型
     */
    @Field(type = FieldType.Keyword, store = false)
    private String alarmDetails;
    /**
     * 报警类型划分
     */
    @Field(type = FieldType.Keyword, store = false)
    private String typeCollect;
    /**
     * 警情程度
     */
    @Field(type = FieldType.Keyword, store = false)
    private String alarmLevel;
    /**
     * 单位联动状态0自动1手动
     */
    @Field(type = FieldType.Keyword, store = false)
    private String LinkState;
    /**
     * 单位id  Long类型的
     */
    @Field(type = FieldType.Long, store = false)
    private Long companyId;
    /**
     * 单位名称
     */
    @Field(type = FieldType.Keyword, store = false)
    private String companyName;
    /**
     * 报警抓拍图
     */
    @Field(type = FieldType.Text, store = false)
    private String alarmPhoto;
    /**
     * 设备di
     */
    @Field(type = FieldType.Keyword, store = false)
    private String deviceId;

    /**
     * 处理人
     */
    @Field(type = FieldType.Keyword, store = false)
    private String warnHandler;
    /**
     * 警情确认:1真实火警,2异常,3误报,4测试
     */
    @Field(type = FieldType.Keyword, store = false)
    private String warnConfirmType;
    /**
     * 所属系统id
     */
    @Field(type = FieldType.Keyword, store = false)
    private String systemId;
    /**
     * 处理说明
     */
    @Field(type = FieldType.Keyword, store = false)
    private String warnDealDes;

    /**
     * 处理时间
     */
    @Field(format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis", store = false, type = FieldType.Date)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date dealTime;


    @Field(format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis", store = false, type = FieldType.Date)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastUpdateDate;// 这个不用管 其他地方用

    //报警次数
    @Field(type = FieldType.Long, store = false)
    private Long alarmTimes;

    //"处理状态(0未处理1已处理)
    @Field(type = FieldType.Keyword, store = false)
    private String dealState;

    @Field(type = FieldType.Keyword, store = false)
    private String userName; //安全负责人

    @Field(type = FieldType.Keyword, store = false)
    private String phone; //安全负责人电话

    @Field(type = FieldType.Keyword, store = false)
    private String coordinate;

    //判断是否本单位的状态,为false时是上级单位,true为本单位
    @Field(type = FieldType.Boolean, store = false)
    private Boolean isSelfCompany;

    @Field(type = FieldType.Keyword, store = false)
    private String deviceModelImg;

    /**
     * 是否是自身单位的警情(truefalse不是)
     */
    @Field(type = FieldType.Boolean, store = false)
    private boolean myself;


    /**
     * 实时数据   对象类型的 注意
     */
    @Field(type = FieldType.Object, store = true)
    private List<ESAlarmDataDTO> realTime;

    /**
     * 设备的当前状态
     */
    @Field(type = FieldType.Keyword, store = false)
    private String warnState;

    /**
     * 父类单位id
     */
    @Field(type = FieldType.Keyword, store = false)
    private String parentCompanyId;

 

3, 然后 Repository   servcie  controller 

public interface EsAlarmRepository extends ElasticsearchRepository<ESAlarmAndDeviceDetailEntity,Long> {
}

service  controller  略过

 

4, json      实体类中的   alarm_info.json

{
  "number_of_shards": "2",
  "number_of_replicas": "1",
  "refresh_interval": "-1",
  "index.store.type": "niofs",
  "analysis": {
    "analyzer": {
      "ik_smart_pinyin": {
        "type": "custom",
        "tokenizer": "ik_smart",
        "filter": [
          "my_pinyin",
          "word_delimiter"
        ]
      },
      "ik_max_word_pinyin": {
        "type": "custom",
        "tokenizer": "ik_max_word",
        "filter": [
          "my_pinyin",
          "word_delimiter"
        ]
      }
    },
    "filter": {
      "my_pinyin": {
        "type": "pinyin",
        "keep_separate_first_letter": true,
        "keep_full_pinyin": true,
        "keep_original": true,
        "limit_first_letter_length": 16,
        "lowercase": true,
        "remove_duplicated_term": true
      }
    }
  }
}

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!