ElasticSearch7.6版本api
完整业务代码
public JobResult search(String salary, String jobaddr, String keyword, Integer page) throws IOException, InvocationTargetException, IllegalAccessException {
//解析参数
// salary: *-*
// page: 1
// jobaddr:
// keyword: java
// jobarea:
String[] salaryMoney = salary.split("-");
int salaryMin=0;
int salaryMax=1;
if ("*".equals(salaryMoney[0])){
//如果最小值为*则等于0
}else {
salaryMin=Integer.parseInt(salaryMoney[0])*10000;
}
if ("*".equals(salaryMoney[1])){
//如果最大值为*则等于1千万
salaryMax=salaryMax*1000000;
}else {
salaryMax=Integer.parseInt(salaryMoney[1])*10000;
}
//判断工作地点是否为空
if (StringUtils.isBlank(jobaddr)){
//如果为空
jobaddr="*";
}
if (StringUtils.isBlank(keyword)){
//如果为空
keyword="*";
}
SearchRequest searchRequest=new SearchRequest("jobinfo");
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//精确查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(0);
boolQueryBuilder.must(rangeQuery("salary_min").gt(salaryMin));
boolQueryBuilder.must(rangeQuery("salary_max").lt(salaryMax));
boolQueryBuilder.must(wildcardQuery("job_name",keyword));
boolQueryBuilder.must(wildcardQuery("company_addr",jobaddr));
sourceBuilder.query(boolQueryBuilder).from(page-1).size(30);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
JobResult jobResult=new JobResult();
List<com.yzdx.pojo.JobInfoEs>jobInfoEsList=new ArrayList<com.yzdx.pojo.JobInfoEs>();
for (SearchHit documentFields : searchResponse.getHits().getHits()) {
com.yzdx.pojo.JobInfoEs jobInfoEs=new com.yzdx.pojo.JobInfoEs();
Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
BeanUtils.populate(jobInfoEs,sourceAsMap);
jobInfoEsList.add(jobInfoEs);
}
jobResult.setRows(jobInfoEsList);
long pageTotal = searchResponse.getHits().getTotalHits().value;//总页数
jobResult.setPageTotal(pageTotal);
return jobResult;
}
}
复杂查询集合
SearchRequest searchRequest=new SearchRequest("jobinfo");//创建查询请求
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();//创建查询对象
//精确查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(0);//创建查询条件集
//添加查询条件集
boolQueryBuilder.must(rangeQuery("salary_min").gt(salaryMin));//大于查询
boolQueryBuilder.must(rangeQuery("salary_max").lt(salaryMax));//小于查询
boolQueryBuilder.must(wildcardQuery("job_name",keyword));//带通配符查询
boolQueryBuilder.must(wildcardQuery("company_addr",jobaddr));//带通配符查询
sourceBuilder.query(boolQueryBuilder).from(page-1).size(30);//查询集合集并进行对查询的数据进行分页
searchRequest.source(sourceBuilder);//在search请求中添加查询条件集
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//执行请求
分析查询的数据
JobResult jobResult=new JobResult();
List<com.yzdx.pojo.JobInfoEs>jobInfoEsList=new ArrayList<com.yzdx.pojo.JobInfoEs>();//创建list集合
for (SearchHit documentFields : searchResponse.getHits().getHits()) {
com.yzdx.pojo.JobInfoEs jobInfoEs=new com.yzdx.pojo.JobInfoEs();
Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();//获取查询数据的map集合
BeanUtils.populate(jobInfoEs,sourceAsMap);//利用BeanUtils工具类将map集合转为JobInfoEs对象
jobInfoEsList.add(jobInfoEs);
}
jobResult.setRows(jobInfoEsList);
long pageTotal = searchResponse.getHits().getTotalHits().value;//总页数
jobResult.setPageTotal(pageTotal);
return jobResult;
随着ES的版本在升级,api也会跟着进行改变,在实现此业务是我也查询了不少资料,建议大家看
https://www.cnblogs.com/zyh-2017/p/12781440.html