Elasticsearch结合Springboot框架实现增删改查以及高亮设置的部分代码
package com.blb.service.impl;
import ch.qos.logback.core.joran.conditional.ElseAction;
import com.blb.dao.GzDataRepository;
import com.blb.domain.GzData;
import com.blb.service.GzDataService;
import com.blb.utils.PageUtil;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Service
public class GzDataServiceImpl implements GzDataService {
@Autowired
private GzDataRepository gzDataRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
//1.排序 2.分页 3.高亮 4.查询条件 未做
//关键字应该是要匹配 公司名 经营的类型 地址 法人 服务的范围 手机号码
@Override
public List<GzData> queryByPage(String keyword) {
List<GzData> gzDataList=new ArrayList<>();
//构建查询的条件
BoolQueryBuilder queryBuilder=QueryBuilders.boolQuery();
queryBuilder.should(QueryBuilders.matchQuery("company",keyword))
.should(QueryBuilders.matchQuery("manager",keyword))
.should(QueryBuilders.matchQuery("address",keyword))
.should(QueryBuilders.matchQuery("type",keyword))
.should(QueryBuilders.matchQuery("service",keyword));
//logstash 可以将数据库的数据同步到ES中 集群 分片
PageRequest pageRequest = PageRequest.of(0, 10);
Iterable<GzData> iterable = gzDataRepository.search(queryBuilder,pageRequest);
Iterator<GzData> iterator = iterable.iterator();
while(iterator.hasNext())
{
gzDataList.add(iterator.next());
}
return gzDataList;
}
/**
* 如果要处理高亮的请求 就不能使用
* @param keyword
* @return
*/
@Override
public PageUtil queryByPageHigh(String keyword) {
//查询设置
BoolQueryBuilder queryBuilder=QueryBuilders.boolQuery();
queryBuilder.should(QueryBuilders.matchQuery("company",keyword))
.should(QueryBuilders.matchQuery("manager",keyword))
.should(QueryBuilders.matchQuery("address",keyword))
.should(QueryBuilders.matchQuery("type",keyword))
.should(QueryBuilders.matchQuery("service",keyword));
//高亮设置
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.preTags("<em>")
.postTags("</em>")
.field("company")
.field("manager")
.field("address")
.field("type")
.field("service");
//searchQueryBuilder 可以将多个条件组合在一起
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
//高亮显示
searchQueryBuilder.withHighlightBuilder(highlightBuilder);
//分页显示
searchQueryBuilder.withPageable( PageRequest.of(0, 10));
//查询条件
searchQueryBuilder.withQuery(queryBuilder);
//排序条件
//searchQueryBuilder.withSort()
//封装一些分页的信息
PageUtil pageUtil=new PageUtil();
AggregatedPage<GzData> pageinfo = elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), GzData.class, new SearchResultMapper() {
//将查询的结果 和我们高亮的样式要结合起来
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) {
//先来一个容器把数据装起来...
List<GzData> gzDataList = new ArrayList<>();
//我们所有查询的结果都放在这个searchResponse里面
//我们现在就是要把我们想要的内容从这个searchResponse里面获取到
SearchHits hits = searchResponse.getHits();
//如果getTotalHits是0 则表示查询不到数据
if (hits.getTotalHits() <= 0) {
return null;
} else {
//从里面获取到一条一条的数据了
for (SearchHit hit : hits) {
GzData gzData = new GzData();
//还要获取到某个字段的高亮特征 高亮的特征和当前的数据 做一个替换
HighlightField companyHighlight = hit.getHighlightFields().get("company");
if (companyHighlight != null) {
gzData.setCompany(companyHighlight.fragments()[0].toString());
} else {
String compnay = (String) hit.getSourceAsMap().get("company");
gzData.setCompany(compnay);
}
//还要获取到某个字段的高亮特征 高亮的特征和当前的数据 做一个替换
HighlightField addressHighlight = hit.getHighlightFields().get("address");
if (addressHighlight != null) {
gzData.setAddress(addressHighlight.fragments()[0].toString());
} else {
String address = (String) hit.getSourceAsMap().get("address");
gzData.setAddress(address);
}
//还要获取到某个字段的高亮特征 高亮的特征和当前的数据 做一个替换
HighlightField managerHighlight = hit.getHighlightFields().get("manager");
if (managerHighlight != null) {
gzData.setManager(managerHighlight.fragments()[0].toString());
} else {
String manager = (String) hit.getSourceAsMap().get("manager");
gzData.setManager(manager);
}
String type = (String) hit.getSourceAsMap().get("type");
gzData.setType(type);
String service = (String) hit.getSourceAsMap().get("service");
gzData.setService(service);
gzDataList.add(gzData);
}
}
return new AggregatedPageImpl(gzDataList);
}
@Override
public <T> T mapSearchHit(SearchHit searchHit, Class<T> aClass) {
return null;
}
});
pageUtil.setData(pageinfo.toList());
return pageUtil;
}
@Override
public GzData queryById(Long id) {
return gzDataRepository.findById(id).get();
}
}