Elasticsearch 5.0下Java API使用指南
阿新 • • 發佈:2019-01-06
一.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.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</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
工程目錄以及執行結果的截圖如下:
五.參考資料
參考資料主要為官網文件: