這里描述操作elasticsearch采用TransportClient這種方式,官方明確表示在ES 7.0版本中將棄用TransportClient客戶端,且在8.0版本中完全移除它.
記錄一些常用的方法:
官方文檔:
TransportClient
@Testpublic void search() throws UnknownHostException
{Settings settings
= Settings
.builder().put("cluster.name","my-application").build();TransportClient transportClient
=new PreBuiltTransportClient(settings
);transportClient
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));GetResponse response
=transportClient
.prepareGet("index3","user3","10").get();System
.out
.println(response
.getSource());}@Testpublic void insert() throws Exception
{Settings settings
= Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));XContentBuilder contentBuilder
= XContentFactory
.jsonBuilder().startObject().field("catid","22").field("classify",54).field("author","ssve").field("id","1").field("title","菜鳥成長記").endObject();IndexResponse indexResponse
= client
.prepareIndex("index3", "user3", "10").setSource(contentBuilder
).get();System
.out
.println(indexResponse
.status());}@Testpublic void delete() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));DeleteResponse deleteResponse
= client
.prepareDelete("index3", "user3", "10").get();System
.out
.println(deleteResponse
.status());}@Testpublic void update() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));UpdateRequest request
=new UpdateRequest();XContentBuilder contentBuilder
=XContentFactory
.jsonBuilder().startObject().field("id","555").endObject();request
.index("index3").type("user3").id("10").doc(contentBuilder
);UpdateResponse updateResponse
= client
.update(request
).get();System
.out
.println(updateResponse
.status());}@Testpublic void upsert() throws IOException
, ExecutionException
, InterruptedException
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));XContentBuilder builder
=XContentFactory
.jsonBuilder().startObject().field("catid","22").field("classify",54).field("author","zhang").field("id","10").field("title","菜鳥成長記").endObject();UpdateRequest request
=new UpdateRequest();IndexRequest indexRequest
=new IndexRequest();indexRequest
.index("index3").type("user3").id("11").source(builder
);request
.index("index3").type("user3").id("11").doc(new XContentFactory().jsonBuilder().startObject().field("id","i love you").endObject()).upsert(indexRequest
);UpdateResponse updateResponse
= client
.update(request
).get();System
.out
.println(updateResponse
.status());}@Testpublic void multiGet() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));MultiGetRequest request
=new MultiGetRequest();request
.add("index3","user3","11");request
.add("index3","user3","10");request
.add("index3","user3","13");request
.add("index3","user3","14");MultiGetResponse multiGetItemResponses
= client
.multiGet(request
).get();for (MultiGetItemResponse response
:multiGetItemResponses
) {System
.out
.println(response
.getResponse().getSourceAsString());}}@Testpublic void bulk() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));BulkRequestBuilder bulkRequestBuilder
= client
.prepareBulk();IndexRequest request
=new IndexRequest();request
.index("index3").type("user3").id("13").source(XContentFactory
.jsonBuilder().startObject().field("catid","85").field("classify","85").field("author","宇宙無敵").field("id","漫威").field("title","漫威").endObject());IndexRequest request1
=new IndexRequest();request1
.index("index3").type("user3").id("14").source(XContentFactory
.jsonBuilder().startObject().field("catid","55").field("classify","85").field("author","宇宙無敵").field("id","漫威").field("title","漫威").endObject());bulkRequestBuilder
.add(request1
);bulkRequestBuilder
.add(request
);BulkResponse bulkItemResponses
= bulkRequestBuilder
.get();System
.out
.println(bulkItemResponses
.status());}@Testpublic void query() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));MatchAllQueryBuilder builder
= QueryBuilders
.matchAllQuery();SearchRequestBuilder index3
= client
.prepareSearch("index3").setQuery(builder
).setSize(5);SearchResponse searchResponse
= index3
.get();SearchHits hits
= searchResponse
.getHits();for (SearchHit hit
:hits
) {System
.out
.println(hit
.getSourceAsString());}}@Testpublic void matchQuery() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));MatchQueryBuilder matchQueryBuilder
= QueryBuilders
.matchQuery("title", "愛");SearchRequestBuilder index
= client
.prepareSearch("index3").setQuery(matchQueryBuilder
).setSize(10);SearchHits hits
= index
.get().getHits();for (SearchHit hit
:hits
) {System
.out
.println(hit
.getSourceAsString());}}@Testpublic void mutilMatchQuery() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));MultiMatchQueryBuilder multiMatchQueryBuilder
= QueryBuilders
.multiMatchQuery("周星馳影帝", "author","title");SearchRequestBuilder index
= client
.prepareSearch("index3").setQuery(multiMatchQueryBuilder
).setSize(10);SearchHits hits
= index
.get().getHits();for (SearchHit hit
:hits
) {System
.out
.println(hit
.getSourceAsString());}}@Testpublic void termsMatchQuery() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));TermsQueryBuilder termsQueryBuilder
= QueryBuilders
.termsQuery("title", "周星馳", "影帝");SearchRequestBuilder index
= client
.prepareSearch("index3").setQuery(termsQueryBuilder
).setSize(10);SearchHits hits
= index
.get().getHits();for (SearchHit hit
:hits
) {System
.out
.println(hit
.getSourceAsString());}}@Testpublic void query1() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));
QueryBuilder queryBuilder
= QueryBuilders
.idsQuery().addIds("1","2","4");SearchRequestBuilder index
= client
.prepareSearch("index3").setQuery(queryBuilder
).setSize(10);SearchHits hits
= index
.get().getHits();for (SearchHit hit
:hits
) {System
.out
.println(hit
.getSourceAsString());}}@Testpublic void aggregation() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));AggregationBuilder aggregationBuilder
= AggregationBuilders
.max("max").field("id");SearchResponse index3
= client
.prepareSearch("index3").addAggregation(aggregationBuilder
).get();Max max
= index3
.getAggregations().get("max");System
.out
.println(max
.getValue());}@Testpublic void queryString() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));QueryBuilder queryBuilder
=QueryBuilders
.queryStringQuery("+周星馳 -sss");SearchRequestBuilder index3
= client
.prepareSearch("index3").setQuery(queryBuilder
).setSize(10);SearchResponse searchResponse
= index3
.get();for (SearchHit hit
:searchResponse
.getHits()) {System
.out
.println(hit
.getSourceAsString());}}@Testpublic void boolQuery() throws Exception
{Settings settings
=Settings
.builder().put("cluster.name","my-application").build();TransportClient client
=new PreBuiltTransportClient(settings
);client
.addTransportAddress(new TransportAddress(InetAddress
.getByName("localhost"),9300));QueryBuilder queryBuilder
=QueryBuilders
.boolQuery().must(QueryBuilders
.matchQuery("author","周星馳")).mustNot(QueryBuilders
.matchQuery("title","梁朝偉")).should(QueryBuilders
.matchQuery("title","影帝")).filter(QueryBuilders
.rangeQuery("id").gte("1"));SearchRequestBuilder index3
= client
.prepareSearch("index3").setQuery(queryBuilder
).setSize(10);SearchResponse searchResponse
= index3
.get();for (SearchHit hit
:searchResponse
.getHits()) {System
.out
.println(hit
.getSourceAsString());}}
總結
以上是生活随笔為你收集整理的Elasticsearch(Transport Client)常用操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。