引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
在测试类或者后台将数据库数据导入到es中
es中只放需要在页面显示的数据就行了,其他的交给详情页面
@Test
public void initEs() throws IOException {
List<Product> products = productMapper.selectProduct();
for (int i = 0; i < products.size(); i++) {
Product product = products.get(i);
String productId = product.getProductId();
String productName = product.getProductName();
Integer soldNum = product.getSoldNum();
List<ProductSku> productSkuList = product.getProductSkuList();
String skuName = "";
String skuImg = "";
Integer sellPrice = 0;
if (productSkuList.size() > 0){
skuName = productSkuList.get(0).getSkuName();
skuImg = productSkuList.get(0).getSkuImg();
sellPrice = productSkuList.get(0).getSellPrice();
}
ProductEs productEs = new ProductEs(productId, productName, skuImg, soldNum,skuName, sellPrice);
IndexRequest request = new IndexRequest("fmmallproducts");
request.id(productId);
request.source(objectMapper.writeValueAsString(productEs),XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println("indexResponse = " + indexResponse);
}
}
从es库中取数据
public ResultVo getProductsByKeyword(String keyword, int pageNum, int limit) {
ResultVo resultVo = null;
try {
// 开始查询的记录数
int start = (pageNum - 1) * limit;
// 从ES查询数据
SearchRequest request = new SearchRequest("fmmallproducts");
// 查询条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.multiMatchQuery(keyword, "productName", "productSkuName"));
//分页条件
searchSourceBuilder.from(start);
searchSourceBuilder.size(limit);
// 高亮显示
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field productName = new HighlightBuilder.Field("productName");
HighlightBuilder.Field productSkuName = new HighlightBuilder.Field("productSkuName");
highlightBuilder.field(productName);
highlightBuilder.field(productSkuName);
highlightBuilder.preTags("<label style='color:red'>");
highlightBuilder.postTags("</label>");
// 高亮封装
searchSourceBuilder.highlighter(highlightBuilder);
// 查询封装
request.source(searchSourceBuilder);
//执行搜索
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 获取命中的数据
SearchHits hits = searchResponse.getHits();
// 获取总记录数
int count = (int) hits.getTotalHits().value;
// mysql模糊查询
//String kw = "%" + keyword + "%";
//List<Product> products = productMapper.selectProductByKeyword(keyword, start, limit);
// 查询总记录数
//Example example = new Example(Product.class);
//Example.Criteria criteria = example.createCriteria();
//criteria.andLike("productName", kw);
//int count = productMapper.selectCountByExample(example);
// 计算总页数
int pageCount = count % limit == 0 ? count / limit : count / limit + 1;
Iterator<SearchHit> iterator = hits.iterator();
// 封装查询结果
ArrayList<ProductEs> products = new ArrayList<>();
while (iterator.hasNext()){
SearchHit searchHit = iterator.next();
ProductEs productEs = objectMapper.readValue(searchHit.getSourceAsString(), ProductEs.class);
// 获取高亮字段
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
HighlightField field1 = highlightFields.get("productName");
if (field1 != null){
String highLightProductName = Arrays.toString(field1.fragments());
productEs.setProductName(highLightProductName);
}
products.add(productEs);
}
// 封装返回数据
PageHelper<Product> pageHelper = new PageHelper(count, pageCount, products);
resultVo = ResultVo.ok().data("pageHelper", pageHelper);
} catch (IOException e) {
e.printStackTrace();
}
return resultVo;
}