多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 使用的对象 ``` @Autowired private ElasticsearchOperations restTemplate; ``` # 添加索引的相关api ``` IndexOperations indexOperations = restTemplate.indexOps(TestIndex.class); // 判断是否已经有索引 boolean exists = indexOperations.exists(); System.out.println(exists); // 创建索引, 但是这个是没有做映射的("mappings" 无信息) boolean b = indexOperations.create(); System.out.println(b); // 根据实体类获取映射关系 Document mapping = indexOperations.createMapping(TestIndex.class); // 把映射关系添加到索引中 indexOperations.putMapping(mapping); ``` # 删除索引 ``` IndexOperations indexOperations = restTemplate.indexOps(TestIndex.class); boolean delete = indexOperations.delete(); System.out.println(delete); ``` # TestIndex ``` import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.DateFormat; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; @AllArgsConstructor @NoArgsConstructor @Builder @Data // 索引名称 @Document(indexName = "test_index") public class TestIndex implements Serializable { private static final long serialVersionUID = 1L; /** 数据来源是 CompanyContacts 表 和 CompanyContactsQcc 表 , sql 又不放一张表了 **/ @Id /** 业务id, CompanyContactsQcc 的主键 */ @Field(type = FieldType.Keyword) private String id; /**更新日期*/ @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @Field(type = FieldType.Date, format = DateFormat.basic_date) private Date updateTime; /**企业名称 设置为text 可以分词 */ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String companyName; public void setCompanyName(String companyName) { if (StringUtils.isNotBlank(companyName)) { this.companyName = companyName; if (companyName.length() > 10) { this.companyNameSort = companyName.substring(0, 10); } else { this.companyNameSort = companyName; } } } /**企业名称 用来排序的 存放 company 前10个字*/ @Field(type = FieldType.Keyword) private String companyNameSort; /**法定代表人 */ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String legalPerson; /**姓名 手动设置为keyword 但同时也就不能分词 */ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String realname; /** 职务 **/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String post; /**手机号*/ @Field(type = FieldType.Keyword) private String phone; /**更多手机号*/ @Field(type = FieldType.Keyword) private String morePhone; /**企业地址*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String companyAddress; /**行业*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String industry; /**所属省份*/ @Field(type = FieldType.Keyword) private String pname; /**所属城市*/ @Field(type = FieldType.Keyword) private String cityname; /**所属区县*/ @Field(type = FieldType.Keyword) private String adname; /**统一社会信用代码*/ @Field(type = FieldType.Keyword) private String usci; /**登记状态*/ @Field(type = FieldType.Keyword) private String registrationStatus; /**注册资本*/ @Field(type = FieldType.Auto) private BigDecimal registrationCapital; /**成立日期*/ @Field(type = FieldType.Date, format = DateFormat.basic_date) @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd") @DateTimeFormat(pattern="yyyy-MM-dd") private Date registrationDate; /**核准日期*/ @Field(type = FieldType.Date, format = DateFormat.basic_date) @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd") @DateTimeFormat(pattern="yyyy-MM-dd") private Date approvalDate; /**邮箱*/ @Field(type = FieldType.Keyword) private String email; /**更多邮箱*/ @Field(type = FieldType.Keyword) private String moreEmail; /**纳税人识别号*/ @Field(type = FieldType.Keyword) private String taxpayerId; /**注册号*/ @Field(type = FieldType.Keyword) private String registrationNumber; /**组织机构代码*/ @Field(type = FieldType.Keyword) private String organizationCode; /**参保人数*/ @Field(type = FieldType.Integer) private Integer contributorsIn; /**企业类型*/ @Field(type = FieldType.Keyword) private String companyType; /**曾用名*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String usedName; /**英文名*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String englishName; /**网址*/ @Field(type = FieldType.Keyword) private String website; /**最新年报地址*/ @Field(type = FieldType.Keyword) private String annalsAddress; /**经营范围*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String businessScope; /**来源 source =0 是 企查查, source = 1 是 内部数据 */ @Field(type = FieldType.Integer) @ApiModelProperty(value = "来源 source =0 是 企查查, source = 1 是 内部数据") private Integer source; /** CompanyContacts 表独有的扩展字段 **/ /**扩展字段1*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String field1; /**扩展字段2*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String field2; /**扩展字段3*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String field3; /**扩展字段4*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String field4; /**扩展字段5*/ @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_max_word") private String field5; } ```