依赖
<!-- es -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
配置 必加
//es:
// hostname: 124.70.138.156
// port: 9200
// scheme: http
@Configuration
@ConfigurationProperties(prefix = "es")
@Data
public class InitElasticsearchRestHighLevelClient {
/**
* es 主机地址
*/
private String hostname;
/**
* 端口
*/
private int port;
/**
* 请求协议
*/
private String scheme;
/**
* 初始化 RestHighLevelClient 用来操作es
*
* @return
*/
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(RestClient.builder(new HttpHost(hostname, port, scheme)));
}
}
异常
配置类没有加es配置信息导致@Configuration无法识别到es配置
查询思维导图
初始化同步数据
@Log4j2
@Component
public class EsSync implements ApplicationRunner {
@Resource
private OmsRemote omsRemote;
@Autowired
private OmsEsController omsEsController;
@Override
public void run(ApplicationArguments args) throws Exception{
List<OmsRecordRes> data = omsRemote.findRelationAll().getData();
data.forEach(System.out::println);
omsEsController.batchInsert(data);
log.info("數據同步成功,內容為:{}",data);
}
}
注解
@Autowired
private RestHighLevelClient restHighLevelClient;
查询 + 高亮
@Override
public Result<PageResult<WashOrderDTO>> queryWashOrder(WashOrderReq req) {
long total = 0;
List<WashOrderDTO> list = new ArrayList<>();
try {
//创建请求索引
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
//创建构造器
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//创建条件拼接
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//判断请求条件
//查询订单号 精确查询
if (StringUtils.isNotBlank(req.getWashId())){
boolQueryBuilder.must(QueryBuilders.termQuery("washId",req.getWashId()));
}
//查询用户名 模糊查询
if (StringUtils.isNotBlank(req.getUserName())){
boolQueryBuilder.must(QueryBuilders.matchQuery("userName",req.getUserName()));
}
//根据总价排序
//条件存放构造器
searchSourceBuilder.query(boolQueryBuilder);
//分页
searchSourceBuilder.from((req.getPageNum()-1)*req.getPageSize());
searchSourceBuilder.size(req.getPageSize());
//排序
searchSourceBuilder.sort("washPrice", SortOrder.DESC);
//高亮
searchSourceBuilder.highlighter(new HighlightBuilder().field("userName").preTags("<span style=\"color=red\">").postTags("</span>"));
searchRequest.source(searchSourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
//总条数
total = hits.getTotalHits().value;
for (SearchHit hit : hits.getHits()) {
String sourceAsString = hit.getSourceAsString();
WashOrderDTO washOrderDTO = JSONObject.parseObject(sourceAsString, WashOrderDTO.class);
washOrderDTO.setWashId(Integer.valueOf(hit.getId()));
//获取高亮集合
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if (null!=highlightFields){
HighlightField userName = highlightFields.get("userName");
if (userName!=null){
String str = "";
for (Text fragment : userName.getFragments()) {
str += fragment;
}
//将字段替换成高亮
washOrderDTO.setUserName(str);
}
}
//将存储的结果存入list集合
list.add(washOrderDTO);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return PageResult.toResult(total,list);
}
标签:userName,高亮,searchSourceBuilder,req,private,查询,es,new,ES
From: https://blog.csdn.net/yxhsk8/article/details/142357042