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(); } }