<dependencies>
<!--ES核心API-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<!--json转换包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
添加
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
创建实体类(Article)
package dt2008.domain;
public class Article {
private Integer id;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
ElasticSearch:创建索引库
package dt2008.elasticsearch;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 演示ES创建索引库
*/
public class Demo1 {
@Test
public void createIndex() throws Exception {
//1. 创建Settings配置信息对象,用于配置ES的集群信息
//注意:默认的ES集群名称就是elasticsearch
Settings settings = Settings.builder()
.put("cluster.name","elasticsearch")
.build();
//2. 创建ES传输客户端对象(***),用于操作ES,例如:创建索引库,添加文档,修改文档....
TransportClient transportClient = new PreBuiltTransportClient(settings);
//2.1 添加连接ES的信息,例如主机地址,端口
transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
//3.使用传输客户端对象创建索引库
//prepareCreate: 创建索引库
//get(): 发出请求到ES去执行
transportClient.admin().indices().prepareCreate("blog2").get();
//4.释放资源
transportClient.close();
}
}
运行后
文章评论