1.クライアントのクライアントのビルド
パッケージcom.pz998.app.service.utils。
静的org.elasticsearch.common.settings.Settings.settingsBuilderをインポートします。
輸入java.net.InetSocketAddress;
輸入org.elasticsearch.client.Client;
輸入org.elasticsearch.client.transport.TransportClient;
輸入org.elasticsearch.common.settings.Settings。
輸入org.elasticsearch.common.transport.InetSocketTransportAddress;
パブリッククラス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( "都市"、searchParam.getCity()))
)
.addHighlightedField( "名前")
.addHighlightedField( "disease_tag")
.setHighlighterPreTags(FIELD_HIGHLIGHT_PRE_TAG)
.setHighlighterPostTags(FIELD_HIGHLIGHT_POST_TAG)
.setFrom(から) .setSize(サイズ).setExplain(真))
.execute()actionGet(); //执行
SearchResultVo searchResultVo = new SearchResultVo();
List<BdDiseaseRpc> diseaseList = new ArrayList<BdDiseaseRpc>();
List<BdDoctorRpc> doctorList = new ArrayList<BdDoctorRpc>();
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<String, Object> sourceAsMap = hits.sourceAsMap();
//获取对应的高亮域
Map<String, HighlightField> 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<String, Object> sourceAsMap = hits.sourceAsMap();
//获取对应的高亮域
Map<String, HighlightField> 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;
}
病気や医師2のインデックスを検索しながら、上記のコードでは、プロファイルの名前の疾患情報を検索します
セットページングクエリ
.setFrom).setSize(サイズ).setExplain(真)(から)
3ハイライト情報
情報包括的パッケージのラベルを強調するために
public static final String FIELD_HIGHLIGHT_PRE_TAG = "<span style=\"color:red\">";
public static final String FIELD_HIGHLIGHT_POST_TAG = "</span>";
.setHighlighterPreTags(FIELD_HIGHLIGHT_PRE_TAG)
.setHighlighterPostTags(FIELD_HIGHLIGHT_POST_TAG)
〜
結果は、情報を得る強調表示しました
//指定されたドメインから採取されたハイライトフィールド設定
; HighlightField highlightFieldText = result.get( "名前")
文字コード=(文字列)sourceAsMap.get( "コード");
HighlightField introField = result.get( "イントロ"を);
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;
}
学習ビデオ
ブラウザで開くようにリンクをコピーし
、Tomcatのソースコード解析
https://study.163.com/course/introduction/1209535854.htm
Springmvcソース解析
https://study.163.com/course/introduction/1209536851.htm
ダボソース解析
https://study.163.com/course/introduction/1209648816.htm