本文讲述了SpringBoot
整合Spring Data Elasticsearch
的详细过程,主要使用了ElasticsearchRestTemplate、ElasticsearchRepository
等类来实现Index
、Document
CRUD操作的Java Api。
在开始之前,我们首先需要选择Es对应版本的jar包,本文所使用的Es版本为7.9.3,查看spring官网可知,对应的springboot版本为2.4.x。
创建一个maven项目,就可以操作起来了。
引入以下jar包:
org.springframework.boot
spring-boot-starter-parent
2.4.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.projectlombok
lombok
1.18.12
compile
com.alibaba
fastjson
1.2.71
spring.data.elasticsearch.repositories.enabled = true
spring.elasticsearch.rest.uris=localhost:9200
@Configuration
public class ElasticsearchRestTemplateConfig extends AbstractElasticsearchConfiguration {
@Value("${spring.elasticsearch.rest.uris}")
private String uris;
@Override
public RestHighLevelClient elasticsearchClient() {
ClientConfiguration configuration = ClientConfiguration.builder()
.connectedTo(uris)
.build();
return RestClients.create(configuration).rest();
}
}
Model类似于数据库实体,不过此处所映射的是Index和Document的字段。注解含义可查看Spring官网。
@Data
@Document(indexName = "order", shards = 1, replicas = 1)
public class Order implements Serializable {
@Id
private Integer id;
@Field(type = FieldType.Keyword)
private Long orderNo;
@Field(type = FieldType.Integer)
private Integer orderType;
@Field(type = FieldType.Long)
private Long orderAmount;
@Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_max_word")
private String orderDesc;
@Field(type = FieldType.Keyword, analyzer = "ik_smart", searchAnalyzer = "ik_max_word")
private String username;
@Field(type = FieldType.Keyword, analyzer = "ik_smart", searchAnalyzer = "ik_max_word")
private String userPhone;
private Map> highlights;
}
ElasticsearchRepository接口封装了Document的CRUD操作,我们直接定义接口继承它即可。
public interface OrderRepository extends ElasticsearchRepository {
}
public interface OrderService {
void saveAll(List orders);
Order findById(Integer id);
void deleteById(Integer id);
void updateById(Order order);
PageResponse findList(Order order, Integer pageIndex, Integer pageSize);
PageResponse findAll(Integer pageIndex, Integer pageSize);
PageResponse findHighlight(Order order, Integer pageIndex, Integer pageSize);
}
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
OrderRepository orderRepository;
@Autowired
ElasticsearchRestTemplate elasticsearchRestTemplate;
@Override
public void saveAll(List orders) {
orderRepository.saveAll(orders);
}
@Override
public void deleteById(Integer id) {
orderRepository.deleteById(id);
}
@Override
public void updateById(Order order) {
orderRepository.save(order);
}
@Override
public PageResponse findList(Order order, Integer pageIndex, Integer pageSize) {
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()
.and(new Criteria("orderDesc").contains(order.getOrderDesc()))
.and(new Criteria("orderNo").is(order.getOrderNo())))
.setPageable(PageRequest.of(pageIndex, pageSize));
SearchHits searchHits = elasticsearchRestTemplate.search(criteriaQuery, Order.class);
List result = searchHits.get().map(SearchHit::getContent).collect(Collectors.toList());
PageResponse pageResponse = new PageResponse();
pageResponse.setTotal(searchHits.getTotalHits());
pageResponse.setResult(result);
return pageResponse;
}
@Override
public PageResponse findAll(Integer pageIndex, Integer pageSize) {
Page page = orderRepository.findAll(PageRequest.of(pageIndex, pageSize));
PageResponse pageResponse = new PageResponse();
pageResponse.setTotal(page.getTotalElements());
pageResponse.setResult(page.getContent());
return pageResponse;
}
@Override
public PageResponse findHighlight(Order order, Integer pageIndex, Integer pageSize) {
if (order == null) {
PageResponse pageResponse = new PageResponse();
pageResponse.setTotal(0L);
pageResponse.setResult(new ArrayList<>());
return pageResponse;
}
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()
.and(new Criteria("orderNo").is(order.getOrderNo()))
.and(new Criteria("orderDesc").contains(order.getOrderDesc())))
.setPageable(PageRequest.of(pageIndex, pageSize));
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("orderNo").field("orderDesc");
highlightBuilder.requireFieldMatch(false);
highlightBuilder.preTags("");
highlightBuilder.postTags("
");
HighlightQuery highlightQuery = new HighlightQuery(highlightBuilder);
criteriaQuery.setHighlightQuery(highlightQuery);
SearchHits searchHits = elasticsearchRestTemplate.search(criteriaQuery, Order.class);
List result = searchHits.get().map(e -> {
Order element = e.getContent();
element.setHighlights(e.getHighlightFields());
return element;
}).collect(Collectors.toList());
PageResponse pageResponse = new PageResponse();
pageResponse.setTotal(searchHits.getTotalHits());
pageResponse.setResult(result);
return pageResponse;
}
@Override
public Order findById(Integer id) {
return orderRepository.findById(id).orElse(null);
}
@RequestMapping("/index/")
@RestController
public class IndexController {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
* 创建索引
*/
@GetMapping("create")
public String create(@RequestParam String indexName) {
IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of(indexName));
if (indexOperations.exists()) {
return "索引已存在";
}
indexOperations.create();
return "索引创建成功";
}
/**
* 删除索引
*/
@GetMapping("delete")
public String delete(@RequestParam String indexName) {
IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of(indexName));
indexOperations.delete();
return "索引删除成功";
}
}
@RequestMapping("/doc/")
@RestController
public class DocController {
@Autowired
OrderService orderService;
/**
* 批量创建
*/
@PostMapping("saveBatch")
public String saveBatch(@RequestBody List orders) {
if (CollectionUtils.isEmpty(orders)) {
return "文档不能为空";
}
orderService.saveAll(orders);
return "保存成功";
}
/**
* 根据id删除
*/
@GetMapping("deleteById")
public String deleteById(@RequestParam Integer id) {
orderService.deleteById(id);
return "删除成功";
}
/**
* 根据id更新
*/
@PostMapping("updateById")
public String updateById(@RequestBody Order order) {
orderService.updateById(order);
return "更新成功";
}
/**
* 根据id搜索
*/
@GetMapping("findById")
public String findById(@RequestParam Integer id) {
return JSON.toJSONString(orderService.findById(id));
}
/**
* 分页搜索所有
*/
@GetMapping("findAll")
public String findAll(@RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
return JSON.toJSONString(orderService.findAll(pageIndex, pageSize));
}
/**
* 条件分页搜索
*/
@GetMapping("findList")
public String findList(@RequestBody Order order, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
return JSON.toJSONString(orderService.findList(order, pageIndex, pageSize));
}
/**
* 条件高亮分页搜索
*/
@GetMapping("findHighlight")
public String findHighlight(@RequestBody(required = false) Order order, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
return JSON.toJSONString(orderService.findHighlight(order, pageIndex, pageSize));
}
}