阅读文本大概需要3分钟。
Elasticsearch官方为Java提供了三种客户端API:
另外Spring框架也提供了spring-data-elasticsearch对Elasticsearch进行CURD操作,但是最底层也是基于Elasticsearch官方提供的API。
对于TransportClientAPI官方有如下一段话
https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.0/java-api.html
The TransportClient
is deprecated infavour of the Java High Level RESTClient and will be removed in Elasticsearch 8.0. The migration guide describes all thesteps needed to migrate.
TransportClient API已经被标识为过期,并在Elasticsearch 8.0.版本后被移除。但是TransportClient API在一些陈年老项目还是使用到的,今天就从TransportClient API开始介绍一下Elasticsearch的几个Java客户端API。
1、在pom.xml文件中引入
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.0.0</version>
</dependency>
2、构建TransportClient对象
public static TransportClient getTransportClient(){
TransportClient client = null;
try {
client = new PreBuiltTransportClient(Settings.EMPTY);
//根据主机名获取InetAddress对象
// InetAddress remAdd=InetAddress.getByName("es.server.com");
//获取本地InetAddress对象
// InetAddress locAdd=InetAddress.getLocalHost();
//创建客户端clients
InetAddress locAdd = InetAddress.getByName("127.0.0.1"); // ip
client.addTransportAddress(new TransportAddress(locAdd, 9300));
System.out.println(client);
//client.close();
}catch (Exception e){
e.printStackTrace();
}
return client;
}
3、document操作,document分为单文档和多文档操作
https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.0/java-docs.html
4、创建Document
Index API 允许我们添加某种类型的JSON文档到特定的index ,并使之可搜索。
使用json字符串创建Index
public static IndexResponse getIndexResponseWithString(TransportClient client){
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
IndexResponse response = client.prepareIndex
("twitter", "tweet", "1")
.setSource(json, XContentType.JSON)
.get();
return response;
}
使用map创建Index
public static IndexResponse getIndexResponseWithMap(TransportClient client) {
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
IndexResponse response = client.prepareIndex
("twitter2", "tweet2", "2")
.setSource(json, XContentType.JSON)
.get();
return response;
}
生成JSON文档的方式如下:
备注:
1、pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>es.transport</groupId>
<artifactId>es-transport</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.0.0</version>
</dependency>-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
</project>
2、使用elasticsearch-5.6.13版本elasticsearch