es索引的RestHighLevelClient实现
Posted davidwang456
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es索引的RestHighLevelClient实现相关的知识,希望对你有一定的参考价值。
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实现的主要内容,如果未能解决你的问题,请参考以下文章
中间件:ES组件RestHighLevelClient用法详解