官網(wǎng)API地址
https://www.elastic.co/guide/en/elasticsearch/client/index.html
具體學(xué)習(xí)描述
- 所有的elasticsearch操作都是通過 Client對象進行的。
- 操作會存儲在client中 然后以批處理的方式執(zhí)行
-所有相關(guān)API 都基于java API
maven依賴
- 需要加入Log4j 2 dependencies
<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>
- 在classpath路徑下添加log4j 2 的配置文件,例如在src/main/resources project 目錄下的 log4j2.properties:
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
rootLogger.level = info
rootLogger.appenderRef.console.ref = console
- 使用SLF4J 需加入以下如下 :
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
我們可以用很多有關(guān)SLF4J的實現(xiàn)詳細見這個頁面,作為例子 ,此處使用slf4j-simple 作為展示
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
處理jar包依賴沖突
通過第三方依賴如Guava and Joda 來處理jar包沖突 ;例如 如果es(以后凡是elasticsearch簡稱es) 使用的是 Joda 2.8 而你的java代碼使用的是 Joda 2.1
那么你又兩種方式可以解決這個問題
方式一
更新你的代碼版本
方式二
重新加載沖突依賴,使其與你的項目或者es及es client其相關(guān)插所需版本一致
詳細操作方法參見這里
打包成含有依賴的jar包
如果你想講你的應(yīng)用打包成含有所有依賴的jar包,不能使用 maven-assembly-plugin 因為他不能處理 META-INF/services目錄結(jié)構(gòu),而應(yīng)該使用maven-shade-plugin 其配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
注意:如果你想要把你的應(yīng)用入口主程序 main.class 加入jar包以便運行,你需要做如下操作:
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.elasticsearch.demo.Generate</mainClass>
</transformer>
部署項目在 JBoss EAP6 module.
Elasticsearch 和 Lucene classes 需要部署在同一個 JBoss module
配置module.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<module name="org.elasticsearch">
<resources>
<!-- Elasticsearch -->
<resource-root path="elasticsearch-2.0.0.jar"/>
<!-- Lucene -->
<resource-root path="lucene-core-5.1.0.jar"/>
<resource-root path="lucene-analyzers-common-5.1.0.jar"/>
<resource-root path="lucene-queries-5.1.0.jar"/>
<resource-root path="lucene-memory-5.1.0.jar"/>
<resource-root path="lucene-highlighter-5.1.0.jar"/>
<resource-root path="lucene-queryparser-5.1.0.jar"/>
<resource-root path="lucene-sandbox-5.1.0.jar"/>
<resource-root path="lucene-suggest-5.1.0.jar"/>
<resource-root path="lucene-misc-5.1.0.jar"/>
<resource-root path="lucene-join-5.1.0.jar"/>
<resource-root path="lucene-grouping-5.1.0.jar"/>
<resource-root path="lucene-spatial-5.1.0.jar"/>
<resource-root path="lucene-expressions-5.1.0.jar"/>
<!-- Insert other resources here -->
</resources>
<dependencies>
<module name="sun.jdk" export="true" >
<imports>
<include path="sun/misc/Unsafe" />
</imports>
</module>
<module name="org.apache.log4j"/>
<module name="org.apache.commons.logging"/>
<module name="javax.api"/>
</dependencies>
</module>
補充:
Lucene是一套用于全文檢索和搜尋的開源程式庫.
Lucene是當(dāng)前以及最近幾年最受歡迎的免費Java信息檢索程序庫;人們經(jīng)常提到信息檢索程序庫,雖然與搜索引擎有關(guān),但不應(yīng)該將信息檢索程序庫與搜索引擎相混淆
client
你可以通過多種方式使用 Java client:
- 在一個已有的集群中,執(zhí)行標(biāo)準(zhǔn)的索引(index)、獲取、刪除、搜索操作
- 在一個運行中的集群,執(zhí)行管理任務(wù)
最簡單的獲取client的方式
通過與集群相連的TransportClient
注意:client 的主程序版本必須和集群中的節(jié)點一致,如果有些微的不同會導(dǎo)致某些新增功能無法使用. Clients 應(yīng)該和cluster的版本一致.
Transport Client
TransportClient 使用 the transport 模塊與Elasticsearch cluster 遠程連接.它并沒有加入集群中,而僅僅是獲取一個或多個傳輸?shù)刂凡⑶乙詒ound robin fashion(原諒我暫時無法翻譯!!!)在每個操作上通信 (though most actions will probably be "two hop" operations).
// on startup
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
// on shutdown
client.close();
如果你所使用的集群名稱不是 "elasticsearch",你就需要設(shè)置你的集群名稱
Settings settings = Settings.builder()
.put("cluster.name", "myClusterName").build();
TransportClient client = new PreBuiltTransportClient(settings);
//Add transport addresses and do something with the client...
所謂的鼻息特征: sniffing feature
Transport client 可以動態(tài)添加新的或刪除舊的主機
原理說明
1,在sniffing 被啟用時 transport client會連接它內(nèi)部通過調(diào)用 addTransportAddress 建立的節(jié)點列表. 此后, transport client會調(diào)用這些節(jié)點的內(nèi)部集群狀態(tài)API 去發(fā)現(xiàn)可用的數(shù)據(jù)節(jié)點. 這個client的內(nèi)部節(jié)點列表將僅僅被這些數(shù)據(jù)節(jié)點替代.
2,這個數(shù)據(jù)列表默認每5秒刷新一次
3,sniffer所連接的ip地址都是在es的配置中定義聲明的
4,這個列表可能不會包含它所連接的初始節(jié)點,如果改初始節(jié)點不是數(shù)據(jù)節(jié)點
例如:如果你初始化連接了一個主機節(jié)點,在sniffing之后,不會請求訪問這個主機節(jié)點,而是請求訪問其他任意的數(shù)據(jù)節(jié)點,因為排除非數(shù)據(jù)節(jié)點可以避免非數(shù)據(jù)主節(jié)點的搜索阻塞
啟用sniffing: set client.transport.sniff 為 true
Settings settings = Settings.settingsBuilder()
.put("client.transport.sniff", true).build();
TransportClient client = new PreBuiltTransportClient(settings);
其他 transport client 參數(shù)設(shè)置說明:
client.transport.ignore_cluster_name
設(shè)為true 時將忽略對連接的節(jié)點集群名稱的有效性驗證(since 0.19.4)
client.transport.ping_timeout
等待ping一個節(jié)點的操作響應(yīng)時間;默認5s.
client.transport.nodes_sampler_interval
sample / ping節(jié)點列表和連接的節(jié)點的頻率. 默認5s.
** coordinating node **
如果一個節(jié)點沒有主節(jié)點的功能,不能持有數(shù)據(jù),也不能預(yù)處理文件,那么這個節(jié)點就是一個 coordinating node ,它只能發(fā)送請求,處理減少搜索時間,分配批處理索引.最重要的是可以作為 協(xié)調(diào)節(jié)點的智能負載平衡器。
Coordinating only nodes在大集群的時候是非常有用的,可以通過從數(shù)據(jù)節(jié)點和主節(jié)點上將轉(zhuǎn)移負擔(dān)到 協(xié)調(diào)節(jié)點. 協(xié)調(diào)節(jié)點加入集群并像其他節(jié)點一樣,接受完整的集群狀態(tài)信息,并利用這些信息去匹配尋找最佳請求路徑
過多的Coordinating only nodes會增加集群的負擔(dān)
Connecting a Client to a Coordinating Only Node
在應(yīng)用里創(chuàng)建一個連接到 Coordinating Only Node的TransportClient
你就可以加載任何你需要的插件.例如 搜索插件