欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

elasticsearch中java客戶端api的使用方法-創(chuàng)新互聯(lián)

1.客戶端client構(gòu)建

堅(jiān)守“ 做人真誠(chéng) · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都VR全景小微創(chuàng)業(yè)公司專業(yè)提供成都定制網(wǎng)頁(yè)設(shè)計(jì)營(yíng)銷(xiāo)網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺(jué)設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁(yè)布局、功能開(kāi)發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。

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)) 
       //搜索醫(yī)生索引 
       .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();// 執(zhí)行

       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("命中疾病條數(shù): " + diseasResp.getHits().getTotalHits()); 

           searchResultVo.setDiseaseTotal(diseasResp.getHits().getTotalHits()); 
           for (SearchHit hits : diseasResp.getHits().getHits()) {       
             Map<String, Object> sourceAsMap = hits.sourceAsMap(); 
             //獲取對(duì)應(yīng)的高亮域 
             Map<String, HighlightField> result = hits.highlightFields();  

             //從設(shè)定的高亮域中取得指定域 
             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("命中醫(yī)生條數(shù): " + doctorResp.getHits().getTotalHits()); 
           searchResultVo.setDoctorTotal(doctorResp.getHits().getTotalHits()); 
           for (SearchHit hits : doctorResp.getHits().getHits()) {       
             Map<String, Object> sourceAsMap = hits.sourceAsMap(); 
             //獲取對(duì)應(yīng)的高亮域 
             Map<String, HighlightField> result = hits.highlightFields();  
             //從設(shè)定的高亮域中取得指定域 
             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; 
}

以上代碼搜索疾病信息中的名字與簡(jiǎn)介,同時(shí)搜索了疾病和醫(yī)生兩個(gè)索引

設(shè)置分頁(yè)查詢

.setFrom(from).setSize(size).setExplain(true))


3高亮信息

設(shè)置高亮顯示信息首尾包裹的標(biāo)簽

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)

獲取高亮顯示的結(jié)果信息

//從設(shè)定的高亮域中取得指定域
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 ""; 
   } 
   //取得定義的高亮標(biāo)簽 
   Text[] higlightTexts =  highlightFieldText.fragments(); 

   //為title串值增加自定義的高亮標(biāo)簽 
   String higlightText = "";  
   for(Text text : higlightTexts){   
     higlightText += text;  
   } 
   return higlightText; 
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

本文標(biāo)題:elasticsearch中java客戶端api的使用方法-創(chuàng)新互聯(lián)
文章分享:http://chinadenli.net/article44/dgjshe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、用戶體驗(yàn)、網(wǎng)站內(nèi)鏈、自適應(yīng)網(wǎng)站動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)