分类:
版权声明:本文为博主原创文章,转载请留言.
Elasticsearch 5.0下Java API使用指南
一.2.X到5.X
Elasticsearch 2.x使用Java api把elasticsearch安装包下的lib文件夹下的jar文件全部加入到工程类路径即可,换到5.x就不适用了.创建Clien的代码:
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
只使用安装包下的jar文件会出现PreBuiltTransportClient cannot be resolved to a type的错误,原因是缺少jar包.下面记录一下如何在5.X中使用Java api
二.创建maven工程
2.1Eclipse中新建maven工程
打开eclipse,file->other->maven project:
创建group id(相当于工程名)和artifact id(相当于包名):
2.2在pom.xml中添加以下依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
添加依赖后maven会自动导包,如下图所示:
至此所有的jar包都导入完成了
三.配置log4j2
在src/main/resources文件夹下新建文件log4j2.properties,加入以下log4
j2的配置:
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
rootLogger.level = info
rootLogger.appenderRef.console.ref = console
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
四.创建Client并搜索数据
首先启动elasticsearch,我这里使用的是5.1.1,创建一个新的索引:
curl -XPUT "http://localhost:9200/blog"
- 1
- 1
添加一条文档
curl -XPUT "http://localhost:9200/blog/article/1" -d '{ "title":"test", "content":"test content" }'
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
在 src/main/java/目录下新建TestEsClient.java,内容如下:
import java.net.InetAddress;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class TestEsClient {
public static void main(String[] args) {
try {
//设置集群名称
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
//创建client
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
//搜索数据
GetResponse response = client.prepareGet("blog", "article", "1").execute().actionGet();
//输出结果
System.out.println(response.getSourceAsString());
//关闭client
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
运行结果:
no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
{ "title":"test","content":"test content"}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
工程目录以及运行结果的截图如下:
五.参考资料
参考资料主要为官网文档: