Commit b8f0bd0a by 赵剑炜

添加ES,增加ES的增删改接口

parent 116fff11
...@@ -47,6 +47,12 @@ ...@@ -47,6 +47,12 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.swagger</groupId> <groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId> <artifactId>swagger-annotations</artifactId>
......
package com.junmp.jyzb.Repository;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junmp.jyzb.entity.ES.Blog;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.awt.print.Pageable;
import java.util.List;
public interface BlogRepository extends ElasticsearchRepository<Blog, Long> {
}
\ No newline at end of file
package com.junmp.jyzb.Repository;
import com.junmp.jyzb.document.ProductDocument;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.stereotype.Component;
public interface ProductDocumentRepository extends ElasticsearchRepository<ProductDocument,String> {
}
package com.junmp.jyzb.Repository;
import com.junmp.jyzb.entity.EquipmentType;
import com.junmp.jyzb.entity.PubOrg;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface PubOrgRepository extends ElasticsearchRepository<PubOrg, Long> {
}
package com.junmp.jyzb.Repository;
import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto;
import com.junmp.jyzb.entity.ES.Blog;
import com.junmp.jyzb.entity.EquipmentType;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface TypeRepository extends ElasticsearchRepository<EquipmentType, Long> {
}
package com.junmp.jyzb.controller.ES;
import com.junmp.jyzb.Repository.BlogRepository;
import com.junmp.jyzb.entity.ES.Blog;
import com.junmp.jyzb.service.EquipmentTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Api(tags = "增删改查(文档)")
@RestController
@Slf4j
@RequestMapping("crud")
public class CrudController {
@Autowired
private BlogRepository blogRepository;
@Resource
public EquipmentTypeService equipmentTypeService;
@ApiOperation("测试")
@PostMapping("testDocument")
public Object testDocument() {
return equipmentTypeService.addEs();
}
@ApiOperation("添加单个文档")
@PostMapping("addDocument")
public Blog addDocument() {
Long id = 1L;
Blog blog = new Blog();
blog.setBlogId(id);
blog.setTitle("巡特警" + id);
blog.setContent("巡特警手铐" + id);
blog.setAuthor("Tony");
blog.setCategory("ElasticSearch");
blog.setCreateTime(new Date());
blog.setStatus(1);
blog.setSerialNum(id.toString());
Long id1 = 2L;
Blog blog1 = new Blog();
blog1.setBlogId(id1);
blog1.setTitle("巡特警" + id1);
blog1.setContent("巡特警盾牌" + id1);
blog1.setAuthor("Tony");
blog1.setCategory("ElasticSearch");
blog1.setCreateTime(new Date());
blog1.setStatus(1);
blog1.setSerialNum(id1.toString());
blogRepository.save(blog1);
return blogRepository.save(blog);
}
@ApiOperation("添加多个文档")
@PostMapping("addDocuments")
public Object addDocuments(Integer count) {
List<Blog> blogs = new ArrayList<>();
for (int i = 1; i <= count; i++) {
Long id = (long)i;
Blog blog = new Blog();
blog.setBlogId(id);
blog.setTitle("Spring Data ElasticSearch学习教程" + id);
blog.setContent("这是添加单个文档的实例" + id);
blog.setAuthor("Tony");
blog.setCategory("ElasticSearch");
blog.setCreateTime(new Date());
blog.setStatus(1);
blog.setSerialNum(id.toString());
blogs.add(blog);
}
return blogRepository.saveAll(blogs);
}
/**
* 跟新增是同一个方法。若id已存在,则修改。
* 无法只修改某个字段,只能覆盖所有字段。若某个字段没有值,则会写入null。
* @return 成功写入的数据
*/
@ApiOperation("修改单个文档")
@PostMapping("editDocument")
public Blog editDocument() {
Long id = 1L;
Blog blog = new Blog();
blog.setBlogId(id);
blog.setTitle("Spring Data ElasticSearch学习教程" + id);
blog.setContent("这是修改单个文档的实例" + id);
// blog.setAuthor("Tony");
// blog.setCategory("ElasticSearch");
// blog.setCreateTime(new Date());
// blog.setStatus(1);
// blog.setSerialNum(id.toString());
return blogRepository.save(blog);
}
@ApiOperation("查找单个文档")
@GetMapping("findById")
public Blog findById(Long id) {
return blogRepository.findById(id).get();
}
@ApiOperation("删除单个文档")
@PostMapping("deleteDocument")
public String deleteDocument(Long id) {
blogRepository.deleteById(id);
return "success";
}
@ApiOperation("删除所有文档")
@PostMapping("deleteDocumentAll")
public String deleteDocumentAll() {
blogRepository.deleteAll();
return "success";
}
}
package com.junmp.jyzb.controller;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EsSearchController {
}
package com.junmp.jyzb.entity.ES;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
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 java.util.Date;
@Data
@Document(indexName = "blog", shards = 1, replicas = 1)
public class Blog {
//此项作为id,不会写到_source里边。
@Id
private Long blogId;
@Field(type = FieldType.Text)
private String title;
@Field(type = FieldType.Text)
private String content;
@Field(type = FieldType.Text)
private String author;
//博客所属分类。
@Field(type = FieldType.Keyword)
private String category;
//0: 未发布(草稿) 1:已发布 2:已删除
@Field(type = FieldType.Integer)
private int status;
//序列号,用于给外部展示的id
@Field(type = FieldType.Keyword)
private String serialNum;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
@Field(type= FieldType.Date, format= DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss.SSS")
private Date createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
@Field(type=FieldType.Date, format=DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss.SSS")
private Date updateTime;
}
...@@ -8,18 +8,22 @@ import java.util.Date; ...@@ -8,18 +8,22 @@ import java.util.Date;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@ApiModel(value = "com-junmp-jyzb-domain-EquipmentType") @ApiModel(value = "com-junmp-jyzb-domain-EquipmentType")
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@TableName("base_equipment_type") @TableName("base_equipment_type")
@Document(indexName = "type")
public class EquipmentType implements Serializable { public class EquipmentType implements Serializable {
/** /**
* 装备类型ID * 装备类型ID
*/ */
@ApiModelProperty(value = "装备类型ID") @ApiModelProperty(value = "装备类型ID")
@TableId(value = "id", type = IdType.ASSIGN_UUID) @TableId(value = "id", type = IdType.ASSIGN_UUID)
@Id
private String id; private String id;
/** /**
......
...@@ -11,6 +11,8 @@ import java.util.Date; ...@@ -11,6 +11,8 @@ import java.util.Date;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/** /**
* 组织机构信息 * 组织机构信息
...@@ -19,11 +21,17 @@ import lombok.NoArgsConstructor; ...@@ -19,11 +21,17 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@TableName("pub_org") @TableName("pub_org")
@Document(indexName = "org")
public class PubOrg implements Serializable { public class PubOrg implements Serializable {
//此项作为id,不会写到_source里边。
@Id
private String id;
/** /**
* 主键 * 主键
*/ */
@TableId(type = IdType.AUTO, value = "org_id") @TableId(type = IdType.AUTO, value = "org_id")
private Long orgId; private Long orgId;
/** /**
* 主键 * 主键
......
...@@ -12,22 +12,14 @@ import java.util.List; ...@@ -12,22 +12,14 @@ import java.util.List;
public interface EquipmentTypeService extends IService<EquipmentType> { public interface EquipmentTypeService extends IService<EquipmentType> {
Object addEs();
String addEquipment(UpdateEquipmentTypeReq req ); String addEquipment(UpdateEquipmentTypeReq req );
Boolean deleteEquipment(UpdateEquipmentTypeReq req); Boolean deleteEquipment(UpdateEquipmentTypeReq req);
Boolean updateEquipment(UpdateEquipmentTypeReq msg); Boolean updateEquipment(UpdateEquipmentTypeReq msg);
Boolean changeEquipmentState(UpdateEquipmentTypeReq req); Boolean changeEquipmentState(UpdateEquipmentTypeReq req);
List<EquipmentTypeDto> getEquipmentList(QueryEquipmentTypeReq req); List<EquipmentTypeDto> getEquipmentList(QueryEquipmentTypeReq req);
EquipmentTypeDto getEquipment(QueryEquipmentTypeReq req ); EquipmentTypeDto getEquipment(QueryEquipmentTypeReq req );
List<EquipmentTypeDto> getLowType(QueryEquipmentTypeReq orgId); List<EquipmentTypeDto> getLowType(QueryEquipmentTypeReq orgId);
ResponseResult setTypeParentIds(); ResponseResult setTypeParentIds();
List<EquipmentTreeDto> GetTypeTree(); List<EquipmentTreeDto> GetTypeTree();
} }
package com.junmp.jyzb.service;
import com.junmp.jyzb.document.ProductDocument;
import java.util.List;
/**
* @author zhoudong
* @version 0.1
* @date 2018/12/13 15:32
*/
public interface EsSearchService extends BaseSearchService<ProductDocument> {
/**
* 保存
* @auther: zhoudong
* @date: 2018/12/13 16:02
*/
void save(ProductDocument... productDocuments);
/**
* 删除
* @param id
*/
void delete(String id);
/**
* 清空索引
*/
void deleteAll();
/**
* 根据ID查询
* @param id
* @return
*/
ProductDocument getById(String id);
/**
* 查询全部
* @return
*/
List<ProductDocument> getAll();
}
...@@ -14,6 +14,8 @@ import com.junmp.jyzb.entity.PubOrg; ...@@ -14,6 +14,8 @@ import com.junmp.jyzb.entity.PubOrg;
public interface PubOrgService extends IService<PubOrg> { public interface PubOrgService extends IService<PubOrg> {
///添加至ES
Object AddToEs();
ResponseResult setShortName(Map<String, Object> orgId); ResponseResult setShortName(Map<String, Object> orgId);
......
...@@ -44,7 +44,7 @@ public class BaseSearchServiceImpl<T> implements BaseSearchService<T> { ...@@ -44,7 +44,7 @@ public class BaseSearchServiceImpl<T> implements BaseSearchService<T> {
@Override @Override
public Object query(String keyword, Class<T> clazz) { public Object query(String keyword, Class<T> clazz) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
Object querys= searchSourceBuilder.query(QueryBuilders.termsQuery(keyword, clazz)) Object querys= searchSourceBuilder.query(QueryBuilders.matchQuery(keyword, clazz))
.sort(SortBuilders.scoreSort().order(SortOrder.DESC)); .sort(SortBuilders.scoreSort().order(SortOrder.DESC));
return querys; return querys;
} }
......
...@@ -3,6 +3,8 @@ package com.junmp.jyzb.service.impl; ...@@ -3,6 +3,8 @@ package com.junmp.jyzb.service.impl;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junmp.jyzb.Repository.BlogRepository;
import com.junmp.jyzb.Repository.TypeRepository;
import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto; import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto;
import com.junmp.jyzb.api.bean.dto.EquipmentTreeDto; import com.junmp.jyzb.api.bean.dto.EquipmentTreeDto;
import com.junmp.jyzb.api.bean.query.QueryEquipmentTypeReq; import com.junmp.jyzb.api.bean.query.QueryEquipmentTypeReq;
...@@ -16,6 +18,7 @@ import com.junmp.jyzb.service.SupplierTypeService; ...@@ -16,6 +18,7 @@ import com.junmp.jyzb.service.SupplierTypeService;
import com.junmp.jyzb.utils.*; import com.junmp.jyzb.utils.*;
import com.junmp.v2.common.exception.base.ServiceException; import com.junmp.v2.common.exception.base.ServiceException;
import com.junmp.v2.common.util.BeanPlusUtil; import com.junmp.v2.common.util.BeanPlusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import com.junmp.jyzb.mapper.EquipmentTypeMapper; import com.junmp.jyzb.mapper.EquipmentTypeMapper;
...@@ -33,8 +36,8 @@ public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, E ...@@ -33,8 +36,8 @@ public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, E
@Resource @Resource
private EquipmentTypeMapper equipmentTypeMapper; private EquipmentTypeMapper equipmentTypeMapper;
@Autowired
private TypeRepository typeRepository;
@Resource @Resource
private SupplierService supplierService; private SupplierService supplierService;
@Resource @Resource
...@@ -54,6 +57,14 @@ public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, E ...@@ -54,6 +57,14 @@ public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, E
return collect; return collect;
} }
@Override
public Object addEs() {
QueryEquipmentTypeReq req =new QueryEquipmentTypeReq();
req.setType(0);
List<EquipmentType> esDto=this.list();
return typeRepository.saveAll(esDto);
}
@Transactional @Transactional
@Override @Override
public String addEquipment(UpdateEquipmentTypeReq req) { public String addEquipment(UpdateEquipmentTypeReq req) {
......
package com.junmp.jyzb.service.impl;
import com.alibaba.fastjson.JSON;
import com.junmp.jyzb.Repository.ProductDocumentRepository;
import com.junmp.jyzb.document.ProductDocument;
import com.junmp.jyzb.service.EsSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* elasticsearch 搜索引擎 service实现
* @author zhoudong
* @version 0.1
* @date 2018/12/13 15:33
*/
@Service
public class EsSearchServiceImpl extends BaseSearchServiceImpl<ProductDocument> implements EsSearchService {
private Logger log = LoggerFactory.getLogger(getClass());
@Resource
private ElasticsearchRestTemplate elasticsearchTemplate;
@Resource
private ProductDocumentRepository productDocumentRepository;
@Override
public void save(ProductDocument ... productDocuments) {
elasticsearchTemplate.putMapping(ProductDocument.class);
if(productDocuments.length > 0){
log.info("【保存索引】:{}", JSON.toJSONString(productDocumentRepository.saveAll(Arrays.asList(productDocuments))));
}
}
@Override
public void delete(String id) {
productDocumentRepository.deleteById(id);
}
@Override
public void deleteAll() {
productDocumentRepository.deleteAll();
}
@Override
public ProductDocument getById(String id) {
return productDocumentRepository.findById(id).get();
}
@Override
public List<ProductDocument> getAll() {
List<ProductDocument> list = new ArrayList<>();
productDocumentRepository.findAll().forEach(list::add);
return list;
}
}
...@@ -3,6 +3,8 @@ package com.junmp.jyzb.service.impl; ...@@ -3,6 +3,8 @@ package com.junmp.jyzb.service.impl;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junmp.jyzb.Repository.PubOrgRepository;
import com.junmp.jyzb.Repository.TypeRepository;
import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto; import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto;
import com.junmp.jyzb.api.bean.dto.OrgDto; import com.junmp.jyzb.api.bean.dto.OrgDto;
import com.junmp.jyzb.api.bean.dto.OrgTreeDto; import com.junmp.jyzb.api.bean.dto.OrgTreeDto;
...@@ -42,7 +44,8 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem ...@@ -42,7 +44,8 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
@Autowired @Autowired
private RedisUtils redisUtils; private RedisUtils redisUtils;
@Autowired
private PubOrgRepository pubOrgRepository;
//例:传入abc,OrderMQReceiver ==>bc 删掉开头的字符串,不是就不删除 //例:传入abc,OrderMQReceiver ==>bc 删掉开头的字符串,不是就不删除
public static String removePrefix(String name, String deleteName) { public static String removePrefix(String name, String deleteName) {
...@@ -130,6 +133,12 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem ...@@ -130,6 +133,12 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
} }
@Override @Override
public Object AddToEs() {
List<PubOrg> listOrg= this.list();
return pubOrgRepository.saveAll(listOrg);
}
@Override
public ResponseResult setShortName(Map<String, Object> orgId) { public ResponseResult setShortName(Map<String, Object> orgId) {
List<PubOrg> menuList = pubOrgMapper.selectAllOrg(); List<PubOrg> menuList = pubOrgMapper.selectAllOrg();
......
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id":{
"type":"long"
},
"title": {
"type": "text"
},
"content": {
"type": "text"
},
"author":{
"type": "text"
},
"category":{
"type": "keyword"
},
"createTime": {
"type": "date",
"format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"updateTime": {
"type": "date",
"format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
},
"status":{
"type":"integer"
},
"serialNum": {
"type": "keyword"
}
}
}
}
\ No newline at end of file
{
"properties": {
"createTime": {
"type": "long"
},
"productDesc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"productName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"updateTime": {
"type": "long"
}
}
}
\ No newline at end of file
...@@ -53,8 +53,8 @@ spring: ...@@ -53,8 +53,8 @@ spring:
enabled: false enabled: false
eventregistry: eventregistry:
enabled: false enabled: false
#mybatis plus 设置 #mybatis plus 设置
mybatis-plus: mybatis-plus:
configuration: configuration:
cache-enabled: true cache-enabled: true
......
#server: #server:
# port: 10030 # port: 10030
# 应用服务器 # 应用服务器
server: server:
tomcat: tomcat:
uri-encoding: UTF-8 #tomcat编码 uri-encoding: UTF-8 #tomcat编码
port: 10030 #tomcat端口 port: 10030 #tomcat端口
spring: spring:
elasticsearch:
rest:
uris: http://192.168.3.188:9200
main: main:
#bea同名类进行注册时,准许覆盖注册 #bea同名类进行注册时,准许覆盖注册
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
...@@ -35,6 +40,8 @@ spring: ...@@ -35,6 +40,8 @@ spring:
serialization: serialization:
indent_output: false indent_output: false
property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategy$PascalCaseStrategy property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategy$PascalCaseStrategy
flyway: flyway:
enable: ture enable: ture
locations: classpath:db/migration locations: classpath:db/migration
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论