package com.example.springboot_es.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.example.springboot_es.entity.Goods;
import com.example.springboot_es.mapper.GoodsMapper;
import com.example.springboot_es.noSql.repository.GoodsRepository;
import com.example.springboot_es.service.GoodsService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Service(value = "goodsimpl")
public class GoodsServiceImpl implements GoodsService {
private static final Logger LOGGER = LoggerFactory.getLogger(GoodsServiceImpl.class);
//通过 注解 注入 dao层接口对象:将xml的实现注入到 接口对象中
@Autowired(required = false)
private GoodsMapper goodsMapper;
@Autowired
private GoodsRepository goodsRepository;
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public int importAll55(Goods goods) {
//查询所有商品
List<Goods> esProductList = goodsMapper.selectAll(goods);
//接收对象集合,实现批量新增到ES:操作对象数据的 存储库中
Iterable<Goods> esProductIterable = goodsRepository.saveAll(esProductList);
System.out.println("esProductIterable====>>"+esProductIterable.toString());
//统计 新增的数量
Iterator<Goods> iterator = esProductIterable.iterator();
int result = 0;
while (iterator.hasNext()) {
result++;
iterator.next();
}
return result;
}
@Override
public void delete(Long id) {
//根据商品ID从ES库中:移除数据
goodsRepository.deleteById(id);
}
/**保存*/
@Override
public Goods create(Long id) {
Goods result = null;
//通过商品id,从数据库中查询商品数据
List<Goods> esProductList = (List<Goods>) goodsMapper.selectByPrimaryKey(Math.toIntExact(id));
if (esProductList.size() > 0) {
//查询出数据后:出入ES库中
Goods esProduct = esProductList.get(0);
result = goodsRepository.save(esProduct);
}
return result;
}
@Override
public void delete(List<Long> ids) {
//判断集合对象 是否为空
if (!CollectionUtils.isEmpty(ids)) {
//定义集合对象
List<Goods> esProductList = new ArrayList<>();
//遍历:传入的ids集合
for (Long id : ids) {
//分组为ES库中的对象
Goods esProduct = new Goods();
esProduct.setRid(Math.toIntExact(id));
//传入对象
esProductList.add(esProduct);
}
//批量: 移除库中对象
goodsRepository.deleteAll(esProductList);
}
}
@Override
public Page<Goods> search(String keyword, Integer pageNum, Integer pageSize) {
/*
* PageRequest:spring-data类的一个公共类
* Pageable:封装了分页的参数,当前页,每页的显示的条数。
*/
Pageable pageable = PageRequest.of(pageNum, pageSize);
//NameOrSubTitleOrKeywords :根据名称 简介 关键字:从ES中 检索数据
return goodsRepository.findByName( keyword,keyword,keyword,pageable);
}
/**
* 不带高亮查询
*/
public List<Goods> noHightQuery(String keyword) throws IOException {
//crm 就是@Document(indexName="crm")的名字,是索引也就是表名
SearchRequest searchRequest=new SearchRequest("crm");
//构建搜索条件
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder()
//在discusspost索引的name字段中查询
.query(QueryBuilders.multiMatchQuery(keyword,"name"))
.sort(SortBuilders.fieldSort("rid").order(SortOrder.ASC)) //价格
.from(0) //指定从那条开始查询
.size(10);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse=restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
List<Goods> list=new ArrayList<>();
for (SearchHit hit: searchResponse.getHits().getHits()) {
Goods goods= JSONObject.parseObject(hit.getSourceAsString(),Goods.class);
System.out.println(goods);
list.add(goods);
}
return list;
}
/**
* 带高亮查询
*/
public List<Goods> highlightQuery(String keyword) throws IOException {
//crm 就是@Document(indexName="crm")的名字,是索引也就是表名
SearchRequest searchRequest=new SearchRequest("crm");
//高亮
HighlightBuilder highlightBuilder=new HighlightBuilder();
//将分词器检索的字段设置为高亮
highlightBuilder.field("name");
//当全局高亮.field设置为:"*"时,覆盖无效
// highlightBuilder.requireFieldMatch(false).field("*");
highlightBuilder.requireFieldMatch(false);
highlightBuilder.preTags("<span style='color:red'>");
highlightBuilder.postTags("</span>");
//构建搜索条件
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder()
//在discusspost索引的name字段中查询
.query(QueryBuilders.multiMatchQuery(keyword,"name"))
.sort(SortBuilders.fieldSort("rid").order(SortOrder.ASC)) //id
.from(0) //指定从那条开始查询
.size(10) //需要查出的总记录条数
.highlighter(highlightBuilder); //高亮
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse=restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
List<Goods> list=new ArrayList<>();
for (SearchHit hit: searchResponse.getHits().getHits()) {
Goods goods= JSONObject.parseObject(hit.getSourceAsString(),Goods.class);
//处理高亮显示的结果
HighlightField nameField=hit.getHighlightFields().get("name");
if(nameField!=null){
goods.setName(nameField.getFragments()[0].toString());
}
System.out.println(goods);
list.add(goods);
}
return list;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springBoot_es-master.zip (21个子文件)
springBoot_es-master
mvnw.cmd 6KB
pom.xml 5KB
src
test
java
com
example
springboot_es
SpringBootEsApplicationTests.java 232B
main
resources
mapper
GoodsMapper.xml 3KB
rebel.xml 666B
application.yml 655B
META-INF
additional-spring-configuration-metadata.json 206B
java
com
example
springboot_es
ServletInitializer.java 436B
mapper
GoodsMapper.java 439B
SpringBootEsApplication.java 723B
controller
GoodsController.java 2KB
service
GoodsService.java 908B
impl
GoodsServiceImpl.java 7KB
entity
Goods.java 706B
noSql
repository
GoodsRepository.java 443B
document
GoodsAttributeValue.java 654B
.mvn
wrapper
maven-wrapper.properties 218B
maven-wrapper.jar 50KB
MavenWrapperDownloader.java 5KB
mvnw 10KB
rebel-remote.xml 161B
共 21 条
- 1
资源评论
博士僧小星
- 粉丝: 1907
- 资源: 5877
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功