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,自动导入包,当然也会清除掉多余的包。

 

posted on 2018-11-06 15:57  一天不进步,就是退步  阅读(707)  评论(0编辑  收藏  举报