ElasticSearch的java客户端demo
程序员文章站
2022-07-01 15:29:28
...
相关依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
代码
package cn.itcast.es;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class TestESREST {
private RestClient restClient;
private static final ObjectMapper MAPPER = new ObjectMapper();
@Before
public void init(){
RestClientBuilder restClientBuilder = RestClient.builder(
new HttpHost("192.168.142.128", 9200),
new HttpHost("192.168.142.128", 9201),
new HttpHost("192.168.142.128", 9202)
);
restClientBuilder.setFailureListener(new RestClient.FailureListener(){
@Override
public void onFailure(Node node) {
System.out.println("出错-->" + node);
}
});
this.restClient = restClientBuilder.build();
}
@After
public void close() throws IOException {
// 跟随应用的关闭而关闭
this.restClient.close();;
}
@Test
public void testInfo() throws IOException {
Request request = new Request("GET", "/_cluster/health");
request.addParameter("pretty","true");
Response response = this.restClient.performRequest(request);
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
}
@Test
public void testSave() throws Exception {
Request request = new Request("POST", "/haoke/house");
request.addParameter("pretty","true");
// 构造 数据
Map<String, Object> data = new HashMap<>();
data.put("id", 2001);
data.put("title", "深圳洪浪北新安三路 一室一厅");
data.put("price", 3500);
String json = MAPPER.writeValueAsString(data);
request.setJsonEntity(json);
Response response = this.restClient.performRequest(request);
System.out.println(request);
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
}
// 根据id查询数据
@Test
public void testQueryData() throws IOException {
Request request = new Request("GET", "/haoke/house/ZvEKR3ABKJ9KQna8XS08");
Response response = this.restClient.performRequest(request);
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
}
// 搜索数据
@Test
public void testSearchData() throws IOException {
Request request = new Request("POST", "/haoke/house/_search");
String searchJson = "{\"query\": {\"match\": {\"title\": \"拎包入住\"}}}";
request.setJsonEntity(searchJson);
request.addParameter("pretty","true");
Response response = this.restClient.performRequest(request);
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
上一篇: 全文检索使用步骤
下一篇: 以太坊 p2p Server 原理及实现
推荐阅读
-
java通过ip获取客户端Mac地址的小例子
-
ElasticSearch实战系列三: ElasticSearch的JAVA API使用教程
-
《ElasticSearch6.x实战教程》之简单搜索、Java客户端(上)
-
Java代码解决ElasticSearch的Result window is too large问题
-
Java开发之使用websocket实现web客户端与服务器之间的实时通讯
-
ElasticSearch配置需要权限认证的jest客户端
-
Java Socket编程系列(四)开发支持多客户端访问的Server
-
[转] Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
-
java之Scoket 客户端和服务器的发送与接收
-
java之Scoket 客户端和服务器的发送与接收