1. client building
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() //Cluster name .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. Multi-Field Retrieval
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() //Search Disease Index .add(client.prepareSearch(DISEASE_INDEX)// Retrieved Directory .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)) //Search Doctor Index .add(client.prepareSearch(DOCTOR_INDEX)// Retrieved Directory .setSearchType(SearchType.DEFAULT)// Query type .setQuery(QueryBuilders.disMaxQuery() .add(QueryBuilders.matchQuery("name", searchParam.getKeyword())) .add(QueryBuilders.matchQuery("disease_tag", searchParam.getKeyword())) //City
// .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();//execute
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("Number of hit diseases: " + diseasResp.getHits().getTotalHits()); searchResultVo.setDiseaseTotal(diseasResp.getHits().getTotalHits()); for (SearchHit hits : diseasResp.getHits().getHits()) { Map<String, Object> sourceAsMap = hits.sourceAsMap(); //Get the corresponding highlight field Map<String, HighlightField> result = hits.highlightFields(); //Gets the specified field from the set highlight field 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("Number of hits to a doctor: " + doctorResp.getHits().getTotalHits()); searchResultVo.setDoctorTotal(doctorResp.getHits().getTotalHits()); for (SearchHit hits : doctorResp.getHits().getHits()) { Map<String, Object> sourceAsMap = hits.sourceAsMap(); //Get the corresponding highlight field Map<String, HighlightField> result = hits.highlightFields(); //Gets the specified field from the set highlight field 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; }
The above code searches for names and descriptions in disease information, and searches both disease and physician indexes
Set Paging Query
.setFrom(from).setSize(size).setExplain(true))
3 Highlight Information
Set a label to highlight the package at the beginning and end of the message
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)
~
Get highlighted result information
//Get the specified field from the set highlight field
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 ""; } //Get Defined Highlight Label Text[] higlightTexts = highlightFieldText.fragments(); //Add custom highlight tags for title string values String higlightText = ""; for(Text text : higlightTexts){ higlightText += text; } return higlightText; }
Learning Video
Copy link, open in browser
tomcat source parsing
https://study.163.com/course/introduction/1209535854.htm
Springmvc Source Parsing
https://study.163.com/course/introduction/1209536851.htm
dubbo Source Parsing
https://study.163.com/course/introduction/1209648816.htm