TransportClient对ES简单增删改查
Posted Firm陈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TransportClient对ES简单增删改查相关的知识,希望对你有一定的参考价值。
Elasticsearch 7.0中开始弃用TransportClient,并在8.0中完全删除它,为此这里我们只简单介绍一下TransportClient 中增删改查的API。首先看一下TransportClient整体的API:
- 整个客户端API提供两个最底层的方法,execute,其关键特征如下: ActionFuture
execute(Action<Request, Response, RequestBuilder> action, Request request); - 返回ActionFuture,根据名称即可知道,该方法是典型的异步调用,Future模式。 void
execute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener listener);无返回值,但需要传入ActionListener listener,同样根据名称即可知道,该参数的作用是事件监听器(回调方法),也就是收到服务端响应后,调用回调函数,进行结果处理。注意:ElasticSearchClient API 其本质是使用异步请求模式。 - prepare 开头的方法,例如IndexRequestBuilder prepareIndex()这类API的设计是使用Build模式,先通过build构建请求参数,最终会通过调用get()方法完成接口调用。
1、建立ES集群连接
// 建立ES集群连接
private String IP;
private int PORT;
TransportClient client ;
@Before
public void init()
this.IP = "192.168.5.166";
this.PORT = 9300;
try
Settings settings = Settings.builder().put("cluster.name", "my-application").put("client.transport.sniff", true).build();
client = new PreBuiltTransportClient(settings)
.addTransportAddresses(new TransportAddress(InetAddress.getByName(IP), PORT));
catch (UnknownHostException e)
e.printStackTrace();
2、查询:
prepareGet:根据ID精确查询节点数据
@Test
public void test2()
GetRequestBuilder requestBuilder = client.prepareGet("index3", "user1", "1");
GetResponse response = requestBuilder.execute().actionGet();
Map<String, Object> source = response.getSource();
System.out.println(source.keySet().toString());
prepareMultiGet:同时查询多条数据
@Test
public void test3()
MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
.add("index3", "user1", "1")
.add("index3", "user1", "hOuIw28B2HN-5BvRXzVY")
.get();
MultiGetItemResponse[] responses = multiGetItemResponses.getResponses();
for(MultiGetItemResponse response : responses)
GetResponse getResponse = response.getResponse();
System.out.println(getResponse.getSource().toString());
prepareSearch:根据字段查询
@Test
public void test4()
// 这只查询的字段和字段值,这里会进行分词查询,相当于match
QueryBuilder builder = QueryBuilders.matchQuery("interest","boxing computer");
SearchResponse searchResponse = client.prepareSearch("index3")
.setTypes("user1")
.setSearchType(QUERY_THEN_FETCH)
.setQuery(builder)
.setSize(10)
.get();
SearchHit[] searchHits = searchResponse.getHits().getHits();
for(SearchHit searchHit : searchHits)
System.out.println(searchHit.getSourceAsString());
3、修改
update:
@Test
public void test5() throws Exception
UpdateRequest updateRequest = new UpdateRequest();
UpdateRequest doc = updateRequest
.index("index3")
.type("user1")
.id("1")
.doc(
XContentFactory.jsonBuilder().startObject()
.field("name","rabbit.sun")
.field("interest","game steak")
.endObject()
);
UpdateResponse response = client.update(doc).get();
System.out.println(response.status().toString());
4、新增
@Test
public void test6() throws Exception
XContentBuilder doc = XContentFactory.jsonBuilder().startObject()
.field("name", "rabbit.sun")
.field("birthday", "1998-4-6")
.field("address", "HeNan Province")
.field("company", "YunZhiHui")
.field("height", 160)
.endObject();
IndexResponse response = client.prepareIndex("index3", "user1","2").setSource(doc).get();
System.out.println(response.status());
5、删除
prepareDelete:根据ID删除
@Test
public void test7()
DeleteResponse response = client.prepareDelete("index3", "user1", "hOuIw28B2HN-5BvRXzVY").get();
System.out.println(response.status());
DeleteByQueryAction:根据字段删除
@Test
public void test8()
BulkByScrollResponse response = DeleteByQueryAction.INSTANCE
.newRequestBuilder(client)
.filter(QueryBuilders.matchQuery("name","leo.wang"))
.source("index3") //index
.get();
System.out.println(response.getDeleted());
以上是关于TransportClient对ES简单增删改查的主要内容,如果未能解决你的问题,请参考以下文章
ubuntu安装es(elesticSearch),es结构介绍,IK分词器的简单使用,es入门增删改查api,结果集高亮