elasticsearch 자바 클 라 이언 트 api 사용 (1)

10748 단어
1. 클 라 이언 트 클 라 이언 트 구축
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; } 


이상 코드 는 질병 정보 중의 이름과 소 개 를 검색 합 니 다.
disease 와 doctor 두 개의 색인 을 동시에 검색 하 였 습 니 다.
페이지 조회 설정
.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);

좋은 웹페이지 즐겨찾기