大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1.客户端client构建
坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都VR全景小微创业公司专业提供成都定制网页设计营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。
package com.pz998.app.service.utils;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
import java.net.InetSocketAddress;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;public class SearchHelp {
private static TransportClient client = null;
public static Client getSearchClient() { if(client!=null){ return client; } Settings setting = settingsBuilder() //集群名称 .put("cluster.name", "es-cluster") .put("client.transport.sniff", true) .put("client.transport.ignore_cluster_name", false) .put("client.transport.ping_timeout", "5s") .put("client.transport.nodes_sampler_interval", "5s") .build(); client = TransportClient.builder().settings(setting).build(); ((TransportClient) client).addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("10.3.32.83", 9300))); return client; }
}
2.多字段检索
public SearchResultVo queryDiseaseOrDoctor(SearchParam searchParam,Client client){
Integer from = searchParam.getFrom()==null?0:searchParam.getFrom();
Integer size = searchParam.getPageSize()==null?0:searchParam.getPageSize();
MultiSearchResponse response = client.prepareMultiSearch()
//搜索疾病索引
.add(client.prepareSearch(DISEASE_INDEX)// 检索的目录
.setSearchType(SearchType.DEFAULT)// Query type
.setQuery(QueryBuilders.disMaxQuery()
.add(QueryBuilders.matchQuery("name", searchParam.getKeyword()))
.add(QueryBuilders.matchQuery("intro", searchParam.getKeyword())
))
.addHighlightedField("name")
.addHighlightedField("intro")
.setHighlighterPreTags(FIELD_HIGHLIGHT_PRE_TAG)
.setHighlighterPostTags(FIELD_HIGHLIGHT_POST_TAG)
.setFrom(from).setSize(size).setExplain(true))
//搜索医生索引
.add(client.prepareSearch(DOCTOR_INDEX)// 检索的目录
.setSearchType(SearchType.DEFAULT)// Query type
.setQuery(QueryBuilders.disMaxQuery()
.add(QueryBuilders.matchQuery("name", searchParam.getKeyword()))
.add(QueryBuilders.matchQuery("disease_tag", searchParam.getKeyword()))
//城市
// .add(QueryBuilders.matchQuery("city", searchParam.getCity()))
)
.addHighlightedField("name")
.addHighlightedField("disease_tag")
.setHighlighterPreTags(FIELD_HIGHLIGHT_PRE_TAG)
.setHighlighterPostTags(FIELD_HIGHLIGHT_POST_TAG)
.setFrom(from).setSize(size).setExplain(true))
.execute().actionGet();// 执行
SearchResultVo searchResultVo = new SearchResultVo();
List diseaseList = new ArrayList();
List doctorList = new ArrayList();
if(response.getResponses() != null) {
MultiSearchResponse.Item diseaseItem = response.getResponses().length>0?(response.getResponses())[0]:null;
MultiSearchResponse.Item doctorItem = response.getResponses().length>1?(response.getResponses())[1]:null;
if(diseaseItem!=null){
SearchResponse diseasResp = diseaseItem.getResponse();
System.out.println("命中疾病条数: " + diseasResp.getHits().getTotalHits());
searchResultVo.setDiseaseTotal(diseasResp.getHits().getTotalHits());
for (SearchHit hits : diseasResp.getHits().getHits()) {
Map sourceAsMap = hits.sourceAsMap();
//获取对应的高亮域
Map result = hits.highlightFields();
//从设定的高亮域中取得指定域
HighlightField highlightFieldText = result.get("name");
String code = (String)sourceAsMap.get("code");
HighlightField introField = result.get("intro");
String name = getHighlightFieldText(highlightFieldText);
name = StringUtils.isEmpty(name)?(String)sourceAsMap.get("name"):name;
String intro = getHighlightFieldText(introField);
intro = StringUtils.isEmpty(intro)?(String)sourceAsMap.get("intro"):intro;
BdDiseaseRpc bdDiseaseRpc = new BdDiseaseRpc();
bdDiseaseRpc.setName(name);
bdDiseaseRpc.setCode(code);
bdDiseaseRpc.setIntro(intro);
diseaseList.add(bdDiseaseRpc);
}
searchResultVo.setDiseaseList(diseaseList);
}
if(doctorItem!=null){
SearchResponse doctorResp = doctorItem.getResponse();
System.out.println("命中医生条数: " + doctorResp.getHits().getTotalHits());
searchResultVo.setDoctorTotal(doctorResp.getHits().getTotalHits());
for (SearchHit hits : doctorResp.getHits().getHits()) {
Map sourceAsMap = hits.sourceAsMap();
//获取对应的高亮域
Map result = hits.highlightFields();
//从设定的高亮域中取得指定域
HighlightField highlightFieldText = result.get("name");
String code = (String)sourceAsMap.get("code");
HighlightField diseaseTagField = result.get("disease_tag");
String name = getHighlightFieldText(highlightFieldText);
name = StringUtils.isEmpty(name)?(String)sourceAsMap.get("name"):name;
String diseaseTag = getHighlightFieldText(diseaseTagField);
diseaseTag = StringUtils.isEmpty(diseaseTag)?(String)sourceAsMap.get("disease_tag"):diseaseTag;
BdDoctorRpc bdDoctorRpc = new BdDoctorRpc();
bdDoctorRpc.setName(name);
bdDoctorRpc.setCode(code);
bdDoctorRpc.setDiseaseTag(diseaseTag);
doctorList.add(bdDoctorRpc);
}
searchResultVo.setDoctorList(doctorList);
}
}
return searchResultVo;
}
以上代码搜索疾病信息中的名字与简介,同时搜索了疾病和医生两个索引
设置分页查询
.setFrom(from).setSize(size).setExplain(true))
3高亮信息
设置高亮显示信息首尾包裹的标签
public static final String FIELD_HIGHLIGHT_PRE_TAG = "";
public static final String FIELD_HIGHLIGHT_POST_TAG = "";
.setHighlighterPreTags(FIELD_HIGHLIGHT_PRE_TAG)
.setHighlighterPostTags(FIELD_HIGHLIGHT_POST_TAG)
获取高亮显示的结果信息
//从设定的高亮域中取得指定域
HighlightField highlightFieldText = result.get("name");
String code = (String)sourceAsMap.get("code");
HighlightField introField = result.get("intro");
String highlight = getHighlightFieldText(highlightFieldText);
String intro = getHighlightFieldText(introField);
public String getHighlightFieldText(HighlightField highlightFieldText){
if(highlightFieldText==null){
return "";
}
//取得定义的高亮标签
Text[] higlightTexts = highlightFieldText.fragments();
//为title串值增加自定义的高亮标签
String higlightText = "";
for(Text text : higlightTexts){
higlightText += text;
}
return higlightText;
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。