zoukankan      html  css  js  c++  java
  • elasticsearch 简单demo RestHighLevelClient LowLeveClient

    参考:

    https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-low.html

    www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-high-search.html

      1 
      3 import java.io.File;
      4 import java.io.FileReader;
      5 import java.io.IOException;
      6 import java.util.HashMap;
      7 import java.util.List;
      8 import java.util.Map;
      9 import java.util.Map.Entry;
     10 import java.util.Properties;
     11 
     12 import org.apache.http.HttpEntity;
     13 import org.apache.http.HttpHost;
     14 import org.apache.http.auth.AuthScope;
     15 import org.apache.http.auth.UsernamePasswordCredentials;
     16 import org.apache.http.client.CredentialsProvider;
     17 import org.apache.http.entity.ContentType;
     18 import org.apache.http.impl.client.BasicCredentialsProvider;
     19 import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
     20 import org.apache.http.nio.entity.NStringEntity;
     21 import org.apache.http.util.EntityUtils;
     22 import org.codehaus.jackson.JsonGenerationException;
     23 import org.codehaus.jackson.map.JsonMappingException;
     24 import org.codehaus.jackson.map.ObjectMapper;
     25 import org.elasticsearch.action.search.SearchRequest;
     26 import org.elasticsearch.action.search.SearchResponse;
     27 import org.elasticsearch.client.RequestOptions;
     28 import org.elasticsearch.client.Response;
     29 import org.elasticsearch.client.RestClient;
     30 import org.elasticsearch.client.RestClientBuilder;
     31 import org.elasticsearch.client.RestHighLevelClient;
     32 import org.elasticsearch.common.unit.TimeValue;
     33 import org.elasticsearch.index.query.QueryBuilders;
     34 import org.elasticsearch.rest.RestStatus;
     35 import org.elasticsearch.search.SearchHit;
     36 import org.elasticsearch.search.SearchHits;
     37 import org.elasticsearch.search.builder.SearchSourceBuilder;
     38 import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
     39 import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
     40 import org.junit.Test;
     41 
     42 import com.alibaba.fastjson.JSONArray;
     43 import com.alibaba.fastjson.JSONObject;
     44 
     45 public class ElasticUtil {
     46      static  ObjectMapper mapper = new ObjectMapper();
     47      static RestClient rc=null;
     48      
     49      public static RestClient getInstance() throws IOException{
     50          if(rc==null) {
     51              return getElasticClient();
     52          }else {
     53              return rc;
     54          }
     55      }
     56      
     57     /**
     58      * RestHighLevelClient使用详情请参考 
     59      * <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-high-search.html">官方文档</a><br>
     60      * 使用样例见{@link #restHighLevelClient_Demo}
     61      * @return
     62      * @throws IOException
     63      * @since jdk 1.8
     64      */
     65      public static RestHighLevelClient getRestHighLevelClient() throws IOException{
     66          Properties pro=new Properties();
     67          String filePath=System.getProperty("user.dir")+File.separator+"config/elasticsearch.properties";
     68          try {
     69              pro.load(new FileReader(filePath));
     70          } catch (Exception e) {
     71              e.printStackTrace();
     72          }
     73          String ip=pro.getProperty("ip");
     74          String port=pro.getProperty("port");
     75          RestHighLevelClient client = new RestHighLevelClient(
     76                     RestClient.builder(
     77                             new HttpHost(ip, Integer.valueOf(port), "http")));
     78          return client;
     79      }
     80 
     81      
     82     /**
     83      * LowLeveClient使用详情请参考 
     84      * <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-high-search.html">官方文档</a><br>
     85      * 使用样例见{@link #restLowLeveClient_Demo}
     86      * @return
     87      * @throws IOException
     88      * @since jdk 1.7
     89      */
     90      public static RestClient getElasticClient() throws IOException{
     91          Properties pro=new Properties();
     92         String filePath=System.getProperty("user.dir")+File.separator+"config/elasticsearch.properties";
     93          try {
     94             pro.load(new FileReader(filePath));
     95         } catch (Exception e) {
     96             e.printStackTrace();
     97         }
     98          String ip=pro.getProperty("ip");
     99          String port=pro.getProperty("port");
    100    102          return getElasticClient(ip,Integer.valueOf(port),username,passwd);
    103     }
    104      
    105     public static void main(String[] args) throws IOException {
    106         SearchResponse res=getELKSerach("index1","关键字",0,10,new String[] {"<em>","</em>"});
    107          SearchHits hits = res.getHits();
    108         for(SearchHit hit: hits){
    109 //            System.out.println();        //高亮部分
    110 //            System.out.println(hit.getScore());        //单个结果集评分
    111             Map<String, Object> map=hit.getSourceAsMap();
    112 //            System.out.println(map);  //json数据转为map<k,v>
    113             for(Entry<String, HighlightField>  ent:hit.getHighlightFields().entrySet()) {
    114                 String highl_key=ent.getValue().getName();
    115                 String highl_val=ent.getValue().getFragments()[0].toString();
    116                 map.put(highl_key, highl_val);
    117 //                System.out.println(ent.getKey()+"	"+ent.getValue());
    118 //                System.out.println(ent.getValue().getName()+"	"+ent.getValue().getFragments()[0]);
    119             }
    120             System.out.println(map); 
    121         }
    122     }
    123     142     
    143     /**
    144      * @param index   查询的index(类似mysql的datadb)
    145      * @param keyWord   查询的关键字
    146      * @param from    分页参数1
    147      * @param size          分页参数2
    148      * @return
    149      * @throws IOException
    150      */
    151     public static SearchResponse getELKSerach(String index,String keyWord,int from,int size) throws IOException {
    152          RestHighLevelClient client= getRestHighLevelClient();
    153          SearchRequest searchRequest = new SearchRequest(index); 
    154          SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
    155          searchSourceBuilder.query(QueryBuilders.queryStringQuery(keyWord)); 
    156          searchSourceBuilder.timeout(TimeValue.timeValueSeconds(5));
    157          searchSourceBuilder.from(from);
    158          searchSourceBuilder.size(size);
    159          searchRequest.source(searchSourceBuilder);
    160          SearchResponse searchResponse=client.search(searchRequest, RequestOptions.DEFAULT);
    161          client.close();
    162         return searchResponse;
    163     }
    164     /**
    165  * @param index   查询的index(类似mysql的datadb)
    166      * @param keyWord   查询的关键字
    167      * @param from    分页参数1
    168      * @param size          分页参数2
    169      * @param Highlight   自定义css
    170      * @return
    171      * @throws IOException
    172      */
    173     public static SearchResponse getELKSerach(String index,String keyWord,int from,int size,String[] Highlight) throws IOException {
    174          RestHighLevelClient client= getRestHighLevelClient();
    175          SearchRequest searchRequest = new SearchRequest(index); 
    176          SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
    177          //高亮
    178          if(Highlight.length==2) {
    179              HighlightBuilder highlightBuilder =new HighlightBuilder();
    180              highlightBuilder.field("*");//require_field_match
    181              highlightBuilder.requireFieldMatch(false);
    182              highlightBuilder.preTags(Highlight[0]);
    183              highlightBuilder.postTags(Highlight[1]);
    184              searchSourceBuilder.highlighter(highlightBuilder);
    185          }
    186          //查询
    187          searchSourceBuilder.query(QueryBuilders.queryStringQuery(keyWord)); 
    188          searchSourceBuilder.timeout(TimeValue.timeValueSeconds(5));
    189          searchSourceBuilder.from(from);
    190          searchSourceBuilder.size(size);
    191          searchRequest.source(searchSourceBuilder);
    192          SearchResponse searchResponse=client.search(searchRequest, RequestOptions.DEFAULT);
    193          client.close();
    194         return searchResponse;
    195     }
    196     
    197       void restLowLeveClient_Demo() throws IOException {
    198          RestClient rc =getInstance();
    199          HashMap<String,String> params=new HashMap<>();
    200          HttpEntity entity= getHttpEntityByKeyWord("关键字",0,10);
    201          Response response =rc.performRequest("GET", "/index1/_search",params,entity);
    202          System.out.println(response.getStatusLine().getStatusCode());
    203          
    204          String responseBody = EntityUtils.toString(response.getEntity());
    205          System.out.println(responseBody);
    206          JSONObject result = JSONObject.parseObject(responseBody); 
    207          JSONObject  hists=result.getJSONObject("hits");
    208          System.out.println(hists);
    209          JSONArray list=hists.getJSONArray("hits");
    210          System.out.println(list);
    211          int i= list.size();
    212          System.out.println("all:"+i);
    213          System.out.println(list.get(0));
    214          rc.close();
    215      }
    216     
    217      void restHighLevelClient_Demo() throws IOException {
    218          RestHighLevelClient client= getRestHighLevelClient();
    219          SearchRequest searchRequest = new SearchRequest("index1"); 
    220          SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
    221          searchSourceBuilder.query(QueryBuilders.queryStringQuery("关键字")); 
    222          searchSourceBuilder.timeout(TimeValue.timeValueSeconds(5));
    223          searchSourceBuilder.from(0);
    224          searchSourceBuilder.size(10);
    225          searchRequest.source(searchSourceBuilder);
    226          SearchResponse searchResponse=client.search(searchRequest, RequestOptions.DEFAULT);
    227          RestStatus status = searchResponse.status();
    228          System.out.println(status);
    229          SearchHits hits = searchResponse.getHits();
    230         for(SearchHit hit: hits){
    231             System.out.println(hit.getScore());
    232             System.out.println(hit.getSourceAsMap());
    233         }
    234          client.close();
    235      }
    236      
    237          /*
    238           * 有分页
    239           */
    240         static HttpEntity getHttpEntityByKeyWord(String  keyWord,int from,int size) throws IOException{
    241              String queryString=
    242                      "{
    " + 
    243                     "   "from" : "+from+", "size" : "+size+","+
    244                      "   "query": {
    " + 
    245                      "    "multi_match" : {
    " + 
    246                      "      "query": ""+keyWord+""
    " + 
    247                      "    }
    " + 
    248                      "  }
    " + 
    249                      "}";
    250             return new NStringEntity(queryString, ContentType.APPLICATION_JSON);
    251         }
    252         
    253         static HttpEntity getHttpEntityByKeyWord(String  keyWord) throws IOException{
    254              String queryString=
    255                      "{
    " + 
    256                      "   "query": {
    " + 
    257                      "    "multi_match" : {
    " + 
    258                      "      "query": ""+keyWord+""
    " + 
    259                      "    }
    " + 
    260                      "  }
    " + 
    261                      "}";
    262             return new NStringEntity(queryString, ContentType.APPLICATION_JSON);
    263         }
    264     /**
    265      * 获取elastic的client的信息
    266      * @param ip
    267      * @param port
    268      * @param usename
    269      * @param passwd
    270      * @return
    271      */
    272     public static RestClient getElasticClient(String ip, Integer port, String usename, String passwd) {
    273         final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    274         credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(usename, passwd));
    275         RestClient restClient = RestClient.builder(new HttpHost(ip, port, "http"))
    276                 .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
    277                     public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
    278                         httpClientBuilder.disableAuthCaching();
    279                         return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    280                     }
    281                 }).build();
    282         return restClient;
    283     }
    284     
    285     public static HttpEntity entiy2HttpEntity(Object ent) throws JsonGenerationException, JsonMappingException, IOException{
    286          String jsonString = mapper.writeValueAsString(ent);
    287          HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
    288          return entity;
    289     }
    290     
    291     /**
    292      * 批处理用
    293      * @param list
    294      * @return
    295      * @throws JsonGenerationException
    296      * @throws JsonMappingException
    297      * @throws IOException
    298      */
    299     public static HttpEntity entiyList2HttpEntity(List<Object> list,String index,String type) throws JsonGenerationException, JsonMappingException, IOException{
    300         String tmp="{ "index":  { "_index": ""+index+"", "_type": ""+type+"" }}";
    301         StringBuilder sb=new StringBuilder();
    302         for (Object ent : list) {
    303             String jsonString = mapper.writeValueAsString(ent);
    304             sb.append(tmp).append("
    ").append(jsonString).append("
    ");
    305         }
    306         
    307         HttpEntity entity = new NStringEntity(sb.toString(), ContentType.APPLICATION_JSON);
    308         return entity;
    309     }
    310     
    311     public static String Response2String( Response response) throws IOException{
    312             return EntityUtils.toString(response.getEntity());
    313     }
    314     
    315 }
  • 相关阅读:
    搭建自己的 github.io 博客
    转:华为副总裁一封信:透露年薪千万的工作感悟,太震撼了!
    SNF开发平台WinForm之五-高级查询使用说明-SNF快速开发平台3.3-Spring.Net.Framework
    SNF开发平台WinForm之四-开发-主细表管理页面-SNF快速开发平台3.3-Spring.Net.Framework
    SNF开发平台WinForm之三-开发-单表选择控件创建-SNF快速开发平台3.3-Spring.Net.Framework
    SNF开发平台WinForm之二-开发-单表表单管理页面-SNF快速开发平台3.3-Spring.Net.Framework
    SNF开发平台WinForm之一-开发-单表表格编辑管理页面-SNF快速开发平台3.3-Spring.Net.Framework
    C#基础总结之八面向对象知识点总结-继承与多态-接口
    C#基础总结之七面向对象知识点总结1
    C#基础总结之六 DataTable (临时表/数据源) 和Datatable 名片练习
  • 原文地址:https://www.cnblogs.com/yanghaolie/p/10880037.html
Copyright © 2011-2022 走看看