码迷,mamicode.com
首页 > 其他好文 > 详细

es索引的RestHighLevelClient实现

时间:2018-11-06 16:40:15      阅读:2364      评论:0      收藏:0      [点我收藏+]

标签:imp   request   cep   org   for   static   mil   stc   long   

java代码:

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class App3 {
    public static void main(String[] args) throws IOException {
         RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("localhost", 9200, "http")));
/*         Long start=System.currentTimeMillis();
         IndicesClient indicesClient=client.indices();
         DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("person");
         DeleteIndexResponse delRes=indicesClient.deleteIndex(deleteIndexRequest);
         if(delRes.isAcknowledged()) {
             System.out.println("delete success");
         }else{
             System.out.println("delete failed");
         }
         System.out.println("耗时:"+(System.currentTimeMillis()-start)+" 毫秒");*/
         SearchRequest req=new SearchRequest("person");
         SearchSourceBuilder builder=new SearchSourceBuilder();
         builder.size(10);
         req.source(builder);
         SearchResponse res=client.search(req);
         SearchHits hits=res.getHits();
         SearchHit[] hitArr=hits.getHits();
         for(SearchHit hit:hitArr) {
             System.out.println(hit.getSourceAsString());
         }
        /**** Done ****/
        System.out.println("Done");

    }
    
}

无用的import自动删除快捷键

导入需要的包快捷键 shift+ctrl+o,自动导入包,当然也会清除掉多余的包。

 

es索引的RestHighLevelClient实现

标签:imp   request   cep   org   for   static   mil   stc   long   

原文地址:https://www.cnblogs.com/davidwang456/p/9915791.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!